Slot Mechanism
-
I have an slot mechanism.If I continuously click pushButtonx I get the (different) values on pushButtony .What I want to do is click the button once and get different values continuously.How to do this? Thanks in advance.
connect(pushButtonx, &QPushButton::pressed, this,[=]() { QString textmessage = QString::number(ethernetThread->counter); pushButtony->setText(textmessage); });
-
@ELIF said in Slot Mechanism:
What I want to do is click the button once and get different values continuously
by starting a QTimer on your button push and connecting to the timeout signal
https://doc.qt.io/qt-5/qtimer.html -
But this
connect(pushButtonx, &QPushButton::pressed, this,= {
QString textmessage = QString::number(ethernetThread->counter);
pushButtony->setText(textmessage);
});
construction does not have signal name or a slot name.I have not a timeout() signal or update() slot.// QTimer *timer1 = new QTimer(this);
// connect(timer1, SIGNAL(timeout()), this, SLOT(update()));
// timer1->start(1000); -
@ELIF so you like lambdas, so I but a lambda in your lambda.
connect(pushButtonx, &QPushButton::pressed, this,[=]()->void { QTimer *neverEndingUpdateTimer(new QTimer()); connect(neverEndingUpdateTimer, QTimer::timeout, this, [=]()->void{ QString textmessage = QString::number(ethernetThread->counter); pushButtony->setText(textmessage); }); neverEndingUpdateTimer->start(20); });
-
Thank you very much I appreciate it :)
QTimer *neverEndingUpdateTimer(new QTimer()); connect(pushButtonx, &QPushButton::pressed, this,[=]()->void { connect(neverEndingUpdateTimer, &QTimer::timeout , this, [=]()->void{ QString textmessage = QString::number(ethernetThread->counter); pushButtony->setText(textmessage); } ); neverEndingUpdateTimer->start(20); });
I write &QTimer::timeout instead of QTimer::timeout ,then it works.
-
@ELIF Typo an my part, but nice spotting :D
this may in general work, but its just a proof of concept, the QTimer isntance is leaked, there's no way to stop the timer once its running and you will get multiple timer running, wenn you click multiple times on the button
and, the naming of this:
ethernetThread->counter
lets me think you're accessing variables across threads without safeguards -
@J-Hilk said in Slot Mechanism:
this may in general work, but its just a proof of concept, the QTimer isntance is leaked, there's no way to stop the timer once its running and you will get multiple timer running, wenn you click multiple times on the button
and, the naming of this: ethernetThread->counter lets me think you're accessing variables across threads without safeguardsActually I get data's from ethernet with using zmq. The naming of ethernetThread->counter is : ethernetThread is a EthernetThread class object, counter is a member of this class.I have server code and client code EthernetThread class is the client code.Purpose of this study is design a radar screen with QT .
Hopefully I will achieve it.Thank you :)