简体   繁体   中英

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. To run eg actionSystemSettings, I've added slot to MainWindows so it looks like this:

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()

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.

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

According to the documentation for 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);

So, I think your slot should have the following signature:

void MainWindow::on_actionSystemSettings_triggered()
{
    //
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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