You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, March 19th 2009, 1:35am

How to set user agent in QWebView?

Hello to everyone!
I'm just starting to write programs with QT and I am using QWebView in my application. I need a way to set the user agent string for the QWebView that I'm using, and I haven't found any way to do this. In QWebPage I found userAgentForUrl, but I am using QWebView and I can't seem to be able to access the QWebPage in QWebView. Any help is much appreciated as I really need to have a custom user agent string.
John

2

Thursday, March 19th 2009, 11:18am

http://doc.trolltech.com/4.5/qwebview.html#setPage
Make descendant of QWebPage and set it to QWebView with setPage.
Fighting fire with fire.
Three can keep a secret if two of them are dead.

3

Thursday, March 19th 2009, 7:34pm

http://doc.trolltech.com/4.5/qwebview.html#setPage
Make descendant of QWebPage and set it to QWebView with setPage.

This is what I did and it still doesn't work:
header file:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
class webPage : public QWebPage
{
public:
webPage();

QString userAgentForUrl(const QUrl &url );
};

class browserControl : public QWebView
{
public:
browserControl();
};

in the cpp file:

Source code

1
2
3
4
5
6
7
8
9
10
11
browserControl::browserControl()
{
QWebView::QWebView();
}
QString webPage::userAgentForUrl(const QUrl &url ){
return QString("test user agent");
}

webPage::webPage(){
QWebPage::QWebPage();
}

In the main class file I did:

Source code

1
2
browserControl *webControl = new browserControl();webPage * page = new webPage();
webControl->setPage((QWebPage*)page);

It's still not working. I'm testing using a local LAMP setup and the user agent string that the server gets is :
Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Qt/4.5.0

Any ideas?
John

4

Friday, March 20th 2009, 5:36am

Source code

1
2
3
4
5
6
7
8
class webPage : public QWebPage
{
public:
webPage();

QString userAgentForUrl(const QUrl &url );
};
}

Your have forggot the "const" at the end. So it is not parent class function (it is new function) and parent class virtual function is still not overriden.
Fighting fire with fire.
Three can keep a secret if two of them are dead.

5

Friday, March 20th 2009, 3:18pm

Thanks a lot!!!
Works now, and I can send a custom user agent string. Thanks again.
John

6

Wednesday, January 20th 2010, 11:40am

I have still se same problem. just get the normal header.

I don#t understand what the suggestion of the missing "const" meant.

here my code



browserControl.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <QWebView>
 #include <QWebPage>
 #include "browserControl.h"

 browserControl::browserControl() { 
QWebView::QWebView();
 }  

QString webPage::userAgentForUrl(const QUrl &url ){

 return QString("test user agent"); 
} 
 
webPage::webPage(){ 
QWebPage::QWebPage(); 
}


browserControl.h

Source code

1
2
3
4
5
6
7
8
class webPage : public QWebPage { 
public: webPage();  
QString userAgentForUrl(const QUrl &url ); 
}; 

class browserControl : public QWebView {
 public: browserControl();
 };


and in the programm:

Source code

1
2
3
4
5
 browserControl *webControl = new browserControl();     
webPage * page = new webPage();     
webControl->resize(1000, 500); 
       webControl->setPage((QWebPage*)page);
 webControl->load(url);


thanks for any suggestions.

I just don't get what is wrong. I compared the code of john556 and messenger

This post has been edited 2 times, last edit by "BrockLanders" (Jan 20th 2010, 12:17pm)


7

Wednesday, January 20th 2010, 1:28pm

Source code

1
2
3
4
class webPage : public QWebPage { 
public: webPage();  
QString userAgentForUrl(const QUrl &url ) const; 
}; 

const at the end ;)
Fighting fire with fire.
Three can keep a secret if two of them are dead.

8

Monday, May 24th 2010, 6:10pm

Resetting qtwebkit user-agent string

Thanks for sharing this code, but I now have the 3 files and can not seem to make things work;(
I get 2 errors when compiling the code in QTCreator 1.3.1:

~/browserControl.cpp:8: error: prototype for ‘QString webPage::userAgentForUrl(const QUrl&)’ does not match any in class ‘webPage’
~/browserControl.h:3: error: candidate is: virtual QString webPage::userAgentForUrl(const QUrl&) const

What am I doing wrong? Thanks;)!

//browserControl.h
class webPage : public QWebPage {
public: webPage();
QString userAgentForUrl(const QUrl &url ) const;
};
class browserControl : public QWebView {
public: browserControl();
};

//browserControl.c
#include <QWebView>
#include <QWebPage>
#include "browserControl.h"
browserControl::browserControl() {
QWebView::QWebView();
}
QString webPage::userAgentForUrl(const QUrl &url ) {
return QString("test user agent");
}
webPage::webPage() {
QWebPage::QWebPage();
}

//main.cpp
#include<QWebView>
#include<QApplication>
#include</home/computer/browserControl.h>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
browserControl *webControl = new browserControl();
webPage * page = new webPage();
webControl->setPage((QWebPage*)page);
webControl->load(QUrl("http://www.mailwriter.com/"));
webControl->showMaximized();
return a.exec();
}

slevon

Trainee

Posts: 90

Location: Germany

Occupation: electrical engineer

  • Send private message

9

Monday, May 24th 2010, 10:08pm

Hi,
your function has to have a const, too.

slevon

This post has been edited 2 times, last edit by "slevon" (May 24th 2010, 10:22pm)


slevon

Trainee

Posts: 90

Location: Germany

Occupation: electrical engineer

  • Send private message

10

Monday, May 24th 2010, 10:17pm

hi again,

your implemantation hast to be like

Source code

1
2
3
4
5
...
QString webPage::userAgentForUrl(const QUrl &url ) const{
return QString("test user agent");
}
...

This should work.

hope this helps,
slevon

This post has been edited 1 times, last edit by "slevon" (May 24th 2010, 10:24pm)


11

Tuesday, May 25th 2010, 1:59am

Thanks slevon, you are so good!
For the new people following this thread,
here is the code that works, have fun!:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//browserControl.h
class webPage : public QWebPage {
	public: webPage();
	QString userAgentForUrl(const QUrl &url ) const;
};
class browserControl : public QWebView {
	public: browserControl();
};

//browserControl.cpp
#include <QWebView>
#include <QWebPage>
#include "browserControl.h"
browserControl::browserControl() {
	QWebView::QWebView();
}
QString webPage::userAgentForUrl(const QUrl &url ) const {
	return QString("test user agent");
}
webPage::webPage() {
	QWebPage::QWebPage();
}

//main.cpp
#include<QWebView>
#include<QApplication>
#include</full/path/to/browserControl.h>
int main(int argc, char *argv[]) {
	QApplication a(argc, argv);
	browserControl *webControl = new browserControl();
	webPage * page = new webPage();
	webControl->setPage((QWebPage*)page);
	webControl->load(QUrl("http://www.google.com/"));
	webControl->showMaximized();
	return a.exec();
}

12

Saturday, August 13th 2011, 6:26pm

Hi, I want to be able to set the user-agent string from the command line argument list, like this

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
int main(int argcchar *argv[]) {
    QApplication a(argcargv);
    QStringList alist a.arguments();
    browserControl *webControl = new browserControl();
    webPage page = new webPage();
    page->userAgentForUrl(QUrl(alist.at(1))); //overrides the protected qt function
    webControl->setPage((QWebPage*)page);
    QNetworkRequest request(QUrl("http://www.google.com/"));
    webControl->load(request);
    webControl->showMaximized();
    return a.exec();
}


Ive changed the function already

PHP Source code

1
2
3
QString webPage::userAgentForUrl(const QUrl &url ) const {
    return QString(url.toString());
}


But this wouldnt work. The User-Agent header becomes the requested url. In this case
User-Agent: http://www.google.com
I want the User-Agent to change to whatever I set it to in the argument list.
Can anybody help me out? :)

This post has been edited 5 times, last edit by "djarend" (Aug 13th 2011, 6:37pm)