简体   繁体   English

在Qt 4.8.6中打开模式窗口

[英]Opening a modal window in qt 4.8.6

I'm working on a simple program for installing wow addons and need some help with having the main window open a preferences window. 我正在开发一个用于安装wow插件的简单程序,需要一些帮助来打开主窗口。 I'm rather new to Qt and struggling a bit with the documentation. 我对Qt并不陌生,并且在文档方面有些挣扎。 I did some looking around on stackoverflow, but still am a bit stuck on getting things in the right area. 我在stackoverflow上做了一些环顾,但是在将内容放到正确的区域仍然有些卡住。

edit: added in error message. 编辑:在错误消息中添加。

mainwindow.h 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 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 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 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 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;
}

Error Received error: no'void MainWindow::on_Preferences_triggered()' member function declared in class 'MainWindow' 错误收到的错误:在类'MainWindow'中声明的'void MainWindow :: on_Preferences_triggered()'成员函数

I put the mainwindow.ui file on pastebin as it was to jumbled up here because of the tooltips added. 我将mainwindow.ui文件放在pastebin上,因为由于添加了工具提示,所以在这里将其弄乱了。

http://pastebin.com/ZXrDxZXz http://pastebin.com/ZXrDxZXz

In general, if you want your dialog to be modal, use QDialog::exec(). 通常,如果希望对话框是模式对话框,请使用QDialog :: exec()。 If you want a modeless dialog, use QDialog::show(). 如果需要无模式对话框,请使用QDialog :: show()。 You can use show() to display a modal dialog too by setting the modal property to true. 您也可以通过将modal属性设置为true来使用show()显示模式对话框。

I've fixed up your code so that it compiles and runs. 我已经修复了您的代码,使其可以编译和运行。 First, the compiler error you're getting is exactly what it says: you've implemented a function MainWindow::on_Preferences_triggered() that you haven't declared in your class definition. 首先,您得到的编译器错误恰恰说明了该错误:您实现了在类定义中未声明的MainWindow :: on_Preferences_triggered()函数。 But there are some other issues. 但是还有其他一些问题。 I'll start with the main.cpp file: 我将从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();
}*/

I commented out the on_Preferences_triggered function, because it should be moved to the mainwindow.cpp file, where the rest of the MainWindow class is implemented. 我注释掉了on_Preferences_triggered函数,因为它应该移到mainwindow.cpp文件中,在其中实现MainWindow类的其余部分。 I also resized the MainWindow instance you created in the main() function. 我还调整了在main()函数中创建的MainWindow实例的大小。

On to mainwindow.h: 转到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

First, in both your Preferences and MainWindow definitions, you have instances of Preferences and MainWindow, respectively. 首先,在Preferences和MainWindow定义中,分别具有Preferences和MainWindow的实例。 I'm not sure why you did that, but I don't think it's what you want. 我不确定您为什么这样做,但是我不认为这就是您想要的。 MainWindow needs a Preferences instance (which I added), but I don't think either class needs an instance of itself. MainWindow需要一个Preferences实例(已添加),但是我不认为任何一个类都需要其自身的实例。

Next, I added your on_Preferences_triggered() function as a slot. 接下来,我将您的on_Preferences_triggered()函数添加为插槽。 You'll need this. 您需要这个。 Finally, I added a setupMenus() function, which sets up the menubar, though you could do this in your constructor if you wanted to. 最后,我添加了setupMenus()函数,该函数用于设置菜单栏,尽管您可以根据需要在构造函数中执行此操作。

mainwindow.cpp: 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);
}

According to the documentation, a QMainWindow without a central widget isn't supported... so I added a QLabel as a placeholder. 根据文档,不支持没有中央窗口小部件的QMainWindow ...因此我添加了QLabel作为占位符。 The rest of the constructor is pretty self-explanatory... allocate your Preferences pointer, call setupMenus. 构造函数的其余部分很容易解释。分配您的Preferences指针,调用setupMenus。

The setupMenus function does the important work. setupMenus函数可以完成重要的工作。 The menubar is created the first time you call menuBar(), so I called it and added a menu. 菜单栏是在您第一次调用menuBar()时创建的,因此我对其进行了调用并添加了一个菜单。 Then I created a QAction which will be used to show your preferences dialog. 然后,我创建了一个QAction,它将用于显示您的首选项对话框。 To do this, you NEED to connect the action's triggered() signal to your on_Preferences_triggered() slot. 为此,您需要将操作的trigger()信号连接到on_Preferences_triggered()插槽。

The on_Preferences_triggered() slot is pretty much what you had, except it doesn't create a Preferences instance (it could though... you could keep what you had). on_Preferences_triggered()插槽几乎是您所拥有的,除了它不会创建Preferences实例(尽管可以...您可以保留所拥有的)。 An important point here: I used pref->show() to pop up the dialog, then made it modal with pref->setModal(true). 这里的重点是:我使用了pref-> show()来弹出对话框,然后使用pref-> setModal(true)使其成为模态。 You could instead have used pref->exec(). 您可以改用pref-> exec()。 But read Kuba Ober's comment below. 但是请阅读下面的库巴·奥伯(Kuba Ober)的评论。

Then, in preferences.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:
  void setupDialog();

  /* Preferences doesn't need an instance of Preferences */
  //Ui::Preferences *ui;
};

#endif // PREFERENCES_H

I added a setupDialog() function, and again, got rid of the instance of Preferences, because I think you don't need it. 我添加了setupDialog()函数,并且再次摆脱了Preferences的实例,因为我认为您不需要它。

And preferences.cpp: 和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"));
}

I just created a dummy layout with a dummy QLabel in the setupDialog function. 我刚刚在setupDialog函数中使用虚拟QLabel创建了虚拟布局。

Also... I commented out your #include "ui_preferences.h" and "ui_mainwindow.h" statements, because you didn't include this code. 另外...我注释掉了您的#include“ ui_preferences.h”和“ ui_mainwindow.h”语句,因为您没有包含此代码。 Not sure what you'd done in those headers, or whether they were important. 不知道您在这些标头中做了什么,或者它们是否重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM