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

Widgets not visible in QGridLayout


  • Moderators

    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?


  • Lifetime Qt Champion

    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 ?


  • Lifetime Qt Champion

    @kshegunov said in Widgets not visible in QGridLayout:

    RbDisplayGridLayout

    Is that a subclass of QGridLayout ?


  • Moderators

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


  • Lifetime Qt Champion

    So basically they were just hidden ?


  • Moderators

    @mrjj said in Widgets not visible in QGridLayout:

    So basically they were just hidden ?

    Yes, very much so.


  • Lifetime Qt Champion

    Intriguing behaviour !

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

    Nice catch !


Log in to reply