September 26, 2011

dvez43 dvez43
Ant Farmer
216 posts

[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

September 26, 2011

mindomobile mindomobile
Lab Rat
10 posts

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.

 Signature 

Mobile application development | mindomobile.com

September 26, 2011

dvez43 dvez43
Ant Farmer
216 posts

I do include the dialog.h in both mainwindow.h and mainwindow.cpp files.

this is what my main window.h looks like.

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QtCore>
  6. #include "dialog.h"
  7.  
  8. namespace Ui {
  9.     class mainwindow;
  10. }
  11.  
  12. class mainwindow : public QMainWindow
  13. {
  14.     Q_OBJECT
  15.  
  16. public:
  17.     explicit mainwindow(QWidget *parent = 0);
  18.     ~mainwindow();
  19.     dialog dia;
  20.  
  21. public slots:
  22.  
  23. private:
  24.     Ui::mainwindow *ui;
  25. };
  26.  
  27. #endif // MAINWINDOW_H

Heres my mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "dialog.h"
  3. #include "ui_mainwindow.h"
  4. #include <QDebug>
  5. #include <QtCore>
  6.  
  7. mainwindow::mainwindow(QWidget *parent) :
  8.     QMainWindow(parent),
  9.     ui(new Ui::mainwindow)
  10. {
  11.     ui->setupUi(this);
  12.  
  13.     dia.exec();      // open dialog
  14. }
  15.  
  16. mainwindow::~mainwindow()
  17. {
  18.     delete ui;
  19. }
  20.  
  21. void mainwindow::opensettings()
  22. {
  23.     dia.exec();   // open dialog
  24. }

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.

September 26, 2011

mindomobile mindomobile
Lab Rat
10 posts

window.h, line 19 check the spelling of diaglog

 Signature 

Mobile application development | mindomobile.com

September 26, 2011

dvez43 dvez43
Ant Farmer
216 posts

I apologize, this is just an error in recreating the example. That is correct in my code, I am still getting the same error.

September 26, 2011

Eus Eus
Lab Rat
121 posts

  1. diaglog dia;

typo?

//Edit: Sorry, I was slow in replying :P

September 26, 2011

dvez43 dvez43
Ant Farmer
216 posts

Yes, i fixed it…. This isnt my actual code. I am just recreating an example of what I am doing. That was just a typo on my part in recreating the example. The typo is not present in my actual code.

September 26, 2011

mindomobile mindomobile
Lab Rat
10 posts

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.

 Signature 

Mobile application development | mindomobile.com

September 26, 2011

Eus Eus
Lab Rat
121 posts

Are you building your app from within QtCreator? Try cleaning up your project first, then rebuild it. If you did a typo in the class definition, it might have “remembered” the old definition.

September 26, 2011

dvez43 dvez43
Ant Farmer
216 posts

I commented out all of the instantiations. and dialog.exec() ‘s and the error when bye bye… so now I am confused!

September 26, 2011

mindomobile mindomobile
Lab Rat
10 posts
dvez43 wrote:
I commented out all of the instantiations. and dialog.exec() ‘s and the error when bye bye… so now I am confused!

Did you checked the class name for dialog? Try to comment definition in mainwindow.h file.

 Signature 

Mobile application development | mindomobile.com

September 26, 2011

dvez43 dvez43
Ant Farmer
216 posts

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.

September 26, 2011

Eus Eus
Lab Rat
121 posts

I think he meant the class definition in your dialog.h
If you built it using Qt Designer, it shouldn’t have possible errors/typos, but it wouldn’t hurt if we look at it as well you know? :P

September 26, 2011

mindomobile mindomobile
Lab Rat
10 posts

So if you created a dialog using Qt Creator and you haven’t change a single thing during wizard you should try change:

  1. dialog dia;

to

  1. Dialog dia;

in mainwindow.h file and try to recompile…

 Signature 

Mobile application development | mindomobile.com

September 26, 2011

dvez43 dvez43
Ant Farmer
216 posts

Heres my dialog.h file

  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3.  
  4. #include <QDialog>
  5.  
  6. namespace Ui {
  7.     class dialog;
  8. }
  9.  
  10. class dialog : public QDialog
  11. {
  12.     Q_OBJECT
  13.  
  14. public:
  15.     explicit dialog(QWidget *parent = 0);
  16.     ~dialog();
  17.  
  18. public slots:
  19.  
  20. private:
  21.     Ui::dialog *ui;
  22. };
  23.  
  24. #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

September 26, 2011

dvez43 dvez43
Ant Farmer
216 posts

I did end up figuring out the problem.

I defined my dialog as a QDialog *setdialog in my headerfile

and set settings as a object of settings

this->setdialog = new settings(this);

worked fine. good lord.

Page  
1

  ‹‹ Is there a way to detect when a file copy process finish?      [solved] QGraphicsItems from model? ››

You must log in to post a reply. Not a member yet? Register here!