我正在尝试使用QAction(QMenu成员条目)打开一个新窗口。确切地说:我希望actionAbout
信号预定义activated
与MainWindow
自定义广告位open AboutWindow
匹配 - 这就是我遇到的麻烦。
我知道我可以在源main_window.cpp文件中手动使用connect
Qt功能,或者只是在Qt Creator中单击它,但我的自定义插槽没有显示,所以我无法选择它。也许我的插槽函数声明是错误的(无效参数),这就是为什么QtCreator不允许我在GUI signals & slots
中选择我的自定义插槽的原因。任何人都可以指出我应该怎样做才能让QtCreator在下拉列表中显示我的自定义插槽以及连接函数调用应该如何?
这是我的main_window.h文件内容:
#include #include "about_window.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void openAboutWindow(); private: Ui::MainWindow *ui; Ui::AboutWindow *aboutWindow; };
这是main_window.cpp:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(actionAbout, SIGNAL(activated()), this, SLOT(openAboutWindow(this)); } MainWindow::~MainWindow() { delete ui; } void MainWindow::openAboutWindow(QWidget *parent) { aboutWindow = new Ui::AboutWindow(parent); // Be sure to destroy you window somewhere aboutWindow->show(); }
编译器对构造函数和openAbutWindow大声喊叫:
../Application/main_window.cpp: In constructor ‘MainWindow::MainWindow(QWidget*)’: ../Application/main_window.cpp:9:13: error: ‘actionAbout’ was not declared in this scope ../Application/main_window.cpp:9:80: error: expected ‘)’ before ‘;’ token ../Application/main_window.cpp: In member function ‘void MainWindow::openAboutWindow(QWidget*)’: ../Application/main_window.cpp:19:44: error: invalid use of incomplete type ‘struct Ui::AboutWindow’ ../Application/about_window.h:7:11: error: forward declaration of ‘struct Ui::AboutWindow’ ../Application/main_window.cpp:20:15: error: invalid use of incomplete type ‘struct Ui::AboutWindow’ ../Application/about_window.h:7:11: error: forward declaration of ‘struct Ui::AboutWindow’
答案 0 :(得分:1)
../Application/main_window.cpp:9:13: error: ‘actionAbout’ was not declared in this scope
错误消息说明了所有内容,QAction
定义在哪里?它应该是ui->actionAbout
吗?
connect(actionAbout, SIGNAL(activated()), this, SLOT(openAboutWindow(this));
openAboutWindow()
不接受任何参数,无论this
是不是类型的实例。