I have a switch case with condition values from 0-8. Upon each value, I give it an x and y coordinate of a 3x3 array. Then the code operates logic on the 3x3 array to get the results.
I also have 9 buttons in my Qt Designer ui (arranged in a 3x3 matrix). I want each click of the nine buttons to get a corresponding integer value which will then pass through the switch case.
switch (square_number)
{
case 1:
x = 0;
y = 0;
break;
case 2:
x = 0;
y = 1;
break;
case 3:
x = 0;
y = 2;
break;
.
.
.
//and so on...
default:
break;
}
I want the square_number to get the integer value corresponding to the push-button clicked.
QSignalMapper seems obsolete or weird at the very least. And I am not that familiar with lambda expressions. Is there a simple way of doing what I want to do?
You can try this approach and use dynamic property:
static constexpr auto IndexPropertyName = "index";
void MainWidnow::SetupPB(QPushButton* b, QPoint index)
{
connect(b, &QPushButton::clicked, this, &MainWidnow::onSomeButtoPressed);
p->setProperty(IndexPropertyName, index);
}
void MainWidnow::onSomeButtoPressed(bool checked)
{
auto index = sender()->property(IndexPropertyName).toPoint();
doSomethingOn(index);
}
bool QObject::setProperty(const char *name, const QVariant &value)
Sets the value of the object's name property to value.
If the property is defined in the class using Q_PROPERTY then true is returned on success and false otherwise. If the property is not defined using Q_PROPERTY, and therefore not listed in the meta-object, it is added as a dynamic property and false is returned.