'evaluateJavaScript()' doesn't return any value. (content position on page wanted)
-
@JNBarchan I'm at work right now so I can't test too much, but from what I see the program is def. waiting. :-)
I guess applying the delay to the right point will do the trick, but I can only verify that later today. :-)
Really interested to try this out, I'll keep you up-to-date! -
@JNBarchan said in 'evaluateJavaScript()' doesn't return any value. (content position on page wanted):
@ShivaHaze
The delay principle will only work provided that the web view continues to load/process during the delay. That is what I am hoping theQCoreApplication::processEvents()
will allow....You are a god to me - Thanks so much!
It works perfectly!
1 second delay is enough to get the job done. :-)Whoever is interested in the full code - here is my full mainwindow.cpp - the only file I really touched until this point. :-)
#include "mainwindow.h" #include "ui_mainwindow.h" #include <string> #include <iostream> #include <stdlib.h> #include <stdio.h> #include <QMainWindow> #include <QApplication> #include <Qt> #include <QDebug> #include <QWebView> #include <QWebPage> #include <QWebFrame> #include <QtWebKit> #include <QScrollBar> #include <QScrollArea> #include <QString> #include <QTimer> using namespace std; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // Configurating Application-Window setWindowFlags(Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint); setFixedSize(width(), height()); // Object-Reference Shortcuts QWebFrame *MF = ui->webView->page()->mainFrame(); // Deactivating Scrolling-Bars MF->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff); MF->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff); QTime dieTime= QTime::currentTime().addSecs(1); while (QTime::currentTime() < dieTime) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); QVariant bodytop = MF->evaluateJavaScript("document.body.getBoundingClientRect().top"); QVariant bodyleft = MF->evaluateJavaScript("document.body.getBoundingClientRect().left"); QVariant elemtop = MF->evaluateJavaScript("document.getElementById('user_reg').getBoundingClientRect().top"); QVariant elemleft = MF->evaluateJavaScript("document.getElementById('user_reg').getBoundingClientRect().left"); qDebug() << "Body Top: " << bodytop.toInt() << " Body Left: " << bodyleft.toInt() << " Elem Top: " << elemtop.toInt() << " Elem Left: " << elemleft.toInt(); } MainWindow::~MainWindow() { delete ui; }
Here you can see the console output before and after applying the delay. :-)
-
@ShivaHaze
@ShivaHaze said in 'evaluateJavaScript()' doesn't return any value. (content position on page wanted):You are a god to me - Thanks so much!
I think you might have meant "good" rather than "god" ;-)
I am very pleased this approach has worked for you. It is potentially useful for me to know for my own work one day. Had I realized you had not tried the "delay" principle earlier, we would have got there quicker.
The actual implementation of the delay loop is "naughty". It will mean that your application is "running busily" (i.e. using CPU time, potentially blocking other applications) for most of 1 second. If an expert here looks at it, please don't shout at me! Like I said, I got it from elsewhere as "quick & dirty". For your purposes it's probably fine, and we achieved it without you having to do slots & signals which you were not keen on learning at this stage.
If you feel like you want to improve/experiment, in the same stackoverflow thread have a look at https://stackoverflow.com/a/3756341/489865, leveraging
QWaitCondition
, whose description sounds like it should have the same effect without the "busy blocking":class SleepSimulator{ QMutex localMutex; QWaitCondition sleepSimulator; public: SleepSimulator::SleepSimulator() { localMutex.lock(); } void sleep(unsigned long sleepMS) { sleepSimulator.wait(&localMutex, sleepMS); } void CancelSleep() { sleepSimulator.wakeAll(); } };
Certainly I would try it. If you copied that class into your code, you would call it (I assume) via:
SleepSimulator* sleep = new SleepSimulator(); sleep->sleep(1000); delete sleep;
Now I think you can get down to the real coding you want to do for your application! Best of luck.