Custom Widget creation to display in QGridLayout cell.
-
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.
-
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.
@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 -
@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