Does a loop continue running even if Signals and slot is not complete?
-
@GCDX
Unrelated, but you'll (usually) do yourself a favor using the new syntax https://wiki.qt.io/New_Signal_Slot_Syntax: :you get type checks at compile time.
You can also hit F2 on your sig/slot methods and navigate direct instead of hunt.I don't have experience using / abusing / this style, I'm pretty sure it's wrong for the old sytax, pretty sure you need to specify a receiver object instance.
connect(object,SIGNAL(function1()),SLOT(function2());// nearly certain this is invalid as you have no receiver object set but use SLOT
can become:
connect(object, &ObjectT::function1, whateverObjectThisWasMeantToBe, &OtherOrSameObjectT::function2); //or connect(object, &ObjectT::function1, function2); //or even a lambda expression!
You should also see this: http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads
@GCDX I just tried this code:
connect(this, SIGNAL(somethingHappend()), this, SLOT(doSomething())); for (int i = 0; i < 2; ++i) { qDebug() << i; emit somethingHappend(); } void MainWindow::doSomething() { qDebug() << "do something"; }
Output was as expected:
0
do something
1
do something -
@GCDX I just tried this code:
connect(this, SIGNAL(somethingHappend()), this, SLOT(doSomething())); for (int i = 0; i < 2; ++i) { qDebug() << i; emit somethingHappend(); } void MainWindow::doSomething() { qDebug() << "do something"; }
Output was as expected:
0
do something
1
do something -
@GCDX
Unrelated, but you'll (usually) do yourself a favor using the new syntax https://wiki.qt.io/New_Signal_Slot_Syntax: :you get type checks at compile time.
You can also hit F2 on your sig/slot methods and navigate direct instead of hunt.I don't have experience using / abusing / this style, I'm pretty sure it's wrong for the old sytax, pretty sure you need to specify a receiver object instance.
connect(object,SIGNAL(function1()),SLOT(function2());// nearly certain this is invalid as you have no receiver object set but use SLOT
can become:
connect(object, &ObjectT::function1, whateverObjectThisWasMeantToBe, &OtherOrSameObjectT::function2); //or connect(object, &ObjectT::function1, function2); //or even a lambda expression!
You should also see this: http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads
@6thC
take a look at this connect overload:
http://doc.qt.io/qt-5/qobject.html#connect-2you can omit the receiver object, connect falls than back to
this
as the receiver. Not consistent and OCD-triggering, but its a valid connect statement. -
@GCDX I just tried this code:
connect(this, SIGNAL(somethingHappend()), this, SLOT(doSomething())); for (int i = 0; i < 2; ++i) { qDebug() << i; emit somethingHappend(); } void MainWindow::doSomething() { qDebug() << "do something"; }
Output was as expected:
0
do something
1
do something@jsulm hmmm yup thanks i found my error but i don't know how to fix it.
void MainWindow::on_pushButton_clicked()
{
QString modules=stringpath;
QUrl url2(modules);
innerPage->setUrl(url2);//innerpage is a QWebview
connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
}void MainWindow::parse(bool)
{
QWebFrame *frameInner = innerPage->page()->mainFrame();
QWebElement doc = frameInner->documentElement();
QString json=doc.toPlainText();
//parsing code
print("parsing executed");
}i realised i put on_pushButton_clicked in a loop as such:
int i=0;
While(i!=2){
i++;
print(i)
on_pushbutton_clicked();
}
outcome:
1
2
parsing executed.i want parsing so be executed in every loop however i cant seem to be able to solve it?
-
@jsulm hmmm yup thanks i found my error but i don't know how to fix it.
void MainWindow::on_pushButton_clicked()
{
QString modules=stringpath;
QUrl url2(modules);
innerPage->setUrl(url2);//innerpage is a QWebview
connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
}void MainWindow::parse(bool)
{
QWebFrame *frameInner = innerPage->page()->mainFrame();
QWebElement doc = frameInner->documentElement();
QString json=doc.toPlainText();
//parsing code
print("parsing executed");
}i realised i put on_pushButton_clicked in a loop as such:
int i=0;
While(i!=2){
i++;
print(i)
on_pushbutton_clicked();
}
outcome:
1
2
parsing executed.i want parsing so be executed in every loop however i cant seem to be able to solve it?
@GCDX said in Does a loop continue running even if Signals and slot is not complete?:
ok first of all remove that line from theon_pushButton_clicked
and place it in your constructor of MainWindowconnect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
otherwise you create additional connections each time you call on_pushButton_clicked.
-> 2nd time on_pushButton_clicked is called -> parse is called twice
-> 3ed timeon_pushButton_clicked is called -> parse is called 3 times
etc.2nd, make a container, QList or QVector for example, that contains all of your targeted urls, create a member variable cnt to help you navigate through the container.
in on pushbutton clicked you set cnt to 0 and start loading the first url.
on load finished, you increase cnt by 1, check if its still smaler that your url list. If it is, load next one, if not, your're done.
-
@jsulm hmmm yup thanks i found my error but i don't know how to fix it.
void MainWindow::on_pushButton_clicked()
{
QString modules=stringpath;
QUrl url2(modules);
innerPage->setUrl(url2);//innerpage is a QWebview
connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
}void MainWindow::parse(bool)
{
QWebFrame *frameInner = innerPage->page()->mainFrame();
QWebElement doc = frameInner->documentElement();
QString json=doc.toPlainText();
//parsing code
print("parsing executed");
}i realised i put on_pushButton_clicked in a loop as such:
int i=0;
While(i!=2){
i++;
print(i)
on_pushbutton_clicked();
}
outcome:
1
2
parsing executed.i want parsing so be executed in every loop however i cant seem to be able to solve it?
@GCDX This is easy: MainWindow::parse(bool) will be called as soon as innerPage emits the signal loadFinished, it does not care about your loop. In fact your while() loop is blocking the event loop, so MainWindow::parse(bool) will NOT be called as long as your loop is running.
-
@jsulm how do a rewrite it so that it will work???
@J-Hilk my method is what you describe which doesn't work! something is preventing the parse from running until the while loop is completed
@GCDX said in Does a loop continue running even if Signals and slot is not complete?:
how do a rewrite it so that it will work???
I don't understand what you actually want to achieve.
I don't understand why you call on_pushbutton_clicked() inside the loop.
If you want to call something immediately inside the loop then simply call the method instead of emitting a signal. -
@jsulm how do a rewrite it so that it will work???
@J-Hilk my method is what you describe which doesn't work! something is preventing the parse from running until the while loop is completed
@GCDX said in Does a loop continue running even if Signals and slot is not complete?:
something is preventing the parse from running
Yes, your while loop! What I describted eleminates the loop and manages the next url just, when the first parse is done
-
@GCDX
untested of course.MainWindow::MainWindow(...){ .... connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool))); urlList = QList<QUrl> ({QUrl("Url1"), QUrl("Url"), ...}); } void MainWindow::continue(){ if(cnt < urlList.size()){ innerPage->setUrl(urlList.at(cnt)); print(cnt); } } void MainWindow::on_pushButton_clicked() { cnt = 0; continue(); } void MainWindow::parse(bool) { QWebFrame *frameInner = innerPage->page()->mainFrame(); QWebElement doc = frameInner->documentElement(); QString json=doc.toPlainText(); //parsing code print("parsing executed"); continue(); } }
-
@GCDX
untested of course.MainWindow::MainWindow(...){ .... connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool))); urlList = QList<QUrl> ({QUrl("Url1"), QUrl("Url"), ...}); } void MainWindow::continue(){ if(cnt < urlList.size()){ innerPage->setUrl(urlList.at(cnt)); print(cnt); } } void MainWindow::on_pushButton_clicked() { cnt = 0; continue(); } void MainWindow::parse(bool) { QWebFrame *frameInner = innerPage->page()->mainFrame(); QWebElement doc = frameInner->documentElement(); QString json=doc.toPlainText(); //parsing code print("parsing executed"); continue(); } }
@J.Hilk said in Does a loop continue running even if Signals and slot is not complete?:
connect(innerPage, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
well there you go - I mean, I'd have expected that to be just fine in the new syntax but there it is specifying SIGNAL and SLOT too =) - that's new to me.