QObject::connect() gives error
-
Hello everyone,
I was trying to connect a signal and a slot:connect(edit, &TextEdit::textChanged, this, &MainWindow::on_edit_textChanged);
(TextEdit is a class that inherits from QTextEdit)
But it gives this error:Signal and slot arguments are not compatible.
I've read some other topics online and I don't know how to solve this.
The slot has the same arguments, as this says, it does not go with any argument.void on_edit_textChanged();
This is the slot in the MainWindow.h file.
I also tried to put an argument in case that it was the problem but it didn't solve nothing.
Can you explain me why is it giving error and how to solve it? -
@mrjj hey, I found out that the problem was actually this:
connect(toUpper, &QAction::triggered, this, &MainWindow::toUpper);
And it says:
Func1 = void (QAction::*)(bool); Func2 = void (MainWindow::*)(QAction*);
Meaning that triggered and toUpper arguments are not identical.
So I just changed toUpper function arguments fromQAction *
tobool checked
But I have another strange thing.
The signals and slots connect correctly but in the application output there is:qt.core.qmetaobject.connectslotsbyname: QMetaObject::connectSlotsByName: No matching signal for on_edit_textChanged()
Same for some others, not all.
Can you tell me why it's happening? -
Hi
What Qt version ?It compiles fine in 5.15.2
class TextEdit : public QTextEdit { public: explicit TextEdit(QWidget* parent = nullptr) : QTextEdit(parent) { } }; ... connect(ui->textEdit, &TextEdit::textChanged, this, &MainWindow::on_edit_textChanged);
I was wondering, if you did try to delete the build folder and recompile all again ?
-
Hmm, tried Qt 6.1.3 and code from link.
Still compiles fine so try a completely clean build. -
@mrjj hey, I found out that the problem was actually this:
connect(toUpper, &QAction::triggered, this, &MainWindow::toUpper);
And it says:
Func1 = void (QAction::*)(bool); Func2 = void (MainWindow::*)(QAction*);
Meaning that triggered and toUpper arguments are not identical.
So I just changed toUpper function arguments fromQAction *
tobool checked
But I have another strange thing.
The signals and slots connect correctly but in the application output there is:qt.core.qmetaobject.connectslotsbyname: QMetaObject::connectSlotsByName: No matching signal for on_edit_textChanged()
Same for some others, not all.
Can you tell me why it's happening? -
Hi
Good you found out.The : QMetaObject::connectSlotsByName: is called inside the setupUI and is part of the auto connect feature.
Since your slot name (on_edit_textChanged) follows the auto connect naming rule it
tried to match up to a widget called "edit" and didn't find one / or it didn't have a signal to match.To avoid this, simply name your slot slightly differently.
Note: its far better to use connect statement than to rely on auto connect as it breaks so easy if you rename widgets etc.
So doing it right. Just use OnTextChange or similar naming