[SOLVED] Creating Instance of dialog error
Page |
1 |
I am having an issue instantiating a dialog box… I open the dialog box in two different functions within my mainwindow file.
First I tried to create an instance of the dialog in the mainwindow header file as public so I could use it multiple times throughout the mainwindow file. This flags an error saying “‘dialog name’ does not name a type”.
Next I attempting to instantiate the dialog box globally at the top of the mainwindow.cpp (which is proper coding practice, I understand that).
Then I am able to compile, yet the program crashes immediately and the debugger says “QWidget: Must construct a QApplication before a QPaintDevice”. This makes sense to me since my main program loop is trying to instantiate the class globally and my main program loop is getting confused…
i am instantiating the class by simply:
dialog dia;
where dialog is the class name. This is how I did it in both situations in the header file as public, as well as the cpp file. Is there something I am missing?
16 replies
This flags an error saying “‘dialog name’ does not name a type”.
This definitely means that your dialog is not declared in the scope. Do you include the dialog .h file in your mainwindow .cpp/.h file? Try to locate the line of code where this error is being thrown and check if you have proper headers included.
I do include the dialog.h in both mainwindow.h and mainwindow.cpp files.
this is what my main window.h looks like.
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QtCore>
- #include "dialog.h"
- namespace Ui {
- class mainwindow;
- }
- {
- Q_OBJECT
- public:
- ~mainwindow();
- dialog dia;
- public slots:
- private:
- Ui::mainwindow *ui;
- };
- #endif // MAINWINDOW_H
Heres my mainwindow.cpp
- #include "mainwindow.h"
- #include "dialog.h"
- #include "ui_mainwindow.h"
- #include <QDebug>
- #include <QtCore>
- ui(new Ui::mainwindow)
- {
- ui->setupUi(this);
- dia.exec(); // open dialog
- }
- mainwindow::~mainwindow()
- {
- delete ui;
- }
- void mainwindow::opensettings()
- {
- dia.exec(); // open dialog
- }
I do have a button that opens the dialog box so the connect statement is also in there but not included in the example. Hope this gives you a better idea of whats going on.
I tried to create test app from your example and everything works as you expected.
So my only suggestions could be to check whether or not your class is called “dialog” or “Dialog” (case sensitive).
You also don’t need to include dialog.h twice, so you can remove it from mainwindow.cpp file (optional).
As I mentioned earlier, try to locate the line where there error pops up. Check the line and couple lines above you might find some bug there.
thats what I did I believe. As in definition do you mean instantiation? (thats kinda what I use for the terminology) :P
I commented out the definitions/instantiations in the mainwindow.h and the .exec() in mainwindow.cpp and the error disappears and the program compiles correctly.
This dialog box dialog has its own class file as well and also a .ui file since I am using Qt designer.
Heres my dialog.h file
- #ifndef DIALOG_H
- #define DIALOG_H
- #include <QDialog>
- namespace Ui {
- class dialog;
- }
- {
- Q_OBJECT
- public:
- ~dialog();
- public slots:
- private:
- Ui::dialog *ui;
- };
- #endif // DIALOG_H
EDIT:
My actual dialog that I did create is named “settings”, its to set settings on the serial port being used in my application. I use this dialog box twice within the mainwindow since if the user resets the settings then the object has to be destroyed, and then recreated. I am trying to make my class instantiation global throughout mainwindow.cpp. And I am failing epically :P
so instead of dialog everywhere, you would see settings… so in my header file for mainwindow.cpp i have settings diasettings; as my instantiation
You must log in to post a reply. Not a member yet? Register here!
