Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to make the center widget maxmize together with the application?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
16 Posts 6 Posters 17.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    liangjingmenghua
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alexander
      wrote on last edited by
      #2

      Can you show code?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        Seems that you did not set a layout.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • I Offline
          I Offline
          IrQX
          wrote on last edited by
          #4

          may be using @setCentralWidget(QWidget * widget)@ will help you?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            alexander
            wrote on last edited by
            #5

            IrQX, it works only for QMainWindow

            1 Reply Last reply
            0
            • L Offline
              L Offline
              liangjingmenghua
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                soroush
                wrote on last edited by
                #7

                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...

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    soroush
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      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.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        soroush
                        wrote on last edited by
                        #11

                        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?

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          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().

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            soroush
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #14

                              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.

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                soroush
                                wrote on last edited by
                                #15

                                Thanks for your useful explanation.

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  Radmir
                                  wrote on last edited by
                                  #16

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

                                  launch QtCreator,

                                  add widget (e.g. QTextEdit),

                                  select instance of QMainWindow in Object Inspector,

                                  press ctrl+H or ctrl+L to choose layout,

                                  save form and test app.

                                  Regards,
                                  Radmir

                                  1 Reply Last reply
                                  1

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved