Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget and addanchor?
Qt 6.11 is out! See what's new in the release blog

QWidget and addanchor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 545 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    Is there something wrong with the information on this page?
    https://doc.qt.io/qt-6/qtwidgets-graphicsview-simpleanchorlayout-example.html

    Because I am trying to add a widget and I get the error message:

    error: Cannot initialize a parameter of type 'QGraphicsLayoutItem *' with an lvalue of type 'QWidget *'
    

    However looking again, its not a QWidget at all, its defined as Widget.

    What I am trying to do is add anchor functionality to my widgets, is this possible ?

    Kind Regards,
    Sy

    JoeCFDJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      Is there something wrong with the information on this page?
      https://doc.qt.io/qt-6/qtwidgets-graphicsview-simpleanchorlayout-example.html

      Because I am trying to add a widget and I get the error message:

      error: Cannot initialize a parameter of type 'QGraphicsLayoutItem *' with an lvalue of type 'QWidget *'
      

      However looking again, its not a QWidget at all, its defined as Widget.

      What I am trying to do is add anchor functionality to my widgets, is this possible ?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      @SPlatten
      Look at the source code here:
      https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/graphicsview/simpleanchorlayout/main.cpp?h=6.6

      class Widget : public QGraphicsWidget
      {
      public:
         Widget(const QColor &color, const QColor &textColor, const QString &caption,
                QGraphicsItem *parent = nullptr)
             : QGraphicsWidget(parent)
             , caption(caption)
             , color(color)
             , textColor(textColor)
         {
         }
      
         void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * = nullptr) override
         {
             QFont font;
             font.setPixelSize(0.75 * qMin(boundingRect().width(), boundingRect().height()));
      
             painter->fillRect(boundingRect(), color);
             painter->save();
             painter->setFont(font);
             painter->setPen(textColor);
             painter->drawText(boundingRect(), Qt::AlignCenter, caption);
             painter->restore();
         }
      
      private:
         QString caption;
         QColor color;
         QColor textColor;
      };
      
      SPlattenS 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @SPlatten
        Look at the source code here:
        https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/graphicsview/simpleanchorlayout/main.cpp?h=6.6

        class Widget : public QGraphicsWidget
        {
        public:
           Widget(const QColor &color, const QColor &textColor, const QString &caption,
                  QGraphicsItem *parent = nullptr)
               : QGraphicsWidget(parent)
               , caption(caption)
               , color(color)
               , textColor(textColor)
           {
           }
        
           void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * = nullptr) override
           {
               QFont font;
               font.setPixelSize(0.75 * qMin(boundingRect().width(), boundingRect().height()));
        
               painter->fillRect(boundingRect(), color);
               painter->save();
               painter->setFont(font);
               painter->setPen(textColor);
               painter->drawText(boundingRect(), Qt::AlignCenter, caption);
               painter->restore();
           }
        
        private:
           QString caption;
           QColor color;
           QColor textColor;
        };
        
        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @JoeCFD said in QWidget and addanchor?:

        https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/graphicsview/simpleanchorlayout/main.cpp?h=6.6

        Thank you, however it doesn't look it supports regular QWidgets, If I want to add a QPushButton with anchors can that be done with this code?

        Kind Regards,
        Sy

        JoeCFDJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @JoeCFD said in QWidget and addanchor?:

          https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/graphicsview/simpleanchorlayout/main.cpp?h=6.6

          Thank you, however it doesn't look it supports regular QWidgets, If I want to add a QPushButton with anchors can that be done with this code?

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4

          @SPlatten Are you trying to add a widget on top of a scene graph?
          If yes, check here out
          https://forum.qt.io/topic/71151/adding-a-qwidget-to-a-qgraphicsscene-clearing-it-then-adding-the-qwidget-again/7

          SPlattenS 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @SPlatten Are you trying to add a widget on top of a scene graph?
            If yes, check here out
            https://forum.qt.io/topic/71151/adding-a-qwidget-to-a-qgraphicsscene-clearing-it-then-adding-the-qwidget-again/7

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @JoeCFD said in QWidget and addanchor?:

            https://forum.qt.io/topic/71151/adding-a-qwidget-to-a-qgraphicsscene-clearing-it-then-adding-the-qwidget-again/7

            What I am trying to do is add the capability for any widgets to be added to any layout with any anchors.

            I believe I can write my own implementation.

            Kind Regards,
            Sy

            SGaistS 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JoeCFD said in QWidget and addanchor?:

              https://forum.qt.io/topic/71151/adding-a-qwidget-to-a-qgraphicsscene-clearing-it-then-adding-the-qwidget-again/7

              What I am trying to do is add the capability for any widgets to be added to any layout with any anchors.

              I believe I can write my own implementation.

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @SPlatten hi,

              You can add a widget to a QGraphicsScene and you get a QGraphicsProxyWidget that you can then add to your layout.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved