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.

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.
  • Y Offline
    Y Offline
    yoavmil
    wrote on 14 Jul 2015, 19:59 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 15 Jul 2015, 06:48
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Jul 2015, 21:03 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 15 Jul 2015, 06:55
      0
      • Y yoavmil
        14 Jul 2015, 19:59

        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 15 Jul 2015, 06:48 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
        • S SGaist
          14 Jul 2015, 21:03

          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 15 Jul 2015, 06:55 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
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 15 Jul 2015, 07:47 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 15 Jul 2015, 07:56
            0
            • S SGaist
              15 Jul 2015, 07:47

              Except that your MainMenu is a QMainWindow not a QDialog

              B Offline
              B Offline
              Brownies
              wrote on 15 Jul 2015, 07:56 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 15 Jul 2015, 08:24 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 15 Jul 2015, 12:38 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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 15 Jul 2015, 13:01 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 15 Jul 2015, 16:52 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
                      • C Offline
                        C Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on 16 Jul 2015, 12:04 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 19 Jul 2015, 14:48
                        0
                        • C Chris Kawa
                          16 Jul 2015, 12:04

                          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 19 Jul 2015, 14:48 last edited by
                          #13

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

                          1 Reply Last reply
                          0

                          11/13

                          15 Jul 2015, 16:52

                          • Login

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