首页 文章

Qt匹配信号与自定义插槽

提问于
浏览
0

我正在尝试使用QAction(QMenu成员条目)打开一个新窗口 . 确切地说:我希望 actionAbout 信号预定义 activated 匹配 MainWindow 自定义插槽 open AboutWindow - 并且's what I'已经遇到了麻烦 .

我知道我可以在源main_window.cpp文件中手动使用 connect Qt函数,或者只是在Qt Creator中单击它,但我的自定义插槽没有't show up so I cannot select it. Maybe my slot function declaration is wrong (invalid parameters) and that'为什么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’

1 回答

  • 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 是不是类型的实例 .

相关问题