Unsolved GUI blocking...
-
@JNBarchan said in GUI blocking...:
P.S.
Sorry if I have hijacked this thread. If you feel you want to move your exchange with me from here to its own thread I would quite understand.Feel free to carry on here, i might just learn something...
-
Feel free to carry on here, i might just learn something...
Guys, please respect the forum etiquette. It's not good to hijack threads :-)
Feell free to start another post for this ongoing exchange about a topic different from the original post. Thank you.
-
Can you explain to me what this is doing, as if explaining to a very small child...
wiringPiSetup(); // only call once in the application QTimer timerRelay4On = new QTimer(this); timerRelay4On->setInterval(relay4IntervalOn); timerRelay4On->setSingleShot(true); connect(timerRelay4On, SLOT(sendLow(pin))); QTimer timerRelay4Off = new QTimer(this); timerRelay4Off->setInterval(relay4IntervalOff); timerRelay4Off->setSingleShot(true); connect(timerRelay4Off, SLOT(sendHigh(pin)));
thanks.
-
@Aesgarth This code will not work.
First:QTimer *timerRelay4On = new QTimer(this); // timerRelay4On needs to be pointer else it will not even compile
Second:
connect(timerRelay4On, SLOT(sendLow(pin))); connect(timerRelay4Off, SLOT(sendHigh(pin)));
both connects are wrong: where are signal and pointer to receiver object? Check documentation for proper connect usage. The slots used require a parameter (pin) which cannot be passed from the timer (QTimer does not know anything about pins) - so, signal and slot will not match.
This code can be simplified using static http://doc.qt.io/qt-5/qtimer.html#singleShot method (no need to create an instance of QTimer).What the source code will do after the issues were fixed: after some time (defined by relay4IntervalOn) sendLow() will be called, after some time (defined by relay4IntervalOff) sendHigh() will be called.
-
To all, as previously pointed out, my code was not tested just provided as a guideline of what might be a possible approach to your requirements. Here you have the snippet of a working example for a checkable button starting the process and the ON/OFF cycle running on intervals of 3 secs ON and 1 sec OFF until the button is unchecked again when the timers are stopped. References to wiringPi library's functions are commented out since I'm not using it for this example, but for reference where they should be. No more code examples will be provided from my side. Thank you.
... MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); timerRelay1On = new QTimer(this); timerRelay1On->setInterval(3000); timerRelay1On->setSingleShot(true); connect(timerRelay1On, SIGNAL(timeout()), this, SLOT(sendLow())); timerRelay1Off = new QTimer(this); timerRelay1Off->setInterval(1000); timerRelay1Off->setSingleShot(true); connect(timerRelay1Off, SIGNAL(timeout()), this, SLOT(sendHigh())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_btnRelay1_toggled() { pin = 0; // pinMode(pin, OUTPUT); if (ui->btnRelay1->isChecked()) { // button initially checked // make the ball roll... on -> off -> on -> off -> etc sendHigh(); } else { // button unchecked // everything must stop timerRelay1Off->stop(); timerRelay1On->stop(); qDebug() << "Everything stopped!"; } } void MainWindow::sendHigh() { // digitalWrite(pin, HIGH); qDebug() << "Pin " << pin << " HIGH"; timerRelay1On->start(); qDebug() << QDateTime::currentDateTime() << ": Timer ON started"; } void MainWindow::sendLow() { // digitalWrite(pin, LOW); qDebug() << "Pin " << pin << " LOW"; timerRelay1Off->start(); qDebug() << QDateTime::currentDateTime() << ": Timer OFF started"; }
-
Thanks. I have your code from above working, using QTimer - and behaving correctly... what i'm now trying to do is repeat the operation while the button is checked but also stopped when it is unchecked.. That sounded like an ideal solution but I haven't tried it as i couldn't work out how it worked... I guess i'll have to keep trying.
as another issue, when i attempt to do wiringPiSetup() as i have above it dosen't work, until a button that has it in is pushed. can i run it when the program starts? if so how?
-
Thanks, very much appreciated. I'll give that a go.
-
-
@Pablo-J.-Rogina said in GUI blocking...:
@Aesgarth you need to call wiringPiSetup() only once in your application, maybe in main.cpp before starting Qt event loop, on in MainWindow constructor for instance. Also don't forget that it needs to be called with root privileges... see here.
Thanks, putting it in the constructor worked - I'd put it in the wrong place earlier.
Re the timer issue, having tried your example above i'm getting the error
timerRelay1On was not declared in this scope
but i've probably done something wrong. I'll keep trying.thanks again.
-
@Aesgarth said in GUI blocking...:
timerRelay1On was not declared in this scope
Well, you have to declare timerRelay1On somewhere before you can use it. Either add it as class member to MainWindow or just do
QTimer *timerRelay1On = new QTimer(this);