Can't execute new UI or setModel.
-
@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.
-
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.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.pngThanks for your help and sorry for being a bother :P
I will definetly use @yoavmil 's advice for hiding a UI.
Thanks! -
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:
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! -
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
-
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.
-
@Chris-Kawa Oh wow. I didn't even know about that! Thanks! :D