Results 1 to 6 of 6

Thread: Adding class to MainWindow - using QHBoxLayout ?

  1. #1
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Adding class to MainWindow - using QHBoxLayout ?

    My objective is to develop the application using modular approach.
    Using QtDesigner I build EMPTY MainWindow , then using same method I build another blank, EMPTY class.
    In this example QMdiArea.
    Next step is to populate QMdiArea class / form as desired.

    Next step is to ADD the QMdiArea class to MainWindow - so it became itergral part of MainWindow, no loger idependelty floating.

    I found this snippet of code which supposedly works, but I am getting runtime error
    and my QMdiArea widget is stiil afloat, pretty much out of control of main window.


    Qt Code:
    1. // create layout
    2. // Set layout
    3. QHBoxLayout *layout = new QHBoxLayout;
    4. this->setLayout(layout);
    5. // add mdi area class
    6. MdiArea *mdiarea = new MdiArea();
    7. mdiarea->show();
    8. // add to main window
    9. layout->addWidget(mdiarea);
    10. mdiarea->show();
    To copy to clipboard, switch view to plain text mode 


    Here is the error
    Starting /media/z/DEV_COPY_LABEL/Qt/QT/qtconnectivity/examples/bluetooth/build-CAT_BT-Desktop-Debug/btscanner...
    QWidget::setLayout: Attempting to set QLayout "" on MainWindow_BT_MDI "MainWindow_BT_MDI", which already has a layout
    OK, main window has "layout" - so how do I physically use it to add my QMdiArea class ?

    PS
    There seems to be standard method to add "QDock" to main window , but I have not tried it, not yet - next task.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding class to MainWindow - using QHBoxLayout ?

    Qt Code:
    1. MainWindow::MainWindow( QWidget * parent )
    2. : QMainWindow( parent )
    3. , ui ( new whateveryouruinameis() )
    4. {
    5. ui->setupUi( this );
    6.  
    7. mpMdiArea = new QMdiArea( this ); // mpMdiArea is a member variable of MainWindow
    8. setCentralWidget( mpMdiArea );
    9. }
    To copy to clipboard, switch view to plain text mode 

    The MainWindow itself already has a layout, and setting the central widget will place the QMdiArea inside the layout it uses to manage the content.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Adding class to MainWindow - using QHBoxLayout ?

    I have tested this code and it works.
    Qt Code:
    1. QHBoxLayout *layout = new QHBoxLayout;
    2. this->setLayout(layout);
    3. this->ui->setupUi(this);
    4. // build mdi area
    5. MdiArea *mdiarea = new MdiArea();
    6. this->layout()->addWidget(mdiarea);
    To copy to clipboard, switch view to plain text mode 

    Could you elaborate on differences between using
    layout and using setCentralWidget( mpMdiArea );?

    Would I use using both methods - one to initially add base class (setCentralWidget) and then adding more classes
    using layout ?


    Added after 31 minutes:


    Here is the latest code
    Qt Code:
    1. this->ui->setupUi(this);
    2. MdiArea *mdiarea = new MdiArea();
    3. setCentralWidget( mdiarea );
    4. this->layout()->addWidget(mdiarea);
    To copy to clipboard, switch view to plain text mode 
    Please note - setCentralWidget( mdiarea ); indieed adds QMdiArea ONLY - as entire MainWindow.
    layout ACTAULLY adds the real QMdiArea class into MainWindow as desired.

    However. I get run time complains...but runs.

    appmenu-qt: handleReparent 143 The given QMenuBar is already registered by appmenu-qt5, skipping
    QMainWindowLayout::addItem: Please use the public QMainWindow API instead
    /media/z/DEV_COPY_LABEL/Qt/QT/qtconnectivity/examples/bluetooth/build-CAT_BT-Desktop-Debug/btscanner exited with code 0
    Last edited by anneranch; 4th September 2020 at 20:54.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding class to MainWindow - using QHBoxLayout ?

    However. I get run time complains...but runs.
    You again seem to be dead set on misusing Qt widgets. The entire purpose of QMainWindow is that is provides a framework with a menubar, toolbar, status bar, optional docking windows, and a central area into which you put the main part of your UI. QMainWindow has been designed in such a way that -it- manages the layout of all these subcomponents so that when you dock or undock widgets, add toolbars, resize your app, or whatever, QMainWindow's layout manager will rearrange things so they just work the way you would expect them to.

    Use QMainWindow the way it is supposed to be used, don't keep trying to invent your own broken wheel.

    By inserting something directly into QMainWindow's layout, you will probably break something which then causes QMainWindow to behave incorrectly when something about the child widget arrangement changes. Yes, it might run, but it won't work correctly.

    setCentralWidget( mdiarea );
    this->layout()->addWidget(mdiarea);
    You apparently don't understand that a widget can have only one parent. When you set your MDI area as the central widget, that makes its parent whatever the central widget uses to manage that central area. It is probably not the QMainWindow's layout. So when you set your MDI area as the central widget, and then add it to QMainWindow's layout, it removes it from being the central widget and makes its parent the QMainWindow itself.

    Would I use using both methods - one to initially add base class (setCentralWidget) and then adding more classes
    using layout ?
    NO. Design your entire application as if the central widget is the only part of the UI you have control of. If you want to use QMdiArea as a central widget, use it as the central widget and add all of the rest of your UI as QMdiSubWindow instances inside the QMdiArea, using the QMdiArea API. If you want to base your application around a QTabWidget as the central widget, make a custom QWidget for each page, and add them to the tab widget using the QTabWidget API. Same thing if you want to use QStackedWidget instead. Do not try to game the system by directly accessing subcomponents of these widget like layouts and manually adding things to them. You will almost certainly break what they are designed to manage. Just because you can access a subcomponent doesn't mean you should. These "manager" widgets have their own APIs for a reason.

    No matter what type of central widget you decide to use, if your application is going to have multiple "views", then each of these "views" will almost certainly be some QWidget-based class that you add layouts and child widgets to before adding that widget as a child of whatever manager you are using as a central widget (A QMdiSubWindow of a QMdiArea, a tab of a QTabWidget, or a child of a QStackedWidget, or something else).

    Your application is a hierarchy of widgets, QMainWidget at the top, the central (manager) widget below that, and all of your custom widgets as children of that.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Aug 2020
    Posts
    28
    Qt products
    Qt5

    Default Re: Adding class to MainWindow - using QHBoxLayout ?

    In so many words and with usual way to make me feel like an idiot for asking I now have BETTER understanding of widgets.
    Thanks.
    Especially the structure of QMainWindow.
    Simple - it contains three QWidgets and the one I want to work with is the "centralwidget".
    I have NO issues adding to "centralwidget" using QtDesigner GUI, i will be looking into HOW to add generic QWidget in code.
    By generic I mean simple "button" or entire class - such as QDock.
    I'll ask for more help IF I NEED IT.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding class to MainWindow - using QHBoxLayout ?

    In so many words and with usual way to make me feel like an idiot for asking I now have BETTER understanding of widgets.
    Sorry, I don't mean to make you feel like an idiot, but so many times I give you example code, and then you seem to decide you want to do something different that doesn't work. Follow my examples, follow the many, many Qt examples that use QMainWindow, and you won't spend so much time trying to get your UI to work.

    If you are using Qt Creator, look on the Welcome page. You'll see 100 or more projects under Examples, and a bunch of video and other tutorials on the Tutorials page. Many of the QWidget-based examples use QMainWindow as a start and add their UI to the central widget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. show object (scene) in widget in class class mainwindow
    By rimie23 in forum Qt Programming
    Replies: 8
    Last Post: 1st May 2012, 17:15
  2. Replies: 4
    Last Post: 23rd August 2011, 22:53
  3. Replies: 7
    Last Post: 18th August 2011, 15:43
  4. Replies: 11
    Last Post: 25th February 2009, 18:35
  5. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 08:57

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.