November 25, 2010

liangjingmenghua liangjingmen..
Lab Rat
3 posts

[Solved] How to make the center widget maxmize together with the application?

 

I created a text editor followed by the lab exercise, but I found that the center widget which is actually the QTextWidget, could not maximize together with the application, which means that the QTextEdit widget is still original size even after I clicked the maximize button of the application and the application maximized as expected.
Please help me to figure out why and how to fix, thanks.

15 replies

November 25, 2010

Alexander Kuchumov Alexander Kuchumov
Lab Rat
379 posts

Can you show code?

November 25, 2010

Volker Volker
Ant Farmer
5428 posts

Seems that you did not set a layout.

November 26, 2010

IrQX IrQX
Lab Rat
74 posts

may be using

  1. setCentralWidget(QWidget * widget)
will help you?

November 26, 2010

Alexander Kuchumov Alexander Kuchumov
Lab Rat
379 posts

IrQX, it works only for QMainWindow

November 26, 2010

liangjingmenghua liangjingmen..
Lab Rat
3 posts

Thanks all
I just called setCentralWidget(ui->textEdit), and it works.
Thanks

November 26, 2010

soroush soroush
Lab Rat
818 posts

If you want to see your central widget in the Designer, first call qmake and find the

  1. setupUi
method in ui_yourForm.h
there should be somethings like
  1. 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:
  2. @<widget class="QWidget" name="centralWidget">

and also remove its corresponding
  1. </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

  1. MainWindow->setCentralWidget
and use unnecessary memory for two central widgets.

note: Hey! this is my first post…

November 27, 2010

Volker Volker
Ant Farmer
5428 posts

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.

November 27, 2010

soroush soroush
Lab Rat
818 posts
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?

November 27, 2010

Volker Volker
Ant Farmer
5428 posts

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.

November 28, 2010

soroush soroush
Lab Rat
818 posts

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?

November 28, 2010

Volker Volker
Ant Farmer
5428 posts

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:

  1. Open Qt Designer
  2. Create a new MainWindow
  3. put a QTextEdit in the central area
  4. add a layout to the central area
  5. save the file and close Designer
  6. open the .ui file in a text edtior and remove the two lines mentioned above
  7. save the .ui file and close the text editor
  8. 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().

November 28, 2010

soroush soroush
Lab Rat
818 posts

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:

  1. I’d like to be able to change type of my central widgets (or perhaps any other widget) by Designer IDE (QtCreator or QtDesigner) ;-)
  2. 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.

November 28, 2010

Volker Volker
Ant Farmer
5428 posts

Setting the centralWidget twice is an error.

There are two ways to create user interfaces:

  1. Open Qt Designer, add all the widgets you need, put them in some layouts and you are done
  2. 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.

November 28, 2010

soroush soroush
Lab Rat
818 posts

Thanks for your useful explanation.

February 4, 2014

Radmir Radmir
Lab Rat
1 posts

Hi, All,
may be you’ll prefer the next way:

  1. launch QtCreator,
  2. add widget (e.g. QTextEdit),
  3. select instance of QMainWindow in Object Inspector,
  4. press ctrl+H or ctrl+L to choose layout,
  5. save form and test app.

Regards,
Radmir

 
  ‹‹ Strange output when I use QString fileName = QFileDialog::getOpenFileName()      QXmlDefaultHandler fatalError\error\warning are not triggered during invalid XML doc parsing ››

You must log in to post a reply. Not a member yet? Register here!