I’ve been pouring over Qt documentation for 5 days straight and just spent literally all night trying to solve this one signal/slot error in a prototype, trying to understand Qt. I’m able to connect the first slot/signal but the other pair isn’t working.
Rectangle {
id: page
width: 1000
height: 1000
color: "#343434"
signal readyToStartGame()
RandomMoveButton{
anchors.left: startGame.right
}
Button {
id: startGame
width: 100
height: 50
Text{
text: "new game"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
readyToStartGame();
}
}
}
...
My RandomMoveButton has a signal, and it’s the one that’s giving me problems. The signal above gives me no problems and connects to its slot just fine. Here is the QML file with the problem signal:
import GameController 1.0
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
Button {
signal clickedRandomMove()
id: randomMove
width: 100
height: 50
Text{
text: "random move"
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
onClicked: {
randomMove.clickedRandomMove()
}
}
}
Here is the code that connects the signal and slot:
void initialize(){
QQuickView view;
view.setSource(QUrl::fromLocalFile("qml/PentagoQtRapidPrototype/RandomMoveButton.qml"));
QObject* object = view.rootObject();
QQuickItem* item = qobject_cast<QQuickItem*>(object);
QObject::connect(item, SIGNAL(clickedRandomMove()), this, SLOT(randomMove()));
}
There are no errors or messages regarding this connection, but when the signal is fired (it definitely happens when I click the button), it doesn’t go into the C++ slot. It just stops. I’m not making any threads (although I have tried going that route with the same results). It’s worth noting that the initialize() is a member of a subclass. The parent class is a pure abstract base class that implements the slot. In other words, the “this” in the receiver argument in the QObject::connect call is a subclass, and randomMove() is a slot implemented in the pure abstract parent class. My problem is that randomMove() never gets called. I have spent 2 days on this one connection, reading every article on the internet I could find.
↧