Widgets not visible in QGridLayout
-
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 simpleQLabel
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 theQGridLayout
.
Did I miss something obvious? -
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 ? -
@kshegunov said in Widgets not visible in QGridLayout:
RbDisplayGridLayout
Is that a subclass of QGridLayout ?
-
@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
). Callingshow
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.
-
So basically they were just hidden ?
-
@mrjj said in Widgets not visible in QGridLayout:
So basically they were just hidden ?
Yes, very much so.
-
Intriguing behaviour !
I thought about suggesting adding a show call but forgot to add it 😅
Nice catch !
-
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.