marco
marco

Reputation: 1752

QWebEngine - get current maximum scroll value

I'm creating a markdown text editor that has a QPlainTextEdit on the left, used to enter the text and a QWebEngineView on the right, used to show the preview.

The problem is that I cannot synchronize the QWebEngineView scroll position when I scroll the left panel (the QPlainTextEdit). I can programmatically scroll the QWebEngineView calling

page()->runJavaScript("window.scrollTo(0, y);")

but without its current maximum scroll value, I cannot calculate the right value.

So, the question is:

is there a way to get the current QWebEngineView's maximum scroll value? An equivalent of a

int max = widget->verticalScrollBar()->maximum()

in other words.

Upvotes: 1

Views: 714

Answers (1)

marco
marco

Reputation: 1752

I've found a solution.

document.body.scrollHeight is what I was looking for so I create a const string:

const QString ScrollJavaScript("window.scrollTo(0, document.body.scrollHeight * %1 / %2);");

and after getting the current scroll value and maximum scroll value of the editor (the QPlainText):

double cP   = m->editor->verticalScrollBar()->value();
double maxP = m->editor->verticalScrollBar()->maximum();

I execute the javascript:

if( maxP > 0 )
{
    m->preview->page()->runJavaScript(ScrollJavaScript.arg(cP).arg(maxP));
}

Upvotes: 1

Related Questions