Qmetaobject::Connectslotsbyname: No Matching Signal

Why do I get QMetaObject::connectSlotsByName: No matching signal with a custom dialog?

In some part of your code you are using the method connectSlotsByName, if you have created a design (.ui) this usually calls it since compiling generates a file ui_somefile.h, and this file is used.

According to the docs:

void QMetaObject::connectSlotsByName(QObject *object)

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

From the above it is observed that this method will try to connect the slots that have that format, and in your case the second slot fulfills it, when trying to connect it looks for the objects and signals but in your case it does not find it since the object does not exist dlgName and generates the warning you see.

This method is created by the .ui file because through the design you can create slots by right clicking on the widget and selecting go to slot, choosing the signal and finally creating the slot.


Note:

If you are going to create your own slot, avoid using the underscores as this could cause you problems because Qt would try to connect it and if the objects do not exist it will send you several warnings.

QMetaObject::connectSlotsByName: No matching signal warning for legit code

Slots named as *"on_something** are handled in a special way by QMetaObject which uses It's reflection mechanisms to connect them to the right control and signal.

The sintax is actually on_objectName_signal. So the error you see in the console is because moc didn't find an object named "pushButton_unary" to attempt the automatic connection.

Since you are connecting your signals to slots manually just rename the slots avoiding that syntax (e.g. name the slot "onUnaryButtonRelesed").

A warning message No matching signal for when executing my application

The warning arises because within the function setupUi calls the function connectSlotsByName.

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

According to the documentation:

void QMetaObject::connectSlotsByName(QObject * object)

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

Then this function looks for objects actionUndo and actionRedo and does not find them because they are not created, a simple solution is to create them before setupUi and pass a name with setObjectName:

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

With this configuration you will no longer need to make the connections, ie you do not need to implement the next line.

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

complete code:

.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";
}

No matching signal for On_actionCapture_triggered

The Qt autoconnect mechanism tries to connect signals to slots of objects with the form of:

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

So here it tries to find an object with the name actionCapture which has a signal with the name of triggered to connect it to your slot. But there is no such a thing and it outputs that warning.

You should change the name of your slot to some other name to avoid this warning.



Related Topics



Leave a reply



Submit