Layout issues
-
I have combined
QVBoxLayoutwithQHBoxLayoutand it fixes the issue but it not properly displays widgets?
Update:
These issues are with 800x600,1024x768, 1152x864 screen resolutions. From 1280x720 works normal. -
I have combined
QVBoxLayoutwithQHBoxLayoutand it fixes the issue but it not properly displays widgets?
Update:
These issues are with 800x600,1024x768, 1152x864 screen resolutions. From 1280x720 works normal.@Cobra91151 Yea your layout code is definitely bad. Can you post the code so we can either duplicate the problem or at least see where you are going wrong?
It doesn't have to be the actual code you could throw some dummy code into a sample app that duplicates the issue so we can play with it.
-
Hi,
Why are you setting your QStackedLayout inside a QHBoxLayout before setting it on the widget ? It looks like a layer to much.
You could maybe even remove that widget altoghether and use a QStackedWidget directly, no ?
-
@Cobra91151 Yea your layout code is definitely bad. Can you post the code so we can either duplicate the problem or at least see where you are going wrong?
It doesn't have to be the actual code you could throw some dummy code into a sample app that duplicates the issue so we can play with it.
Hi!
I useQStackedLayoutto emulate tab behavior. I will testQStackedWidgetand post code later. -
I have tested
QStackedWidget, but the issue is still present on small screen resolutions. I have deleted all layouts and added QHBoxLayout for apply and exit buttons.This is without layout. QHBoxLayout has just buttons (Apply/Exit).

Code:
QListWidget *settingsView = new QListWidget(tabSettings); //tabSettings is QTabWidget QWidget *appSettingsTab = new QWidget(settingsView); QStackedWidget *settingsStackedWidget = new QStackedWidget(); settingsStackedWidget->addWidget(appSettingsTab); QHBoxLayout *appSettingsButtonLayout = new QHBoxLayout(); appSettingsButtonLayout->addStretch(0); appSettingsButtonLayout->setAlignment(Qt::AlignBottom | Qt::AlignRight); appSettingsButtonLayout->addWidget(appSettingsApplyButton); appSettingsButtonLayout->addWidget(appSettingsExitButton); appSettingsTab->setLayout(appSettingsButtonLayout); QHBoxLayout *allSettingsDataLayout = new QHBoxLayout(); allSettingsDataLayout->addWidget(settingsStackedWidget); allSettingsDataLayout->setContentsMargins(0, 0, 0, 0); settingsView->setLayout(allSettingsDataLayout);So how should I combine layouts to display widgets in three columns?
-
What does small screen represent ?
-
QWidgets are not fully visible on 800x600,1024x768, 1152x864 screen resolutions.
-
Ok, so pretty standard sizes.
Why are you putting
settingsViewinappSettingsTaband not directly in the QStackedWidget ? That makessettingsViewa "floating" widget insideappSettingsTab. -
Ok, so pretty standard sizes.
Why are you putting
settingsViewinappSettingsTaband not directly in the QStackedWidget ? That makessettingsViewa "floating" widget insideappSettingsTab.I have app with horizontal tabs (
QTabWidgets), the last tab is settings and it displays two views for a list of different settings menu and settings which controlsQStackedWidget. I will try to combine layouts and post code later. -
I also have layout issue with buttons:
Code:
QVBoxLayout *firstButtonsColumnLayout = new QVBoxLayout; firstButtonsColumnLayout->addStretch(0); firstButtonsColumnLayout->setContentsMargins(0, 0, 0, 0); firstButtonsColumnLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); firstButtonsColumnLayout->addWidget(registryButton, 0,); firstButtonsColumnLayout->addWidget(recycleBinButton, 0); firstButtonsColumnLayout->addWidget(deviceManagerButton, 0); firstButtonsColumnLayout->addWidget(windowsUpdateButton, 0); firstButtonsColumnLayout->addWidget(systemConfigButton, 0); QHBoxLayout *testLayout = new QHBoxLayout; testLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); testLayout->addLayout(firstButtonsColumnLayout); commandsTab->setLayout(testLayout);The problem is that buttons are not fully visible at small screen resolutions without layouts. So when I have added layout to some of them it displayed bottom not top.
Screenshot:

It should look like this with layouts:

How to fix these layout issues? What layout I should use?
-
Not knowing how you build the rest of the buttons, it's not really possible to guess what is going wrong.
-
I also have layout issue with buttons:
Code:
QVBoxLayout *firstButtonsColumnLayout = new QVBoxLayout; firstButtonsColumnLayout->addStretch(0); firstButtonsColumnLayout->setContentsMargins(0, 0, 0, 0); firstButtonsColumnLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); firstButtonsColumnLayout->addWidget(registryButton, 0,); firstButtonsColumnLayout->addWidget(recycleBinButton, 0); firstButtonsColumnLayout->addWidget(deviceManagerButton, 0); firstButtonsColumnLayout->addWidget(windowsUpdateButton, 0); firstButtonsColumnLayout->addWidget(systemConfigButton, 0); QHBoxLayout *testLayout = new QHBoxLayout; testLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft); testLayout->addLayout(firstButtonsColumnLayout); commandsTab->setLayout(testLayout);The problem is that buttons are not fully visible at small screen resolutions without layouts. So when I have added layout to some of them it displayed bottom not top.
Screenshot:

It should look like this with layouts:

How to fix these layout issues? What layout I should use?
@Cobra91151 Since the project you are doing is big enough not to share the code, it's really hard for us to figure out what is going on.
How about this.. Can you write up a quick/small project that just uses dummy widgets to show the same problem. Something you can share with us so we can see what you are doing wrong?
That way it is minimal code to share, and there is nothing that is proprietary being given to us on the forums here.
I don't see anything wrong, but I can't see the whole picture either. I don't have these issues with layouts and can't duplicate your problem.
-
Ok. I will create small project and post it here later.
Update:
I will upload test project on Mega and post link here. -
Test project is available here - Mega. Thanks.
-
Test project is available here - Mega. Thanks.
@Cobra91151 Ok in looking at your example project, the major problem I see is you are using fixed sizing for everything. When you use fixed sizing you are limiting what the layout can do to help display your widgets. This is why you are having issues with things going off screen.
So questions:
- Why do you want to use fixed sizes?
- How do you want your buttons to look? Is it the second picture you posted with the "All Commands"? I can try to throw together a quick layout for you that has those buttons in it.
-
Test project is available here - Mega. Thanks.
@Cobra91151
jep @ambershark is right,
aQGridLayoutwhere your buttons are imbedded should solve all the layout issues you have with them.QGridLayouts allow Layoutitems to span more than 1 row or column, therefore it's the best option here, as long as you want those 2 special buttons that are twice as wide as the others.
At the moment you order them in 4 different
QVBoxLayoutsand 1QHBoxLayoutthat combination that does not really support the multi column span mechanic you want.to add a widget to a QGridlayout you can do the following:
QGridLayout* gLay = new QGridlayout(); //add widget the default way at cell (0,0): gLay->addWidget(myWidget, 0,0); //add widget that spans 2 columns at (origin) cell (0,1): gLay->addWidget(myWidget,0,1,1,2) -
@Cobra91151 Ok in looking at your example project, the major problem I see is you are using fixed sizing for everything. When you use fixed sizing you are limiting what the layout can do to help display your widgets. This is why you are having issues with things going off screen.
So questions:
- Why do you want to use fixed sizes?
- How do you want your buttons to look? Is it the second picture you posted with the "All Commands"? I can try to throw together a quick layout for you that has those buttons in it.
- To get buttons fix size and position them.
- Yes, like in the second picture.
I have changed code to
setGeometryfunction but it looks not like in the second picture. The question is how to control widgets size and position in layouts, or what layout should I use? -
@Cobra91151
jep @ambershark is right,
aQGridLayoutwhere your buttons are imbedded should solve all the layout issues you have with them.QGridLayouts allow Layoutitems to span more than 1 row or column, therefore it's the best option here, as long as you want those 2 special buttons that are twice as wide as the others.
At the moment you order them in 4 different
QVBoxLayoutsand 1QHBoxLayoutthat combination that does not really support the multi column span mechanic you want.to add a widget to a QGridlayout you can do the following:
QGridLayout* gLay = new QGridlayout(); //add widget the default way at cell (0,0): gLay->addWidget(myWidget, 0,0); //add widget that spans 2 columns at (origin) cell (0,1): gLay->addWidget(myWidget,0,1,1,2) -
@Cobra91151 Here ya go.. This does the buttons with a grid exactly as you want them.. Scales up and down to a minimum just below 800. You could get them smaller if you wanted by changing font and reducing wording on your buttons.
#include <QApplication> #include <QWidget> #include <QGridLayout> #include <QPushButton> int main(int ac, char **av) { QApplication app(ac, av); // create a main window QWidget mw; mw.resize(800, 600); mw.show(); // create a layout with our buttons auto grid = new QGridLayout; grid->addWidget(new QPushButton("Registry"), 0, 0); grid->addWidget(new QPushButton("File Explorer"), 0, 1); grid->addWidget(new QPushButton("System Information"), 0, 2); grid->addWidget(new QPushButton("DX Diagnostic Tool"), 0, 3); grid->addWidget(new QPushButton("Windows Defender"), 0, 4); grid->addWidget(new QPushButton("Recycle Bin"), 1, 0); grid->addWidget(new QPushButton("Task Manager"), 1, 1); grid->addWidget(new QPushButton("Computer Management"), 1, 2); grid->addWidget(new QPushButton("Windows Firewall"), 1, 3); grid->addWidget(new QPushButton("Command Prompt"), 1, 4); grid->addWidget(new QPushButton("Device Manager"), 2, 0); grid->addWidget(new QPushButton("Programs and Features"), 2, 1, 1, 2); grid->addWidget(new QPushButton("Control Panel"), 2, 3); grid->addWidget(new QPushButton("Devices and Printers"), 2, 4); grid->addWidget(new QPushButton("Windows Update"), 3, 0); grid->addWidget(new QPushButton("About Windows"), 3, 1); grid->addWidget(new QPushButton("Date and Time"), 3, 2); grid->addWidget(new QPushButton("Network and Sharing Center"), 3, 3, 1, 2); grid->addWidget(new QPushButton("System Configuration"), 4, 0); grid->addWidget(new QPushButton("Power Options"), 4, 1); // create a vbox to hold our grid so we can add stretches to squish it to look proper auto layout = new QVBoxLayout; layout->addStretch(1); layout->addLayout(grid); layout->addStretch(1); // set layout on our widget mw.setLayout(layout); return app.exec(); }[edit: fixed small typo SGaist]
-
@Cobra91151 Here ya go.. This does the buttons with a grid exactly as you want them.. Scales up and down to a minimum just below 800. You could get them smaller if you wanted by changing font and reducing wording on your buttons.
#include <QApplication> #include <QWidget> #include <QGridLayout> #include <QPushButton> int main(int ac, char **av) { QApplication app(ac, av); // create a main window QWidget mw; mw.resize(800, 600); mw.show(); // create a layout with our buttons auto grid = new QGridLayout; grid->addWidget(new QPushButton("Registry"), 0, 0); grid->addWidget(new QPushButton("File Explorer"), 0, 1); grid->addWidget(new QPushButton("System Information"), 0, 2); grid->addWidget(new QPushButton("DX Diagnostic Tool"), 0, 3); grid->addWidget(new QPushButton("Windows Defender"), 0, 4); grid->addWidget(new QPushButton("Recycle Bin"), 1, 0); grid->addWidget(new QPushButton("Task Manager"), 1, 1); grid->addWidget(new QPushButton("Computer Management"), 1, 2); grid->addWidget(new QPushButton("Windows Firewall"), 1, 3); grid->addWidget(new QPushButton("Command Prompt"), 1, 4); grid->addWidget(new QPushButton("Device Manager"), 2, 0); grid->addWidget(new QPushButton("Programs and Features"), 2, 1, 1, 2); grid->addWidget(new QPushButton("Control Panel"), 2, 3); grid->addWidget(new QPushButton("Devices and Printers"), 2, 4); grid->addWidget(new QPushButton("Windows Update"), 3, 0); grid->addWidget(new QPushButton("About Windows"), 3, 1); grid->addWidget(new QPushButton("Date and Time"), 3, 2); grid->addWidget(new QPushButton("Network and Sharing Center"), 3, 3, 1, 2); grid->addWidget(new QPushButton("System Configuration"), 4, 0); grid->addWidget(new QPushButton("Power Options"), 4, 1); // create a vbox to hold our grid so we can add stretches to squish it to look proper auto layout = new QVBoxLayout; layout->addStretch(1); layout->addLayout(grid); layout->addStretch(1); // set layout on our widget mw.setLayout(layout); return app.exec(); }[edit: fixed small typo SGaist]
Thanks for the example code. I have fixed buttons layout issue adding
QSizePolicy testSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);. But the problem with adding layouts to different widgets still present andQGridLayoutis not working well with it. For example:
I need to add some layout to display widgets like in the image above. What layout should I use?