[英]Opening a modal window in qt 4.8.6
我正在开发一个用于安装wow插件的简单程序,需要一些帮助来打开主窗口。 我对Qt并不陌生,并且在文档方面有些挣扎。 我在stackoverflow上做了一些环顾,但是在将内容放到正确的区域仍然有些卡住。
编辑:在错误消息中添加。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
preferences.h
#ifndef PREFERENCES_H
#define PREFERENCES_H
#include <QDialog>
namespace Ui {
class Preferences;
}
class Preferences : public QDialog
{
Q_OBJECT
public:
explicit Preferences(QWidget *parent = 0);
~Preferences();
private:
Ui::Preferences *ui;
};
#endif // PREFERENCES_H
main.cpp中
#include "mainwindow.h"
#include "preferences.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
void MainWindow::on_Preferences_triggered()
{
Preferences = new PreferencesDialog(this);
Preferences->show();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "preferences.h"
#include <QtGui/QDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
preferences.cpp
#include "preferences.h"
#include "ui_preferences.h"
Preferences::Preferences(QWidget *parent) :
QDialog(parent),
ui(new Ui::Preferences)
{
ui->setupUi(this);
}
Preferences::~Preferences()
{
delete ui;
}
错误收到的错误:在类'MainWindow'中声明的'void MainWindow :: on_Preferences_triggered()'成员函数
我将mainwindow.ui文件放在pastebin上,因为由于添加了工具提示,所以在这里将其弄乱了。
通常,如果希望对话框是模式对话框,请使用QDialog :: exec()。 如果需要无模式对话框,请使用QDialog :: show()。 您也可以通过将modal属性设置为true来使用show()显示模式对话框。
我已经修复了您的代码,使其可以编译和运行。 首先,您得到的编译器错误恰恰说明了该错误:您实现了在类定义中未声明的MainWindow :: on_Preferences_triggered()函数。 但是还有其他一些问题。 我将从main.cpp文件开始:
#include "mainwindow.h"
#include "preferences.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(200,200);
w.show();
return a.exec();
}
/* Move this to mainwindow.cpp
void MainWindow::on_Preferences_triggered()
{
Preferences = new PreferencesDialog(this);
Preferences->show();
}*/
我注释掉了on_Preferences_triggered函数,因为它应该移到mainwindow.cpp文件中,在其中实现MainWindow类的其余部分。 我还调整了在main()函数中创建的MainWindow实例的大小。
转到mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "preferences.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
/* add a function to set up a menubar/menu (you could do this in the constructor instead) */
void setupMenus();
/* Your main window doesn't need an instance of Ui::MainWindow, it IS an instance*/
//Ui::MainWindow *ui;
/* It needs a Preferences instance, though */
Preferences *pref;
private slots:
void on_Preferences_triggered();
};
#endif // MAINWINDOW_H
首先,在Preferences和MainWindow定义中,分别具有Preferences和MainWindow的实例。 我不确定您为什么这样做,但是我不认为这就是您想要的。 MainWindow需要一个Preferences实例(已添加),但是我不认为任何一个类都需要其自身的实例。
接下来,我将您的on_Preferences_triggered()函数添加为插槽。 您需要这个。 最后,我添加了setupMenus()函数,该函数用于设置菜单栏,尽管您可以根据需要在构造函数中执行此操作。
mainwindow.cpp:
#include "mainwindow.h"
//#include "ui_mainwindow.h"
/* Move this to mainwindow.h */
//#include "preferences.h"
#include <QtGui/QDialog>
#include <QMenuBar>
#include <QAction>
#include <QMenu>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
//ui(new Ui::MainWindow)
{
//ui->setupUi(this);
/* initialize your preferences window */
pref = new Preferences(this);
/* add a menubar with a menu that will allow you to open the preferences windwow */
setupMenus();
/* You need a central widget: */
setCentralWidget(new QLabel("Central widget", this));
}
MainWindow::~MainWindow()
{
//delete ui;
/* You can explicitly delete the Preferences pointer, but Qt will take care of it, since its parent is this */
}
void MainWindow::setupMenus()
{
QMenu *menu = menuBar()->addMenu(tr("&Menu"));
QAction *prefAction = new QAction("Show preferences", this);
/* Crucial: connect the action's triggered signal to your on_Preferences_triggered slot */
connect(prefAction, SIGNAL(triggered()), this, SLOT(on_Preferences_triggered()));
menu->addAction(prefAction);
}
void MainWindow::on_Preferences_triggered()
{
/*Preferences = new PreferencesDialog(this);
Preferences->show();*/
pref->show();
pref->setModal(true);
}
根据文档,不支持没有中央窗口小部件的QMainWindow ...因此我添加了QLabel作为占位符。 构造函数的其余部分很容易解释。分配您的Preferences指针,调用setupMenus。
setupMenus函数可以完成重要的工作。 菜单栏是在您第一次调用menuBar()时创建的,因此我对其进行了调用并添加了一个菜单。 然后,我创建了一个QAction,它将用于显示您的首选项对话框。 为此,您需要将操作的trigger()信号连接到on_Preferences_triggered()插槽。
on_Preferences_triggered()插槽几乎是您所拥有的,除了它不会创建Preferences实例(尽管可以...您可以保留所拥有的)。 这里的重点是:我使用了pref-> show()来弹出对话框,然后使用pref-> setModal(true)使其成为模态。 您可以改用pref-> exec()。 但是请阅读下面的库巴·奥伯(Kuba Ober)的评论。
然后,在preferences.h中:
#ifndef PREFERENCES_H
#define PREFERENCES_H
#include <QDialog>
namespace Ui {
class Preferences;
}
class Preferences : public QDialog
{
Q_OBJECT
public:
explicit Preferences(QWidget *parent = 0);
~Preferences();
private:
void setupDialog();
/* Preferences doesn't need an instance of Preferences */
//Ui::Preferences *ui;
};
#endif // PREFERENCES_H
我添加了setupDialog()函数,并且再次摆脱了Preferences的实例,因为我认为您不需要它。
和preferences.cpp:
#include "preferences.h"
//#include "ui_preferences.h"
#include <QVBoxLayout>
#include <QLabel>
Preferences::Preferences(QWidget *parent) :
QDialog(parent)
//ui(new Ui::Preferences)
{
//ui->setupUi(this);
setupDialog();
}
Preferences::~Preferences()
{
//delete ui;
}
void Preferences::setupDialog()
{
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("Preferences Dialog"));
}
我刚刚在setupDialog函数中使用虚拟QLabel创建了虚拟布局。
另外...我注释掉了您的#include“ ui_preferences.h”和“ ui_mainwindow.h”语句,因为您没有包含此代码。 不知道您在这些标头中做了什么,或者它们是否重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.