Qt匹配信号与自定义插槽

Qt匹配信号与自定义插槽,qt,signals-slots,qwidget,Qt,Signals Slots,Qwidget,我正在尝试使用QAction(QMenu成员条目)打开一个新窗口。准确地说:我想要actionAbout信号预定义激活以匹配main窗口自定义槽openaboutwindow——这就是我遇到的问题 我知道我可以在source main_window.cpp文件中手动使用connectQt函数,也可以在Qt Creator中单击它,但我的自定义插槽没有显示,因此我无法选择它。可能我的插槽函数声明错误(参数无效),这就是为什么QtCreator不允许我在GUIsignals&slots中选择自定义插

我正在尝试使用QAction(QMenu成员条目)打开一个新窗口。准确地说:我想要
actionAbout
信号预定义
激活
以匹配
main窗口
自定义槽
openaboutwindow
——这就是我遇到的问题

我知道我可以在source main_window.cpp文件中手动使用
connect
Qt函数,也可以在Qt Creator中单击它,但我的自定义插槽没有显示,因此我无法选择它。可能我的插槽函数声明错误(参数无效),这就是为什么QtCreator不允许我在GUI
signals&slots
中选择自定义插槽的原因。有人能告诉我应该怎么做才能让QtCreator在下拉列表中显示我的自定义插槽,以及connect函数调用应该是什么样子的吗

这是我的主窗口.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; }; #包括 #包括“关于窗口.h” 名称空间用户界面{ 类主窗口; } 类主窗口:公共QMainWindow { Q_对象 公众: 显式主窗口(QWidget*parent=0); ~main窗口(); 公众时段: void openAboutWindow(); 私人: 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(); } 主窗口::主窗口(QWidget*父窗口): QMainWindow(父级), 用户界面(新用户界面::主窗口) { 用户界面->设置用户界面(此); 连接(actionAbout,SIGNAL(activated()),此,插槽(openAboutWindow(this)); } MainWindow::~MainWindow() { 删除用户界面; } void主窗口::openAboutWindow(QWidget*父窗口) { aboutWindow=newui::aboutWindow(父项);//确保在某个地方破坏您的窗口 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’ ../Application/main_window.cpp:在构造函数“MainWindow::MainWindow(QWidget*)”中: ../Application/main_window.cpp:9:13:错误:“actionAbout”未在此范围内声明 ../Application/main_window.cpp:9:80:error:expected')在“;”之前代币 ../Application/main_window.cpp:在成员函数“void main window::openAboutWindow(QWidget*)”中: ../Application/main_window.cpp:19:44:错误:对不完整类型“struct Ui::AboutWindow”的使用无效 ../Application/about_window.h:7:11:错误:“struct Ui::AboutWindow”的正向声明 ../Application/main_window.cpp:20:15:错误:对不完整类型“struct Ui::AboutWindow”的使用无效 ../Application/about_window.h:7:11:错误:“struct Ui::AboutWindow”的正向声明 错误消息说明了一切,
QAction
在哪里定义?是否应该是
ui->actionAbout

connect(actionAbout, SIGNAL(activated()), this, SLOT(openAboutWindow(this));

openAboutWindow()
不接受任何参数,而且无论
如何,这
都是一个实例而不是一种类型。

好的,我把它改为
connect(ui->actionAbout,SIGNAL(activated()),this,SLOT(openAboutWindow(QWidget*)而且似乎很有效,谢谢。但openNewWindow方法仍然无效:../Application/main_window.cpp:19:44:错误:不完整类型“struct Ui::AboutWindow”的使用无效这意味着什么,struct Ui::AboutWindow是不完整的,如果它是类,为什么是struct?这意味着编译器没有看到类型
Ui::AboutWindow
的声明,因为“about_window.h”只有一个转发声明。好吧,现在我知道了。我不应该创建Ui::Class,而应该创建Class,因为Ui是从.Ui接口文件自动生成的内部Qt内容。谢谢。
connect(actionAbout, SIGNAL(activated()), this, SLOT(openAboutWindow(this));