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. Can't execute new UI or setModel.
QtWS25 Last Chance

Can't execute new UI or setModel.

Scheduled Pinned Locked Moved Solved General and Desktop
setmodelexec
13 Posts 4 Posters 9.2k 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.
  • B Offline
    B Offline
    Brownies
    wrote on last edited by A Former User
    #1

    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'
       mainmenu.exec();
    //error: C2039: 'exec' : is not a member of 'MainMenu'
    }
    

    I tried Googling but to no avail. I read that setModel requires 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:
    http://s16.postimg.org/rak5e3c45/Help.png

    note:
    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?

    Thanks!

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yoavmil
      wrote on last edited by
      #2

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

      B 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        B 1 Reply Last reply
        0
        • Y yoavmil

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

          B Offline
          B Offline
          Brownies
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • SGaistS SGaist

            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 ?

            B Offline
            B Offline
            Brownies
            wrote on last edited by
            #5

            @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
            {
                Q_OBJECT
            public:
                explicit MainMenu(QWidget *parent = 0);
                ~MainMenu();
            private slots:
                    void showTime(); /*For Time*/
            private:
                Ui::MainMenu *ui;
            };
            

            Nothing unusual and to me, everything seems in order.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Except that your MainMenu is a QMainWindow not a QDialog

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              B 1 Reply Last reply
              0
              • SGaistS SGaist

                Except that your MainMenu is a QMainWindow not a QDialog

                B Offline
                B Offline
                Brownies
                wrote on last edited by
                #7

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

                MainWindow.cpp

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

                MainWindow.h
                Added as a private data member of class 'MainWindow'

                private:
                   MainMenu *MainMenuWin;
                

                After that, I just went to the MainMenu UI, selected the form and changed the windowModality as can be seen below:
                http://s1.postimg.org/6se84xv5b/Help.png

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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Brownies
                    wrote on last edited by Brownies
                    #9

                    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:

                    MainWindow::~MainWindow()
                    {
                        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:

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

                    Just convert it to the following:

                    public:
                        explicit MainWindow(QWidget *parent = 0);
                        ~MainWindow()
                              {
                                   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!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        Brownies
                        wrote on last edited by Brownies
                        #11

                        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

                        1 Reply Last reply
                        0
                        • Chris KawaC Offline
                          Chris KawaC Offline
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on last edited by Chris Kawa
                          #12

                          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();
                             mm->setAttribute(Qt::WA_DeleteOnClose);
                             mm->show();
                          }
                          

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

                          B 1 Reply Last reply
                          0
                          • Chris KawaC Chris Kawa

                            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();
                               mm->setAttribute(Qt::WA_DeleteOnClose);
                               mm->show();
                            }
                            

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

                            B Offline
                            B Offline
                            Brownies
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0

                            • Login

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