Important: Please read the Qt Code of Conduct -

open multiple windows from a base window

  • I am building my first app in Qt5. I have a window that I open multiple windows from.
    I would like to keep more than on open from the base window.
    How is this done?
    Your help and time is appreciated.

  • Lifetime Qt Champion

    Hi and welcome
    well, if you use dialogs (for the extra windows)
    then you should read about modal and
    modeless dialogs.
    When modeless, you can open as many you like and still be able to click on main window.
    is that what you want?

    The big difference is how you call to "open" it.
    ->exec() // its blocks
    ->show() // it shows dialog and execute next line ( not block)

    Beware the beginners trap:

    void OpenMyDialog() {
    QDialog mydia;;
    Since show don't block, it will exit the function and
    mydia will be deleted.

    So keep your dialogs instances in qmainwindow or use pointers.

  • Thanks mrjj for your response.
    This is the code I am using from the 'dashboard' module.

    Some more information on my situation.
    I have studied modal and modeless as you suggested.
    I have created a 'dashboard' that stays up and it is modal. From this 'dashboard' all apps are opened.
    When an app is set to modeless and is selected it appears for a mil sec and disappears.
    I have not been able to find the code that will keep modeless windows up.
    This is the code I am using to launch the app from the 'dashboard':
    ViewReceipt viewreceipt;

    Thanks again for your help!

  • Lifetime Qt Champion

    that kinda look as beginners trap :)
    if this is in a function

    ViewReceipt viewreceipt;
    ----- here viewreceipt is deleted and closed

    ViewReceipt* = new viewreceipt;

    works, but you will have mem leak.
    so its better to put ViewReceipt viewreceipt; in the base window (in .,h , in the class)
    as a class member and then just call show() when needed.

  • This is what I found after being pointed to the correct path by mrjj, thanks so much sir.
    included the modelessdialog.h file to basewin_dialog.cpp

    added this code:
    // modeless heap memory uses arrow pointer
    pmodeless =new ModelessDialog(this);

    added in the private: section:
    ModelessDialog *pmodeless;

    now opens and the basewin_dialog is released
    and now there can be mulitple windows opened

    Works fine every time.

    The only thing left is the problem of memory leak.
    Will the exit close (this->close();) of the modeless win take care of memory leaks.
    Or does it need to be dealt with another way?

  • Lifetime Qt Champion

    ++The only thing left is the problem of memory leak.
    ++Will the exit close (this->close();) of the modeless win take care of memory leaks.
    nope. all you NEW must be deleted unless you give it to a parent that owns it.
    For dialogs, you should delete them yourself.

    Other options is to use
    (in dialog)
    that means it cleans up when closed but be sure you then new it again before
    calling show() else u crash.

    so since u have ModelessDialog *pmodeless; in private section.
    I suggest just to new in basewin_dialog constructor and
    delete in basewin_dialog destructor so that class will clean up.
    will be the easiest. :)

    But actually , you don't have to new them
    you can have (in class private , NOT in function :)
    ModelessDialog pmodeless; // not pointer
    and just call;
    notice that its "." and not ->
    Then its deleted when basewin_dialog is.

Log in to reply