Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    3
    4
    732
    Loading More Posts
    • 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.
    • SPlatten
      SPlatten last edited by SPlatten

      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?

      Kind Regards,
      Sy

      JonB 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

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

        SPlatten 1 Reply Last reply Reply Quote 1
        • JonB
          JonB @SPlatten last edited by JonB

          @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

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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.

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

            SPlatten 1 Reply Last reply Reply Quote 1
            • SPlatten
              SPlatten @SGaist last edited by SPlatten

              @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.

              Kind Regards,
              Sy

              1 Reply Last reply Reply Quote 0
              • First post
                Last post