[Solved] How to make the center widget maxmize together with the application?
-
Thanks all
I just called setCentralWidget(ui->textEdit), and it works.
Thanks -
If you want to see your central widget in the Designer, first call qmake and find the @setupUi@ method in ui_yourForm.h
there should be somethings like @MainWindow->setCentralWidget(centralWidget)@, just replace centralWidget with your textEdit or any other widget. At this step do not run qmake. Open your ui file with your favorite text editor and remove following line:
@<widget class="QWidget" name="centralWidget">@
and also remove its corresponding @</widget>@ (closing tag). do not remove contents of the central widget (lines between tag start and end) because your textEdit is somewhere there.This makes your widget really to be central widget of your form (at runtime and in the designer). The central widget is specified once during application run. so you don't have to call two @MainWindow->setCentralWidget@ and use unnecessary memory for two central widgets.
note: Hey! this is my first post...
-
I don't recommend following this advice!
Qt Designer complains about an inconsistency in the .ui file. Fiddling around in the .ui XML is error prone and the way Qt Designer setups the GUI elements is definitely okay!
One may save one QWidget and one Layout, but the memory footprint and other overhead are negligible in a real world application.
-
Volker: Could you explain when an error will occur? This is like changing type of your central widget from QWidget to QTextEdit, and QTextEdit is an inherited type from QWidget. So why QtDesigner complains? Here another question arises: Is central widget restricted to be from type of QWidget?
note: I Always do like that in my semi-real world applications and nothing goes wrong (yet). Should I change all of my codes?
-
Just try it - reopen your manipulated .ui file in Qt Designer. If you remove the two lines you will get the error.
It will occur because Qt Designer designs QMainWindow classes the way they are designed - sorry, it's a kind of take-it-or-leave-it thingy. You cannot change the structures that designer relies on from the outside in an incompatible way and expect it not to complain.
No, it is not necessary that the central widget must be a QWidget, it only must be a subclass of QWidget. You can always set, let's say, a QTextEdit as the central widget if you construct the UI on your own, not using Qt Designer (which is completely ok and sometimes necessary). The use of an additional QWidget with a layout is just the way Qt Designer works and does no harm to your application. The overhead is marginal and is definitely not worth the hassles of manipulating ui files manually. Also, if you use Qt Designer you must not set the centralWidget on your own, this is done in the setupUI() method that is generated wit uic.
-
I didn't get any error. I try it with QtDesigner, QtCreator, and Qt Eclipse Integration. I don't understand what's wrong with setting central widget to a widget of QTextEdit type? If there is just one widget needed in the MainWindow, why we should keep old central widget and set new one at runtime?
-
Nothing is wrong with that. As I mentioned earlier, there are two ways to create your UI - the manual one, where you call setCentralWidget() in your own code and the Designer way, where uic does all the work for you. You should NOT modify the designer generated .ui files manually, nor should you modify the uic generated ui_xxx.h files (the latter one's will be overwritten by uic if you modify the ui file anyways).
To see what's wrong with your suggestion, just follow your advise step by step:
Open Qt Designer
Create a new MainWindow
put a QTextEdit in the central area
add a layout to the central area
save the file and close Designer
open the .ui file in a text edtior and remove the two lines mentioned above
save the .ui file and close the text editor
reopen the .ui file in Designer
Then you get the error message.
Edit:
Why do you bother to modify the central widget at runtime? Your UI is setup correctly from Designer, so there is no need to call setCentralWidget() yourself (this is done in ui_xxxx.h)!If the original poster does not use a Designer generated UI, he must call setCentralWidget().
-
I do all of these (of course except 4th step), because I think setting the central widget twice isn't the best way.
Once ui->setupUi(this) from MainWindow sets central widget to a simple QWidget, and once again we set central widget to another inherited widget. So first QWidget is unused during program run.I tried to write an MDI app today in the same way. After creating new project and before adding any other other widgets, I opened ui file and changed type of the central widget to QMdiArea. Is this wrong? if is then what do you suggest to do?
Note:
I'd like to be able to change type of my central widgets (or perhaps any other widget) by Designer IDE (QtCreator or QtDesigner) ;-)
I often do like above if I want to set central widget unless if there were written codes depended on widget I want to be central, or if there were more than one widget in central container.
-
Setting the centralWidget twice is an error.
There are two ways to create user interfaces:
Open Qt Designer, add all the widgets you need, put them in some layouts and you are done
Don't use Qt Designer and create the UI manually in your .cpp class
If you go with the first approach, you will call uic which in turn creates an ui_xxx.h file for you, where the C++ code resides that actually creates the UI during runtime. When using a QMainWindow there is a QWidget set as centralWidget. To this widget you add your "worker" widgets (eg a QTextEdit, some buttons, etc.) and nicely arrange them with a layout. You must not set a central widget in your C++ class that uses this ui. If you want to create an MDI app you put the QMdiArea instead a QTextEdit into the centralWidget and add a layout to the widget. The centralWidget is indeed "only" a QWidget in this case. It holds as a container for the widget that you put on it. I don't see any disadvantages of this approach - thousands of Qt users do it this way without any problems or complaints.
If you are not satisfied with the .ui files that Designer creates for you, you probably should not use Designer at all and create the UI manually - that's the second way. Actually, I strongly advise against fiddling around the .ui files. It's error prone and may completely screw up the UI, at worst in an not recoverable way.
One can combine both methods. I've done it several times, but only by adding a generic a QFrame, a QWidget, a QStackedWidget or similar to provide a generic container. This container widget can be used to add other widgets from the C++ code. I've never manipulated the .ui file, nor had I any need for this.