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. Adding children to a QWidget, but not showing...
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.6k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    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

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      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

      SPlattenS 1 Reply Last reply
      1
      • SPlattenS 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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #3

          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

          SPlattenS 1 Reply Last reply
          1
          • SGaistS SGaist

            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.

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

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

            • Login

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