I’m starting using Qt for a GUI, but I have some problems with headers/libraries because I’m missing some functions.
Two of them are:
<obj_name>.setModal(true);
<obj_name>.exec();
They should work fine as in the video I’m following (at 6:30).
Because I did exactly what they did, so my clue is his version isn’t the same as mine.
I want to know which header should I include.
Here’s my code:
void MainWindow::on_actionNew_Window_triggered()
{
MyDialog mDialog;
mDialog.setModal(true);
mDialog.exec();
}
Even with:
#include <QDialog>
Still doesn’t work. It says:
C:\QtSDK\teste-build-desktop-Qt_4_8_1_for_Desktop_-MinGW_Qt_SDK__Debug..\teste\mainwindow.cpp:22: error: ‘class MyDialog’ has no member named ‘setModal’.
mydialog.h code:
#ifndef MYDIALOG_H
#define MYDIALOG_H
#include <QMainWindow>
#include <QDialog>
namespace Ui {
class MyDialog;
}
class MyDialog : public QMainWindow
{
Q_OBJECT
public:
explicit MyDialog(QWidget *parent = 0);
~MyDialog();
private:
Ui::MyDialog *ui;
};
#endif // MYDIALOG_H
It is included in mainwindow.cpp and mydialog.cpp (the header is just the class).
MyDialogis noQDialog. You created it as a “main window” which is no dialog.To hot-fix this (without recreating the dialog using QtCreator), just rewrite the inheritance in
mydialog.hfrom:
to:
In your
mydialog.cppyou find the implementation of the constructor ofMyDialogwhich calls the superclass constructor. Since we just changed the superclass, we also have to change this call from:to
You also have to fix your .ui file to morph the whole widget from a main window to a dialog. I’ll add how to do so in a few minutes (have to find it out)You don’t need to touch the .ui file.