QLayouts Will Not Cooperate
-
Hi, I'm trying to learn Qt and so I create a new QWidgets Project, here's the code I've written in the constructor of the MainWindow that is automatically generated by the project
//setup menubar this->createMenu(); QTableWidget* aTable = new QTableWidget(0, 2, this); QVBoxLayout* mainLayout = new QVBoxLayout; mainLayout->setMenuBar(ui->menuBar); mainLayout->addWidget(aTable, 1); setLayout(mainLayout);
It looks like this with the tablewidget placed on top of the menubar no matter what I do:
I've tried nesting other layouts inside of mainLayout and it does nothing it always looks like that. Can someone tell me what I'm doing wrong?
-
Hi and welcome to the forums.
The ui->toolbar is already in a layout (for an std GUI project)
so not sure why you reassigned it to the main layout?
( Did you make a project that is not using a UI file ?)Also what Design are you actually after?
Should the QTableWidget really be up with the toolbar or do you mean to have it in the center ?
-
I'm using just the standard QWidget project settings with a .ui file. I would like the QTable widget to be in the center of the MainWindow dialog.
The design I'm after is a simple utility, the QTableWidget will be underneath the menubar, and underneath the QTableWidget, I'd like a button. I'm writing just a simple /proc/ vfs monitor for linux that will fill the QTableWidget with running processes and when I click a process it will open a child dialog and display it's loaded .so files by parsing /proc/[pid]/maps.
So, if I nest mainlayout in MainWindow's existing layout it will layout properly or should I just remove the menubar from the existing layout and add it to mainlayout first?
-
@nob0dy
Hi
MainWindow Widget is a bit special as it has a center widget called centralWidget where
the user can put his stuff. you can just leave the menu and toolbar alone.so i think you are looking for something like
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QTableWidget *aTable = new QTableWidget(0, 2, this); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(aTable, 1); centralWidget()->setLayout(mainLayout); // we apply the layout to this central widget, not MainWindow directly }
However, its a requirement to do this from code ?
The Designer lets you do this very easy.
Click the MainWindow.ui file
Drag a QTableWidget to the center of it.
Right click, on empty area next to the QTableWidget,
From the layout menu, choose a layout.
Now drag a PushButton from left to under the QTableWidget.
Watch the blue line it show. when in place release the mouse button and have the button inserted. -
@mrjj said in QLayouts Will Not Cooperate:
However, its a requirement to do this from code ?
The Designer lets you do this very easy.
Click the MainWindow.ui file
Drag a QTableWidget to the center of it.
Right click, on empty area next to the QTableWidget,
From the layout menu, choose a layout.
Now drag a PushButton from left to under the QTableWidget.
Watch the blue line it show. when in place release the mouse button and have the button inserteHi, thanks a lot. I didn't know that centralwidget in MainWindow was what was causing all the confusion, it's fixed by swapping my line of code:
this->setLayout(mainLayout);
with your line of code:
this->centralWidget()->setLayout(mainLayout);
On the topic of whether it's required to do this from code, no it's not but I'd also like to learn the layouts manually that way if I were to say not use QtCreator and write code with vim/tmux or whatever else I wouldn't feel crippled without QtCreator's ui designer (which is great btw). I first learned about signals and slots through QT creator's UI and since it made it so easy for me I didn't have a good understanding on how to do signals/slots from code manually.
-
@nob0dy
Ok good plan.
Besides the auto connect from Creator is very convenient but not very reliable as it
breaks if you later rename the widget or the slot.
So manual connect is the way to go away.Also, just in case you didnt notice.
Anything UI you draw is converted to c++ code.
(look inside setupUI() )So you can use Designer to design something and then take the code and
reuse.