[Solved] QTabWidget does not respect spacing from layout
-
I have this test case:
// Scroll QScrollArea *sa = new QScrollArea(ui->centralWidget); sa->setWidgetResizable( true ); // Layout for widgets QVBoxLayout *vl_2 = new QVBoxLayout(); vl_2->setSpacing(0); // Widget to attach the scroll to and the layout QWidget *widget = new QWidget() widget->setLayout(vl_2); sa->setWidget(widget); // Test widgets QComboBox *cb_1 = new QComboBox(); QComboBox *cb_2 = new QComboBox(); vl_2->addWidget( cb_1 ); vl_2->addWidget( cb_2 );
And the widgets have 0 space between them.
But if I add them to a QTabWdiget, it all breaks as if QTabWidget does not respect the set setSpacing(0);
// TabWidget QTabWidget *run_results = new QTabWidget(ui->centralWidget); run_results->resize( this->size().width() -20, this->size().height() -80 ); run_results->show(); // Scroll QScrollArea *sa = new QScrollArea(ui->centralWidget); sa->setWidgetResizable( true ); // Layout for widgets QVBoxLayout *vl_2 = new QVBoxLayout(); vl_2->setSpacing(0); // Widget to attach the scroll to and the layout QWidget *widget = new QWidget() widget->setLayout(vl_2); sa->setWidget(widget); // Add the scroll to as the TabWidget tab. run_results->addTab(sa, "test"); // Test widgets QComboBox *cb_1 = new QComboBox(); QComboBox *cb_2 = new QComboBox(); vl_2->addWidget( cb_1 ); vl_2->addWidget( cb_2 );
Anyone know what I need to do to force QTabWidget to not resize and move my widgets so that they take all the space?
I tried to add Qt::AlignTop to the addWdiget but it did nothing other than place the first widget at the top and the next in the middle of the screen.
-
If I am not mistaken, you shouldn't be using
setSpacing
. Spacing refers to the space between items in, say, aQVBoxLayout
. Rather, it sounds like you want to make margins around yourQTabWidget
be zero.You do that with
run_results->setContentsMargins(0, 0, 0, 0);
-
Thanks for the reply.
I actually want to set the spacing between my widgets. I do not want it to be 0 but that is just my example. The problem is that when I put them into a QTabWidget's tab item the widgets are placed in such a way as to take as much space as possible. They are stretched from 1 side of the QTabWidget to the other and the space between them is big (so that it has equal space above the first widget, after it, and after the last widget).
-
Hi and welcome to devnet,
If I understand you correctly you would like to have your two combo boxes on top of each other at the top of your widget then add
vl_2->addStretch(1);
after
vl_2->addWidget( cb_2 );
Hope it helps
-
Thanks for the suggestion.
It does work. I can make it "work" for my project but I would prefer if there is a way for me to force QTabWidget to respect the setSpacing of the Layer. Let me explain why.
I have 1 GridLayer that is on the widget of the QTabWidget's tab. Inside that GridLayer I add QVBoxLayers as cells. Think of row 1 of GridLayer with 4 QVBoxLayers as 4 columns. The number of QVBoxLayers per row and the number of rows of the GridLayer is controlled dynamically at run time. Inside each QVBoxLayer I have a QHBoxLayers as rows containing 4 widgets each. Think of the QHBoxLayers as drawers. There are different number of QHBoxLayers per QVBoxLayer, again controlled dynamically.
As you can see it is a bit more complex than my example with the 2 QComboBoxes. If I use addStretch(1) after I add a QHBoxLayer to a QVBoxLayer I will have to setStretch(0) the previously added stretch and addStretch(1) again to the end. That is not an issue but I would like if there is a solution in which I can avoid this. Why is it that QT works as expected (at least what I expected as normal from it) when I add all these layers to the centralWidget but if I add them to the QTabWidget it requires addStretch(1)?
-
I have this test case:
// Scroll QScrollArea *sa = new QScrollArea(ui->centralWidget); sa->setWidgetResizable( true ); // Layout for widgets QVBoxLayout *vl_2 = new QVBoxLayout(); vl_2->setSpacing(0); // Widget to attach the scroll to and the layout QWidget *widget = new QWidget() widget->setLayout(vl_2); sa->setWidget(widget); // Test widgets QComboBox *cb_1 = new QComboBox(); QComboBox *cb_2 = new QComboBox(); vl_2->addWidget( cb_1 ); vl_2->addWidget( cb_2 );
And the widgets have 0 space between them.
But if I add them to a QTabWdiget, it all breaks as if QTabWidget does not respect the set setSpacing(0);
// TabWidget QTabWidget *run_results = new QTabWidget(ui->centralWidget); run_results->resize( this->size().width() -20, this->size().height() -80 ); run_results->show(); // Scroll QScrollArea *sa = new QScrollArea(ui->centralWidget); sa->setWidgetResizable( true ); // Layout for widgets QVBoxLayout *vl_2 = new QVBoxLayout(); vl_2->setSpacing(0); // Widget to attach the scroll to and the layout QWidget *widget = new QWidget() widget->setLayout(vl_2); sa->setWidget(widget); // Add the scroll to as the TabWidget tab. run_results->addTab(sa, "test"); // Test widgets QComboBox *cb_1 = new QComboBox(); QComboBox *cb_2 = new QComboBox(); vl_2->addWidget( cb_1 ); vl_2->addWidget( cb_2 );
Anyone know what I need to do to force QTabWidget to not resize and move my widgets so that they take all the space?
I tried to add Qt::AlignTop to the addWdiget but it did nothing other than place the first widget at the top and the next in the middle of the screen.
Hi,
@Anastaziel said:
Why is it that QT works as expected (at least what I expected as normal from it) when I add all these layers to the centralWidget but if I add them to the QTabWidget it requires addStretch(1)?
The difference lies in how you put the QScrollArea inside the parent widget.
@Anastaziel said:
QScrollArea *sa = new QScrollArea(ui->centralWidget);
Here, you simply set centralWidget as the parent of sa. You did not set sa as the central widget, and you did not put sa inside the central widget's layout. This means:
- sa is placed in the upper-left corner of centralWidget.
- If you enlarge your QMainWindow, centralWidget will also get enlarged, but sa will not.
However, things will be very different if you did it this way (try it!):
// Method X QScrollArea *sa = new QScrollArea; ui->setCentralWidget(sa);
@Anastaziel said:
run_results->addTab(sa, "test");
Here, you set sa as the "central widget" of the QTabWidget, just like Method X that I posted above. This means:
- sa always fills the entire space taken by the tab.
- If you enlarge your QTabWidget, sa will also get enlarged.
There are 2 ways to achieve what you want:
- (Method X) Add 1 more row and column to your QGridLayout 1 more row and column, and call setRowStretch()/setColumnStretch() on them.
- (Your original QMainWindow method) Don't add sa directly to the QTabWidget. Instead, create a blank QWidget, make it sa's parent, and add your blank QWidget to the QTabWidget.
-
Thanks @JKSH. Now I understand what the problem was. I fixed it by the second approach. :)
How do I mark something as answered?
-
You're welcome, @Anastaziel :)
At the bottom of the page, click "Topic Tools" -> "Mark Solved"
-
I am sorry to post again but on "Topic Tools" I only have "Delete Topic".
-
I am sorry to post again but on "Topic Tools" I only have "Delete Topic".
@Anastaziel said:
I am sorry to post again but on "Topic Tools" I only have "Delete Topic".
I reported this at http://forum.qt.io/topic/51926/real-quick-forum-things/4. At the moment, it looks like only moderators can mark "solved". Tero, our community manager, is looking into this.
For now, I've marked it for you.