Solved trying to figure out layouts for MainWindow
-
I learned about creating a QMainWindow with the "Main Window" example. Easy. Then I explored the "Tab Dialog Box" example. I saw a clear difference in how each one was done. With Main Window, QMainWindow is sort of your master parent object that you put stuff in. With Tab Dialog Box, a QDialog object is what you put QTabWidgets into.
I can only assume that the Tab Dialog Box example chose to create the QDialog object in main() instead of creating a MainWindow to put it in, for the sake of cutting down on code to show a bare-bones example. So, I decided to try to integrate the two examples together to see if I could do it.
I did not have much success with trying to make a TabDialog object (that's the QDialog class from Tab Dialog Box example) and somehow putting it into my MainWindow. I had to cut out the QDialog object altogether, get rid of the TabDialog class, and just write the QTabWidget stuff out from within MainWindow(). Lastly, I slapped setCentralWidget(tabs); in there and it all worked.
There is a problem that I discovered however, which is that a QMainWindow only allows one widget in it - the central widget. I want to put more than just the tabs in my MainWindow. So, I tried to make an empty Qwidget, set it as the central widget, then add the tab stuff to it. Then I can control the layouts within that widget. I tried to just make a private function within the MainWindow class to do this, but any time I call setLayout() for the container Qwidget from within a MainWindow class function, it warns me that MainWindow already has a layout. So, I figured that I would need to put my tab code in a whole new class, try to create that object class thing from MainWindow(), and then my setLayout() wouldn't think I was trying to make it apply to MainWindow.
So, I did all of that, but now since the tab and container widget code is in a new class, I can't call setCentralWidget() to make the blank widget the central widget because it's not in a MainWindow function any more.
At this point, I'm tired. I've rewritten this code so many times it's not even funny. I feel frustrated that I learned about layouts, but the second I create a MainWindow, it's like "no you can't do that now". I feel like I'm going about all of this wrong and I'm getting no where fast.
All I want to do is
- make a MainWindow
- make a dummy QWidget as the central widget
- apply a layout to that QWidget so I can put lots of widgets into it and organize them
That's all I'm trying to do.
-
Hi, welcome to devnet.
QMainWindow has its own, private, layout that makes all the docking magic happen. So you shouldn't set layouts on main window. You can set a central widget and that puts it into that private layout of main window. then you can set layout on that "dummy" and put in it whatever you want.
The code that does that is basically 3 lines of preparation and off you go:
auto lay = new QVBoxLayout(); //or whichever layout you want // .. put stuff into the layout, e.g. the tab widget yourMainWindow->setCentralWidget(new QWidget()); yourMainWindow->centralWidget()->setLayout(lay);
Where you put it is up to you. It can be a method of your main window subclass or you can do it in main function or from wherever you have access to main window object.
-
this you can do with Fake mainwindow as centralwidget in the main window.It's like having fake mainwindow in the mainwindow.
MainWindow:: MainWindow() { QWidget *main_window = new QWidget(); //Fake Mainwindow to the MainWindow. This will be your dummy grid_fake_mainwindow = new QMainWindow(); //http://doc.qt.io/qt-5/qtwidgets-mainwindows-mainwindow-example.html gridid_widget->addWidget ( grid_Fake_mainwindow ); ///you can add any number of docks widgets here to you dummy mainwindow. //Here you will have three layers horizontally QHBoxLayout *HLayout = new QHBoxLayout; HLayout->addLayout(RightHlayout_anything tab or anytjing as you need ); HLayout->addLayout(LeftHlayout_anything tab or anything as you need ); HLayout->addLayout(grid_widget); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(HLayout); main_window->setLayout(mainLayout); //set mainwindow as central setCentralWidget(main_window);}
-
Thank you both. I had not caught on to the usefulness of doing something like "new myClass()" inside of another method as is done in setCentralWidget(new QWidget()). I had done it in previous code with tabWidget->addTab(new myTab(), tr("myTab")); but wasn't quite used to that type of code. Classes are my weakness I guess.
Thanks so much for the help. I think I will be well on my way to doing some fancy stuff with my program now that I've gotten over that bump in the road.
-
If you don't yet feel comfortable with creating instances in-place you can always create them explicitly:
auto widget = new QWidget(); widget->setLayout(lay); yourMainWindow->setCentralWidget(widget);
it's just more typing and I'm lazy :)