Qt World Summit: Submit your Presentation

Can't execute new UI or setModel.

  • Hi! I'm not sure where to post questions but since everyone's putting them here, I might as well do the same.
    I'm having trouble opening a new UI form with a pushbutton using the following code:

    void MainWindow::on_MyPushButton_clicked()
      MainMenu mainmenu; //Where MainMenu is the new UI
       mainmenu.setModel (true);
    //error: C2039: 'setModel' : is not a member of 'MainMenu'
    //error: C2039: 'exec' : is not a member of 'MainMenu'

    I tried Googling but to no avail. I read that setModelrequires a specific library, but including it doesn't seem to work?

    I'm using: Qt Creator 3.4.2 (opensource)

    Also for reference, here are the files:

    This allows my UI to popup on top of the existing one. Anyway to make it so that a single, and newest, UI is displayed only?


  • your question is unclear.
    do you want the button to open another instance of your program? do you want to show another, different, dialog? what does the function setModel() do? it isn't a Qt function.

    in short, to open a new dialog, that, for example, asks the user some question, ore tells him somthing, use QDialog or one of the classes that insherrits it.

    If you want to hide your main dialog, do this->hide()

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you maybe mean setModal ? In this case these two functions belongs to QDialog which should be the base class for MainMenu. What should MainMenu do ?

  • @yoavmil Sorry for being unclear.
    I want the pushbutton to open another UI and then hide the original UI.

    Ex: You're on form One.ui. You press a button and Two.ui opens while One.ui disappears. Hope I was clear.

  • @SGaist I'm really embarrassed for making such a mistake; Yes, I did mean setModal instead of setModel.
    Though I changed my mistake to setModal and I added #include <QDialog> but that didn't work either. I thought that perhaps I was including the library in the wrong file, so I included it in all the files for testing but that didn't work either. I received the following errors on every try:

    mainwindow.cpp:38: error: C2039: 'setModal' : is not a member of 'MainMenu'
    mainmenu.h:11: see declaration of 'MainMenu'
    mainwindow.cpp:39: error: C2039: 'exec' : is not a member of 'MainMenu'
    mainmenu.h:11: see declaration of 'MainMenu'

    The following is the code for mainmenu.h:11

    class MainMenu : public QMainWindow
        explicit MainMenu(QWidget *parent = 0);
    private slots:
            void showTime(); /*For Time*/
        Ui::MainMenu *ui;

    Nothing unusual and to me, everything seems in order.

  • Lifetime Qt Champion

    Except that your MainMenu is a QMainWindow not a QDialog

  • @SGaist I sorta noticed that after I peered through my own stupidity.
    I made the following changes, and it works fine.


    void MainWindow::on_MyPushButton_clicked()
       MainMenuWin = new MainMenu();
       MainMenuWin -> show();

    Added as a private data member of class 'MainWindow'

       MainMenu *MainMenuWin;

    After that, I just went to the MainMenu UI, selected the form and changed the windowModality as can be seen below:

    Thanks for your help and sorry for being a bother :P
    I will definetly use @yoavmil 's advice for hiding a UI.

  • Lifetime Qt Champion

    Beware, you have a memory leak, each time on_MyPushButton_clicked is called you create a new MainMenu object

  • Oh right, I forgot to mention that you have to delete the pointer MainMenuWin as such:

    If the mainwindow.cpp file already referances the deconstructor, then add this to the deconsturctor:

        delete ui;
        delete MainMenuWin; //Add this to delete the dynamically created pointer ( new MainMenu(); )

    If you don't have the deconstructor referance in mainwindow.cpp, go to mainwindow.h and look for the deconstructor in the public part of the class:

        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow(); //Deconstructor

    Just convert it to the following:

        explicit MainWindow(QWidget *parent = 0);
                   delete MainMenuWin; //Delete pointer

    Sorry if this seems like trivial baby talk to most of you; I just like explaining it step-by-step in case a beginner reads this thread.
    Thanks again for your help!

  • Lifetime Qt Champion

    That doesn't solve the problem, you are still creating a new MainMenuWin each time on_MyPushButton_clicked is called. So either add a check in the slot to only construct it once, or do it in the constructor.

  • I understand what you're saying. But in my program, the user can only call the function once and then the program will close, running the deoconstructor. So I don't think I will face the problem of having the button pressed multiple times and having MainMenuWin created more than once.
    But I guess I will implement a check to make sure that if the function is called more than once (for any possible reason), MainMenuWin will not be dynamically created again.
    Thank you again!

    if ( MainMenuWin != &MainMenu() ) {MainMenuWin = new MainMenu();}
    /* If Pointer MainMenuWin points to the address of MainMenu, then MainMenu has already been dynamically created
    and there is no need to create it once more. */

    Hope I'm on the right track :D

  • Moderators

    To avoid having to worry about duplicates or deleting pointers (or even storing them) you might want to simply create the thing and set a Qt::WA_DeleteOnClose flag to let Qt delete the object for you when you close the window:

    void MainWindow::on_MyPushButton_clicked()
       auto mm = new MainMenu();

    Unless of course you don't want multiple copies of the window to be shown at the same time.

  • @Chris-Kawa Oh wow. I didn't even know about that! Thanks! :D

Log in to reply