failed connect
-
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 :)