PDA

View Full Version : Problems with setScrollBarPolicy in QwebView



shariqkhan
17th June 2011, 16:59
Hi All,

There’s an application running on an embedded device which uses QWebView(actually, a class derived from QWebView to be more precise). Now, in the constructor of that class the scrollbarpolicy is being set to “As needed”


page()->currentframe()->setScrollBarPolicy(Qt::Horizontal,ScrollBarAsNeede d);
page()->currentframe()->setScrollBarPolicy(Qt::Vertical,ScrollBarAsNeeded) ;

So now when we try to load a page, the scrollbars show up(or dont show up) depending on the size of the web page. All fine up to here.
Now, we have an OSK (OnScreenKeyboard) kind of thing that comes up whenever a user activates an input control. This OSK covers the half screen.
Now as per the design requirement, we are required to hide the scrollbars when the OSK is up and showing.
For this I put some checks at places where the OSK gets to show up, to set the scrollbar policy to AlwaysOff. And when I am done with the OSK and close it …at that place I am again setting the policy to AsNeeded.
The problem here is that when the OSK gets closed, the scroll bars still do not appear, even when the page is too big to fit in the viewport.

The slightest mention of the scrollbar is done only at 3 places:
1. Constructor
2. When OSK is activated
3. When OSK is closed

I am not able to understand what code I am missing which results in scrollbar not getting displayed, once it has been hidden and then tried to get displayed again.

Could anyone please be so kind to help me on this.

Not sure whether I am right, but I kind of doubt, is getting the current frame from the page, the right widget to set the scrollBarPolicy. Sorry for the lengthy post, but I am working for the first time on Qt and QWebkit.

Rachol
17th June 2011, 19:17
Have you tried what happens when you set the policy to Qt::ScrollBarAlwaysOn instead of Qt::ScrollBarAsNeeded after your OSK is closed?

Does it show up?

shariqkhan
17th June 2011, 19:26
Thanks for your reply.

Yes, I did try that. But the problem is that doing so displays the Scrollbar even if the web page is small enough to fit in the view-port.

Meanwhile, I am writing a dummy application to simulate that defect.

Rachol
17th June 2011, 19:46
Try then a hack and see if that helps:
page()->currentframe()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOn);
page()->currentframe()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded);

and same for vertical

shariqkhan
17th June 2011, 20:30
Rachol, I tried that, but no luck. It seems that my code is ignoring the scroll bar policy, completely after the OSK is closed. I mean, I called an explicit

this->update();

but that too is not serving any purpose.

Rachol
17th June 2011, 20:52
I keep guessing:

QSize size = page()->viewportSize();
size.rheight() -= 1;
page()->setViewportSize(size);

Does this show the scrollbar?

shariqkhan
17th June 2011, 21:08
Rochel, I am out of my workplace now. Sure, will try that first thing in the morning when I turn up at my office. Thanks

shariqkhan
19th June 2011, 18:00
Okay, today I wrote a dummy application to demonstrate my problem. It consists of Main(source file), MainWindow(source and header files). Here it is:

Main.cpp


#include <QtGui>
#include "MainWindow.h"

int main(int argc, char * argv[])
{
QApplication app(argc, argv);
MainWindow browser;
browser.show();
return app.exec();
}

MainWindow.cpp


#include <QtGui>
#include <QtWebKit>
#include "MainWindow.h"

MainWindow::MainWindow()
{
m_bFullMode = true;

//allocate memory to member widgets
view = new QWebView(this);
addrBar = new QLineEdit(this);
pushButton = new QPushButton("Resize");

//create a toolbar
QToolBar *toolBar = addToolBar(tr("Navigation"));
toolBar->addAction(view->pageAction(QWebPage::Back));
toolBar->addAction(view->pageAction(QWebPage::Forward));
toolBar->addAction(view->pageAction(QWebPage::Reload));
toolBar->addAction(view->pageAction(QWebPage::Stop));
toolBar->addWidget(addrBar);
toolBar->addWidget(pushButton);

//make connections between Signals and Slots
connect(addrBar, SIGNAL(returnPressed()), SLOT(goToAddress()));
connect(pushButton,SIGNAL(clicked()),SLOT(toggleSi ze()));

setCentralWidget(view);
this->resize(450,400);

view->resize(450,250);

view->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAsN eeded);
view->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAsNee ded);

view->load(QUrl("http://www.google.com"));
}


void MainWindow::goToAddress()
{
QUrl url = QUrl(addrBar->text());
view->load(url);
view->setFocus();
}

void MainWindow::toggleSize()
{

if(m_bFullMode == true)
{
m_bFullMode = false;
view->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlw aysOff); //Line A
view->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlway sOff); //Line B
view->resize(450,200);
}
else
{
m_bFullMode = true;
view->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlw aysOn); //Line C
view->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlway sOn); //Line D

QSize size = view->page()->viewportSize();
size.rheight() -= 1;
view->page()->setViewportSize(size);
view->resize(450,300);
}
}




MainWindow.h


#include <QtGui>

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

class QWebView;
class QLineEdit;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

protected slots:

void toggleSize();
void goToAddress();

private:

QWebView *view;
QLineEdit *addrBar;
QPushButton *pushButton;
bool m_bFullMode;
};

#endif // MAINWINDOW_H


This application, too suffers from the same problem, if "Resize" button is hit to toggle the view.

shariqkhan
20th June 2011, 10:37
Can somebody guide me as to what am I doing wrong in the code?

Rachol
20th June 2011, 11:25
Try this:


void MainWindow::toggleSize()
{

if(m_bFullMode == true)
{
m_bFullMode = false;
view->resize(450,300);
view->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlw aysOff); //Line A
view->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlway sOff); //Line B
view->page()->view()->update();
}
else
{
m_bFullMode = true;
view->resize(450,300);
view->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlw aysOn); //Line C
view->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlway sOn); //Line D
view->page()->view()->update();
}
}

shariqkhan
20th June 2011, 12:35
Rochel, still not working.

Rachol
20th June 2011, 12:40
Are you sure? I am running it right now and it seems to work... What problem do you see now?

Edit: But it doesn't when I set ScrollBarAsNeeded....

shariqkhan
20th June 2011, 13:25
Oh, actually right now I tried it on my project application. It does not seem to work over there neither with ScrollBarAsNeeded nor with ScrollBarAlwaysOn. Though, eventually I have to get it working with "AsNeeded".

However, when I get home, I will try it on the dummy application, in that case. And if it works there, may be I can get a way to fix things in the real application as well.
Many thanks.

Rachol
20th June 2011, 13:29
I think this must be considered as QtWebKit bug...

shariqkhan
20th June 2011, 14:05
Exactly my thoughts indeed. But since I am a novice, I need to confirm this before I take to the bugs forum. I do not want to be balsed beacuse of some silly mistake on my part. :D