2

执行我的应用程序时会出现一些警告消息:
QMetaObject::connectSlotsByName: No matching signal for on_actionUndo_triggered(), QMetaObject::connectSlotsByName: No matching signal for on_actionRedo_triggered()

我已经对我创建的信号和插槽实施了规则void on_objectName_signalName(signalParameters);,但我不知道为什么会出现这些消息,请注意信号和插槽工作正常。

宣言:

class Widget : public QWidget {
    Q_OBJECT

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

private:
    Ui::Widget *ui;
    QAction *actionUndo;
    QAction *actionRedo;

private slots:
    void on_actionUndo_triggered();
    void on_actionRedo_triggered();
};

定义:

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
    ui->setupUi(this);   
    QVBoxLayout *layout = new QVBoxLayout(this);
    QMenuBar *menuBar = new QMenuBar();
    QMenu *editMenu = new QMenu("&Edit");
    menuBar->addMenu(editMenu);

    this->actionUndo = new QAction("&Undo", editMenu);
    this->actionUndo->setShortcut(QKeySequence::Undo);
    QObject::connect(this->actionUndo, SIGNAL(triggered()), this, SLOT(on_actionUndo_triggered()));

    this->actionRedo = new QAction("&Redo", editMenu);
    this->actionRedo->setShortcut(QKeySequence::Redo);
    QObject::connect(this->actionRedo, SIGNAL(triggered()), this, SLOT(on_actionRedo_triggered()));

    editMenu->addAction(this->actionUndo);
    editMenu->addAction(this->actionRedo);

    this->layout()->setMenuBar(menuBar);
}

Widget::~Widget() {
    delete ui;
}


void Widget::on_actionUndo_triggered() {

}

void Widget::on_actionRedo_triggered() {

}
4

1 回答 1

2

出现警告是因为在函数 setupUi 中调用了函数connectSlotsByName

void setupUi(QWidget *Widget)
{
    [...]
    QMetaObject::connectSlotsByName(Widget);
}

根据文档:

void QMetaObject::connectSlotsByName(QObject * object)

递归搜索给定对象的所有子对象,并将来自它们的匹配信号连接到遵循以下形式的对象槽:

void on_<object name>_<signal name>(<signal parameters>);

然后这个函数查找对象actionUndo并且actionRedo因为它们没有被创建而没有找到它们,一个简单的解决方案是在 setupUi 之前创建它们并传递一个名称setObjectName

actionUndo = new QAction("&Undo", this);
actionUndo->setObjectName("actionUndo");
actionRedo = new QAction("&Redo", this);
actionRedo->setObjectName("actionRedo");
ui->setupUi(this);

使用此配置,您将不再需要建立连接,即您不需要实现下一行。

QObject::connect(this->actionUndo, SIGNAL(triggered()), this, SLOT(on_actionUndo_triggered()));
QObject::connect(this->actionRedo, SIGNAL(triggered()), this, SLOT(on_actionRedo_triggered()));

完整代码:

。H

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_actionUndo_triggered();
    void on_actionRedo_triggered();

private:
    Ui::Widget *ui;
    QAction *actionUndo;
    QAction *actionRedo;
};

.cpp

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    actionUndo = new QAction("&Undo", this);
    actionUndo->setObjectName("actionUndo");
    actionUndo->setShortcut(QKeySequence::Undo);

    actionRedo = new QAction("&Redo", this);
    actionRedo->setObjectName("actionRedo");
    actionRedo->setShortcut(QKeySequence::Redo);

    ui->setupUi(this);

    setLayout(new QVBoxLayout);

    QMenuBar *menuBar = new QMenuBar(this);
    QMenu *editMenu = new QMenu("&Edit");

    menuBar->addMenu(editMenu);
    editMenu->addAction(actionUndo);
    editMenu->addAction(actionRedo);

    layout()->setMenuBar(menuBar);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_actionUndo_triggered()
{
    qDebug()<<"undo";
}

void Widget::on_actionRedo_triggered()
{
    qDebug()<<"redo";
}
于 2017-06-04T17:54:43.297 回答