Results 1 to 10 of 10

Thread: QWebView linkclicked and form submit

  1. #1
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QWebView linkclicked and form submit

    Hello everybody.
    I am creating a new class to subclass QWebView

    To manage link navigation i used :
    view->page()->setLinkDelegationPolicy(QWebPage:elegateAllLinks); and connected
    and then
    connect( view->page(), SIGNAL(linkClicked(const QUrl&)),this, SLOT(OnLinkClicked(const QUrl&)));

    it work for all kind of links but when a form is submitted I dont receive the signal and the submitted page is loaded in the QWebPage

    I read that form submit call the function acceptNavigationRequest
    and when I check in the sourcecode of Qwebpage and i get

    #if QT_VERSION >= 0x040400
    bool QWebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
    #else
    bool QWebPage::acceptNavigationRequest(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
    #endif
    {
    if (type == NavigationTypeLinkClicked) {
    switch (d->linkPolicy) {
    case DontDelegateLinks:
    return true;
    case DelegateExternalLinks:
    if (WebCore::SecurityOrigin::shouldTreatURLSchemeAsLo cal(request.url().scheme()))
    return true;
    emit linkClicked(request.url());
    return false;
    case DelegateAllLinks:
    emit linkClicked(request.url());
    return false;
    }
    }
    return true;
    }

    any navigation request should emit linkclicked when DelegateAllLinks is set


    Where is the problem ? I am using Qt 4.5

    thx for your help.

    [EDIT:] I am beginning at Qt

  2. #2
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWebView linkclicked and form submit

    The problem is here:
    Qt Code:
    1. if (type == NavigationTypeLinkClicked)
    To copy to clipboard, switch view to plain text mode 
    According to the docs type will be QWebPage::NavigationTypeFormSubmitted for a form, and as such the code you pasted will just return true. You need to subclass QWebPage and override acceptNavigationRequest.

    P.S Please use [code][/code] tags.

  3. The following user says thank you to numbat for this useful post:

    maddog_fr (4th August 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView linkclicked and form submit

    geez i am stupid i didnt see the return true !!

    another problem come then. I am not using directly QWebPage but a subclassed Qwebview
    is it possible to use a subclassed QWebPage in my subclassed QWebview ?

  5. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWebView linkclicked and form submit

    Quote Originally Posted by maddog_fr View Post
    is it possible to use a subclassed QWebPage in my subclassed QWebview ?
    Yes you can use,
    Qt Code:
    1. QWebView::setPage ( YOURPAGE * page )
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to yogeshgokul for this useful post:

    maddog_fr (4th August 2009)

  7. #5
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView linkclicked and form submit

    I just tried quickly but seems it's not so easy to subclass QWebPage

  8. #6
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView linkclicked and form submit

    Quote Originally Posted by maddog_fr View Post
    I just tried quickly but seems it's not so easy to subclass QWebPage
    Sublclassing QWebPage is not the problem in fact but it seems that the function acceptNavigationRequest is protected

  9. #7
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWebView linkclicked and form submit

    I'm not a C++ guru, but I think protected member functions can be overridden in a subclass. Have you tried?

  10. #8
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView linkclicked and form submit

    Ok I managed to make it work (I guess my brain was tired yesterday).

    Qt Code:
    1. //myqwebpage.h
    2. #ifndef MYQWEBPAGE_H
    3. #define MYQWEBPAGE_H
    4.  
    5. #include <QObject>
    6. #include <QWebPage>
    7.  
    8. class MyQWebPage : public QWebPage
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MyQWebPage(QObject *parent=0);
    14.  
    15. protected:
    16. #if QT_VERSION >= 0x040400
    17. bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
    18. #else
    19. bool acceptNavigationRequest(QWebFrame *frame, const QWebNetworkRequest &request, NavigationType type);
    20. #endif
    21.  
    22. signals:
    23. #if QT_VERSION >= 0x040400
    24. void formSubmitted(const QNetworkRequest &request);
    25. #else
    26. void formSubmitted(const QWebNetworkRequest &request);
    27. #endif
    28.  
    29.  
    30. };
    31.  
    32. #endif // MYQWEBPAGE_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // myqwebpage.cpp
    2. #include <QtGui>
    3. #include "myqwebpage.h"
    4.  
    5. MyQWebPage::MyQWebPage(QObject *parent): QWebPage(parent){
    6. }
    7.  
    8.  
    9. #if QT_VERSION >= 0x040400
    10. bool MyQWebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
    11. #else
    12. bool MyQWebPage::acceptNavigationRequest(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
    13. #endif
    14. {
    15. if (type == NavigationTypeFormSubmitted) {
    16. emit formSubmitted(request);
    17. return false;
    18. }
    19. return QWebPage::acceptNavigationRequest(frame,request,type);
    20. }
    To copy to clipboard, switch view to plain text mode 

    then finaly I connect the signal to a slot
    Qt Code:
    1. #if QT_VERSION >= 0x040400
    2. connect(webpage,SIGNAL(formSubmitted(const QNetworkRequest&)),this,SLOT(OnFormSubmitted(const QNetworkRequest&)));
    3. #else
    4. connect(webpage,SIGNAL(formSubmitted(const QWebNetworkRequest&)),this,SLOT(OnFormSubmitted(const QWebNetworkRequest&)));
    5. #endif
    To copy to clipboard, switch view to plain text mode 

    thx for your help.

  11. #9
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView linkclicked and form submit

    Seems I did it for nothing.


    I thought that the QNetworkRequest was holding the data to be posted but i dont find it.
    any idea how to find the data ?

    My goal is to open the result of the pos in a new window (window = a widget with a qwebview)

  12. #10
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView linkclicked and form submit

    Ok I managed to do what I want

    Qt Code:
    1. #if QT_VERSION >= 0x040400
    2. bool MyQWebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, QWebPage::NavigationType type)
    3. #else
    4. bool MyQWebPage::acceptNavigationRequest(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
    5. #endif
    6. {
    7. if (type == NavigationTypeFormSubmitted) {
    8. HTMLWindow *newWindow = WindowManager::instance()->addHTMLWindow();
    9. MyQWebView *webview;
    10. webview = newWindow->view;
    11. webview->load(request);
    12. return false;
    13. }
    14. return QWebPage::acceptNavigationRequest(frame,request,type);
    15. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.