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. Widgets not visible in QGridLayout
QtWS25 Last Chance

Widgets not visible in QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 3.2k 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.
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by
    #1

    Hello,
    I've been struggling with a problem for a couple of days now and I'm pretty much at a loss. Here's the relevant code:

    void RbDisplayGridLayout::put(const RbGraphWidgetList & list)
    {
        layout()->addWidget(new QLabel("test"));  //< Widget shows
        for (int i = 0; i < list.size(); i++)
            layout()->addWidget(list[i]); //< Widget doesn't show
    }
    

    The list is defined as:

    typedef QVector<RbGraphWidget *> RbGraphWidgetList;
    

    and the RbGraphWidget is just a dummy defined with:

    RbGraphWidget::RbGraphWidget(const Graph & meta, QWidget *parent)
        : QWidget(parent), graph(meta)
    {
        ui.setupUi(this);
    
        setWindowTitle(graph.name);
        ui.label->setText(graph.name);
    }
    

    The ui.label is a simple QLabel that shows a string, nothing fancy here.

    The odd thing is that I can see the dummy widgets just fine if I add them to a QTabWidget, but they don't display when I add them to the QGridLayout.
    Did I miss something obvious?

    Read and abide by the Qt Code of Conduct

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

      Hi,

      That looks correct...

      Some silly tests:

      • add one of your custom widget the same way you do with your QLabel before the loop.
      • create the list of your custom widget in the same method you pass them to your grid layout.

      Are these widget used elsewhere ?

      What version of Qt are you running ?
      On what OS ?

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

      kshegunovK 1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @kshegunov said in Widgets not visible in QGridLayout:

        RbDisplayGridLayout

        Is that a subclass of QGridLayout ?

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          That looks correct...

          Some silly tests:

          • add one of your custom widget the same way you do with your QLabel before the loop.
          • create the list of your custom widget in the same method you pass them to your grid layout.

          Are these widget used elsewhere ?

          What version of Qt are you running ?
          On what OS ?

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @SGaist said in Widgets not visible in QGridLayout:

          Hi,

          Hi. I solved it, but I'm going to add answers to the questions just for posterity.

          That looks correct...

          It turns out it is correct.

          Some silly tests:

          • add one of your custom widget the same way you do with your QLabel before the loop.

          Same result. The first thing I tried.

          • create the list of your custom widget in the same method you pass them to your grid layout.

          Hadn't tested that, but if I had, then I'd have had less gray hair. Your spidey sense is correct.

          Are these widget used elsewhere ?

          Yes, they're taken out from a QTabWidget as this:

          RbGraphWidgetList RbDisplayTabLayout::take()
          {
              int size = count();
          
              RbGraphWidgetList list;
              list.reserve(size);
          
              for (int i = 0; i < size; i++)
                  list.append(reinterpret_cast<RbGraphWidget *>(widget(i)));
          
              QTabWidget::clear();
          
              // We need a clean slate before reinserting the widgets into another layout
              for (QWidget * widget : list)
                  widget->setParent(nullptr);
          
              return list;
          }
          

          What version of Qt are you running ?

          Qt 5.14.x

          On what OS ?

          Linux (debian testing), but I imagine the windows is going to behave the same.


          Now what the problem was and how it is solved:

          The problem is that (all) the widgets I get are hidden even the active one (probably by QTabWidget::clear). Calling show on them when I add them to the layout fixes the problem. Why is that isn't exactly clear to me, but it appears to be some idiosyncrasy of the tab widget I was not aware of, nor is it spelled in the docs explicitly.


          EDIT:

          @mrjj said in Widgets not visible in QGridLayout:

          @kshegunov said in Widgets not visible in QGridLayout:

          RbDisplayGridLayout

          Is that a subclass of QGridLayout ?

          No, it's a widget.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            So basically they were just hidden ?

            kshegunovK 1 Reply Last reply
            1
            • mrjjM mrjj

              So basically they were just hidden ?

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @mrjj said in Widgets not visible in QGridLayout:

              So basically they were just hidden ?

              Yes, very much so.

              Read and abide by the Qt Code of Conduct

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

                Intriguing behaviour !

                I thought about suggesting adding a show call but forgot to add it 😅

                Nice catch !

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

                1 Reply Last reply
                2
                • osirisgothraO Offline
                  osirisgothraO Offline
                  osirisgothra
                  wrote on last edited by
                  #8

                  fwiw, late though it may be, i found an odd behavior with some custom widgets requiring you to explicitly set the row/colspan when adding the item (eg, insertWidget(item,row,col,1,1)) -- this does not seem to happen with custom widgets that are a collection of custom controls, just ones that do their own custom painting in paintEvent.

                  I'm truly glad you r/offmychess t finally, but please don't go too far, because you r/beyondvoxels and that implies that u r/donewithlife. Oh well time to git back to the lab, because azure sea here, I have a lot of work to do...

                  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