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. Custom Widget creation to display in QGridLayout cell.
Forum Update on Monday, May 27th 2025

Custom Widget creation to display in QGridLayout cell.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 246 Views
  • 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 15 Jun 2021, 07:55 last edited by
    #1

    I am creating a new class based on QWidget, its very early stages. So far the paintEvent function contains just:

    void truth::paintEvent(QPaintEvent* pEvent) {
        QRect rctGeom(geometry());
        QColor colBG(mobjBGcol.toString());
    
        bool blnValidgeom(rctGeom.isValid());
        bool blnValidColor(colBG.isValid());
    qDebug() << rctGeom.x() << ", " << rctGeom.y() << ", " << rctGeom.width() << ", " << rctGeom.height();
    
        QPainter objVisible(this);
        objVisible.fillRect(rctGeom, colBG);
    }
    

    mobjBGcol is an instance of QJsonValue which contains #ffff0077.

    With the above code I see nothing, if I insert:

    rctGeom.setX(0);
    rctGeom.setY(0);
    this->setGeometry(rctGeom);
    

    Above the qDebug() line, I see a magenta filled rectangle but not in the correct location. The widget I'm creating is added to a QGridLayout with:

    mpui->pgrdloContent->addWidget(pGroundTruth, 2, 2);
    

    What I really want to is get the widget to fill the cell in the grid layout, with the setGeometry it doesn't and the rectangle appears on the top left of the rectangle not in the layout at all. Without the geometry setting, nothing is appearing at all.

    Kind Regards,
    Sy

    J 1 Reply Last reply 15 Jun 2021, 08:01
    0
    • S SPlatten
      15 Jun 2021, 07:55

      I am creating a new class based on QWidget, its very early stages. So far the paintEvent function contains just:

      void truth::paintEvent(QPaintEvent* pEvent) {
          QRect rctGeom(geometry());
          QColor colBG(mobjBGcol.toString());
      
          bool blnValidgeom(rctGeom.isValid());
          bool blnValidColor(colBG.isValid());
      qDebug() << rctGeom.x() << ", " << rctGeom.y() << ", " << rctGeom.width() << ", " << rctGeom.height();
      
          QPainter objVisible(this);
          objVisible.fillRect(rctGeom, colBG);
      }
      

      mobjBGcol is an instance of QJsonValue which contains #ffff0077.

      With the above code I see nothing, if I insert:

      rctGeom.setX(0);
      rctGeom.setY(0);
      this->setGeometry(rctGeom);
      

      Above the qDebug() line, I see a magenta filled rectangle but not in the correct location. The widget I'm creating is added to a QGridLayout with:

      mpui->pgrdloContent->addWidget(pGroundTruth, 2, 2);
      

      What I really want to is get the widget to fill the cell in the grid layout, with the setGeometry it doesn't and the rectangle appears on the top left of the rectangle not in the layout at all. Without the geometry setting, nothing is appearing at all.

      J Online
      J Online
      jsulm
      Lifetime Qt Champion
      wrote on 15 Jun 2021, 08:01 last edited by
      #2

      @SPlatten said in Custom Widget creation to display in QGridLayout cell.:

      What I really want to is get the widget to fill the cell in the grid layout

      The widget can only paint on its own surface.
      https://doc.qt.io/qt-5/qwidget.html#geometry-prop: "This property holds the geometry of the widget relative to its parent and excluding the window frame". What you need is https://doc.qt.io/qt-5/qwidget.html#rect-prop

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 15 Jun 2021, 08:09
      2
      • J jsulm
        15 Jun 2021, 08:01

        @SPlatten said in Custom Widget creation to display in QGridLayout cell.:

        What I really want to is get the widget to fill the cell in the grid layout

        The widget can only paint on its own surface.
        https://doc.qt.io/qt-5/qwidget.html#geometry-prop: "This property holds the geometry of the widget relative to its parent and excluding the window frame". What you need is https://doc.qt.io/qt-5/qwidget.html#rect-prop

        S Offline
        S Offline
        SPlatten
        wrote on 15 Jun 2021, 08:09 last edited by
        #3

        @jsulm , excellent thank you!

        Kind Regards,
        Sy

        1 Reply Last reply
        0

        1/3

        15 Jun 2021, 07:55

        • Login

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