简体   繁体   English

没有与QAction匹配的信号,没有“转到插槽”菜单条目

[英]No matching signal for QAction, no “go to slot” menu entry

我已经自动生成了QMainWindow(MainWindows),其中包含QMenuBar(menuBar)和QMenu(menuWork)。

I have problem with actually running QActions created with QtCreator. 我在实际运行使用QtCreator创建的QAction时遇到问题。 To run eg actionSystemSettings, I've added slot to MainWindows so it looks like this: 为了运行例如actionSystemSettings,我向MainWindows添加了插槽,因此它看起来像这样:

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_menuWork_actionSystemSettings();

private:
    Ui::MainWindow *ui;
};

And this: 和这个:

void MainWindow::on_menuWork_actionSystemSettings() {
    qDebug() << "Yay!";
}

It prompts: 它提示:

QMetaObject::connectSlotsByName: No matching signal for on_menuWork_actionSystemSettings() QMetaObject :: connectSlotsByName:没有匹配信号on_menuWork_actionSystemSettings()

I guess it's some dumb mistake and I just forgot about something but reading documentation gives me nothing. 我猜这是一个愚蠢的错误,我只是忘记了一些东西,但是阅读文档却没有任何帮助。 I have no "go to slot" menu entry which should auto-create some template... at least Visual Studio for C# did that. 我没有“去插槽”菜单项,它应该自动创建一些模板……至少Visual Studio for C#做到了。

When you're defining slots the correct way is: 在定义插槽时,正确的方法是:

on_<widget_name>_<signal>

for instance if you have to name your slot 例如,如果您必须命名广告位

private slots:
    on_actionSystemSettings_triggered();

See QtAutoConnect 请参阅QtAutoConnect

According to the documentation for QMetaObject::connectSlotsByName() : 根据QMetaObject::connectSlotsByName()的文档:

Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form: 递归搜索给定对象的所有子对象,并将匹配的信号从它们连接到遵循以下形式的对象插槽:

void on_object-name_signal-name(signal-parameters); void on_object-name_signal-name(signal-parameters);

So, I think your slot should have the following signature: 因此,我认为您的广告位应具有以下签名:

void MainWindow::on_actionSystemSettings_triggered()
{
    //
}

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

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