Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adding children to a QWidget, but not showing...



  • I have a Window that I am creating programmatically, this bit works fine the window is rendered with Close, Minimise and Maximise widgets. I'm trying to add additional widgets programmatically to the window in the display area. I can see that after creating and adding the new widgets by calling:

    QObjectList objList = pobjWidget->children();
    

    The objList length is incrementing. I've added four instances of QLabel to the Window, however for some reason that are not showing. Here is the styles I have applied to each label:

        static const char scszLabelProperties[] = 
                            "QLabel { text-align:left;"
                                     "vertical-align: text-top;"
                                     "border:0px;"
                                     "color:#000000;"
                                     "format:rich;"
                                     "text-decoration:none; }";
    

    When the label is created a pointer to the parent Widget / Window is passed QLabel constructor. Can anyone suggest what might be wrong?

    I can post more source code if required, just let me know what?


  • Lifetime Qt Champion

    Hi,

    Just passing a parent to a widget created at run time won't make it visible. You have to explicitly call show on it.



  • @SPlatten
    Start by removing any stylesheet. Is this a stylesheet or a widget adding issue? If it's the adding, don't complicate by introducing/mentioning stylesheet


  • Lifetime Qt Champion

    Hi,

    Just passing a parent to a widget created at run time won't make it visible. You have to explicitly call show on it.



  • @JonB , I started off with no stylesheet at all, thats why I added the style sheet.

    @SGaist , I also have the code:

                    if ( objList.length() == 0 ) {
        //No children
                        ptOffsets.setX(0);
                        ptOffsets.setY(0);
                    } else {
        //Set offset to last child
                        QWidget* pobjLastWidget = static_cast<QWidget*>(objList[objList.length() - 1]);
                        ptOffsets = pobjLastWidget->geometry().bottomLeft();
                    }
        //Get existing node geometry
                    QRect rctGeom = pobjNode->rctGetGeometry();
        //Offset into poisition
                    rctGeom.setTopLeft(ptOffsets);
                    pobjNode->setGeometry(rctGeom);
                    pobjNode->setVisible(true);
    

    Where pobjNode is a generic class that is used for all the widget types and each of the methods being called effect the widget, for example:

    void clsXMLnode::setGeometry(QRect& rrctGeom) {
        bool blnValid;
    
        if ( (blnValid = rrctGeom.isValid()) == true ) {
    //If there is a widget associated with this node set it's geometry
            if ( mpobjWidget != nullptr ) {
                mrctGeom = rrctGeom;
                mblnPendingGeom = false;
                mpobjWidget->setGeometry(mrctGeom);
            } else {
                blnValid = false;
            }
        }
        if ( blnValid != true ) {
            mblnPendingGeom = true;
            mrctPending = rrctGeom;
        }
    }
    @SGaist , however, I just added:
    
                QWidget* pobjChild = pobjNode->pobjGetWidget();
                pobjChild->update();
                pobjChild->show();
    
    And now they are showing, so I have a bug in my helper class which I will investigate, thank you.

Log in to reply