Unsolved GUI blocking...
-
@jsulm said in GUI blocking...:
because SLOT() is a macro and macros are handled by preprocessor before compiling and not at runtime
Yes I am well aware.
Take a look at SLOT macro implementation to see what its output will be or use -E parameter for GCC to tell it to stop after preprocessing step to see what preprocessor did with SLOT().
As I said: "I don't have/use C++" [for Qt], I didn't even know for sure that it's a macro rather than, say, in-line function, I only code from Python/PyQt, which handles it quite differently. None of your comments about syntax, compilation etc. apply to Python/PyQt. I'm requesting whether you might be kind enough to paste the C++
SLOT
macro code precisely so I can have a look at it to understand. -
#define QLOCATION "\0" __FILE__ ":" QT_STRINGIFY(__LINE__) #define METHOD(a) qFlagLocation("0"#a QLOCATION) #define SLOT(a) qFlagLocation("1"#a QLOCATION) #define SIGNAL(a) qFlagLocation("2"#a QLOCATION)
However the process is rather complicated.
qFlagLocation
here is a function, but that's beyond the point as it's not really needed for the resolution. The macro converts the method's signature to a string, which string is then used by the Qt meta-object system to retrieve the exact function address/signal index by means of the data generated by the meta-object compiler (moc). Then Qt can call the needed method based on that string with argument type checking and such. The arguments for the slots are packed through a special utility class (e.g. seeQ_ARG
macro in the docs andQGenericArgument
). -
@kshegunov
[You don't show the definition of theSLOT()
macro too?]
Edit: Yeah, sorry, I've added it to the post ~kshegunovAh, that is very interesting indeed, thank you.
Note that it looks quite different from what I am writing in my code from Python/PyQt, e.g.:
self.btnDelete.clicked.connect(self.deletePayment)
Note how the slot-handling function is written as
func
notfunc()
(i.e. function reference not call), which is much more what I expected. -
@JNBarchan said in GUI blocking...:
i.e. function reference not call
In C++ that would not be a call, but rather the signature (the declaration). It's needed to know by the meta-object system so the magic of finding the correct overloads and the correct arguments, and name to be found in the generated by
moc
data. The resolution to a pointer to the function is done in runtime. In Qt 5 however, a pointer-to-member should be passed (thus resolution to a an address is at compile time) and there is some very different template magic at work in that case. -
@kshegunov said in GUI blocking...:
In Qt 5 however, a pointer-to-member should be passed (thus resolution to a an address is at compile time) and there is some very different template magic at work in that case.
Yes indeed, and this seems to be what the inherited Python/PyQt code I have uses (and that used to work in that code in Qt4/PyQt4 too).
It's clearly a lot more efficient to pass a direct function address at "compile" time than to look up strings of names at runtime.
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. -
@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);