failed connect
-
Hello there, I'm trying from a tutorial making a small tictactoe game with Qtcreator. That's my first project with Qt.
When I run the binary, I get these two messages:Starting /home/sven/develop/Qt/build-tictactoe-Desktop_Qt_5_6_0_GCC_64bit-Debug/tictactoe... QObject::connect: No such signal TicTacToeWidget::currentPlayerChanged(TicTacToeWidget::Player) in ../tictactoe/mainwindow.cpp:17 QObject::connect: (sender name: 'gameBoard') QObject::connect: (receiver name: 'MainWindow') QObject::connect: No such signal TicTacToeWidget::gameOver(TicTacToeWidget::Player) in ../tictactoe/mainwindow.cpp:20 QObject::connect: (sender name: 'gameBoard') QObject::connect: (receiver name: 'MainWindow')
But i have both functions in the 'signals' part in the class declaration of TicTacToeWidget.
What else could be caused this errors? -
hi and welcome
One common error is that moc.exe was not run so it dont know it has the signals.
Or forgetting the Q_OBJECT macro in the class .hSo check if u have Q_OBJECT
do clean all
run qmake
rebuildif it still says it, please post some code :)
-
Q_OBJECT is set, and the build process was without warnings.
The perfidious is, that i have all source files belonging to the tutorial so i was being able to compare these to my own try. But although I compared every piece of code, I didn't found a semantic distinction.
Only the *.ui files I couldn't check, that would too wearisome for me.Here the code for the connect statements:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect( ui->actionNewGame, SIGNAL(triggered()), this, SLOT(startNewGame())); connect( ui->actionQuit, SIGNAL(triggered()), qApp, SLOT(quit())); connect( ui->gameBoard, SIGNAL(currentPlayerChanged(TicTacToeWidget::Player)) , this, SLOT(updateNameLabels())); connect( ui->gameBoard, SIGNAL(gameOver(TicTacToeWidget::Player)) , this, SLOT(handleGameOver(TicTacToeWidget::Player))); }
The first two connections will created fine.
I suppose i made a mistake within Designer Mode, but i have no clue where.
Could it be that I made a mistake at naming the sender's source? But i don't see such thing, in the Designer Mode's Object Inspector. -
@nischu said:
hi the UI is just translated into code. ( the setUI function)
so i doubt it has anything to do with it.also since you use directly connect, you are in control.
one thing i do wonder
connect( ui->gameBoard, SIGNAL(currentPlayerChanged(TicTacToeWidget::Player))
, this, SLOT(updateNameLabels()));should that not be
connect( ui->gameBoard, SIGNAL(currentPlayerChanged(TicTacToeWidget::Player))
, this, SLOT(updateNameLabels(TicTacToeWidget::Player)));anyway, you can check what connect returns to see if it fails
qDebug() << "con1:" << connect(xxx
it says TRUE when it accepts the connect.
-
@mrjj said:
should that not be
connect( ui->gameBoard, SIGNAL(currentPlayerChanged(TicTacToeWidget::Player))
, this, SLOT(updateNameLabels(TicTacToeWidget::Player)));No, it's ok. Here the handling:
void MainWindow::updateNameLabels() { QFont f = ui->player1->font(); f.setBold( ui->gameBoard->currentPlayer() == TicTacToeWidget::Player1); ui->player1->setFont(f); f.setBold( ui->gameBoard->currentPlayer() == TicTacToeWidget::Player2); ui->player2->setFont(f); }
Thanks for your hint with QDebug. But that says no news: con1,con2 == true, con3,con4 == false
-
Hi if you can zip the whole project, i could have a look?
or link to the tut.
or show the .h and .cpp where signals/slots are defined -
mrjj, Thanks for taking your time looking over my source code.
[Here are my own files][And here the files published by my book] This compiles and runs without errors.
-
Hi @nischu,
when you connect signal and slot the sign has to be exactly the same.
If you have a signal signed: currentPlayerChanged(TicTacToeWidget::Player)
you need to link with a slot signed: compatibleSlotToBeLinked(TicTacToeWidget::Player)if not, the link not occur as specified in the doc (just before "Advanced Signals and Slots Usage" not far from the end of the article doc.
-
hi as @jerome_isAviable says
it was the name space/scope naming that confused it.
"player" vs "TicTacToeWidget::player"void currentPlayerChanged( Player);
works if
void currentPlayerChanged( TicTacToeWidget::Player );here is fixed and running sample
https://www.dropbox.com/s/jvsoulsvmh3292g/tictactoe.zip?dl=0 -
Despite almost all the documentation still using SIGNAL() and SLOT(), you should really switch to the Qt5 syntax https://wiki.qt.io/New_Signal_Slot_Syntax it catches these problems at compile time and makes it easier to solve them
-
@VRonin
I complete agree that the new syntax has much better checks etc
but sadly its very unfriendly to
beginners when overloaded signals are used.connect(spinbox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), slider, &QSlider::setValue);
vs
connect(spinbox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));Its sad that compile time check has such a high price in syntax complexity and
pretty ugly to look at :) ( IMO)update:
just seen that Qt 5.7 has qOverload
so my point is not so valid :)