簡體   English   中英

在Qt 4.8.6中打開模式窗口

[英]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上,因為由於添加了工具提示,所以在這里將其弄亂了。

http://pastebin.com/ZXrDxZXz

通常,如果希望對話框是模式對話框,請使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM