Results 1 to 3 of 3

Thread: QWebPage createWindow! When I set body onload=window.open…, window.open return undefi

  1. #1
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Android

    Question QWebPage createWindow! When I set body onload=window.open…, window.open return undefi

    I want to rewrite QWebPage *createWindow(QWebPage::WebWindowType type) to get the open event, but when the page is like below it not work. document.write write “unfined”. If I call the function on a button’s click event, it works well. what’s wrong with qtwebkit? Is there some settings needed to be set? Html code

    <body onload = myopen()>
    <script>
    function myopen(){
    win = window.open('http://www.baidu.com');
    document.write(win);
    }
    </script>
    <input value="click" type="button" ></input>
    </body>




    C code is like below //main.cpp:

    class webPage : public QWebPage
    {
    Q_OBJECT
    public:
    explicit webPage(QObject *parent = 0);

    protected:
    QWebPage *createWindow(QWebPage::WebWindowType type)
    {
    QWebView *wv = new QWebView;
    if (type == QWebPage::WebModalDialog)
    wv->setWindowModality(Qt::ApplicationModal);
    return wv->page();
    }
    };
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QWebView view;
    view.settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
    view.settings()->setAttribute(QWebSettings::JavascriptCanOpenWindo ws, true);
    view.setPage(new WebPage);
    view.load(QUrl("file:///<path-to-html>"));
    view.show();
    return a.exec();
    }

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QWebPage createWindow! When I set body onload=window.open…, window.open return un

    There's nothing wrong with webkit. Your code was not displaying a new view (i.e. never called show()). Here is a working example with the new windows logic in the view not page.

    Qt Code:
    1. #include <QtGui>
    2. #include <QWebPage>
    3. #include <QWebView>
    4. #include <QDebug>
    5.  
    6. class WebView: public QWebView
    7. {
    8. Q_OBJECT
    9. public:
    10. WebView(QWidget *p = 0): QWebView(p)
    11. {
    12. }
    13.  
    14. QWebView *createWindow(QWebPage::WebWindowType type)
    15. {
    16. QWebView *webView = new QWebView;
    17. QWebPage *newWeb = new QWebPage(webView);
    18. if (type == QWebPage::WebModalDialog)
    19. webView->setWindowModality(Qt::ApplicationModal);
    20. webView->setAttribute(Qt::WA_DeleteOnClose, true);
    21. webView->setPage(newWeb);
    22. webView->show();
    23.  
    24. return webView;
    25. }
    26. };
    27.  
    28. int main(int argc, char *argv[])
    29. {
    30. QApplication a(argc, argv);
    31.  
    32. WebView view;
    33. view.settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
    34. view.settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
    35.  
    36. QUrl url = QUrl::fromLocalFile("/tmp/tt/test.html");
    37. view.load(url);
    38. view.show();
    39.  
    40. return a.exec();
    41. }
    42. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

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

    allenchang (14th January 2013)

  4. #3
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Android

    Default Re: QWebPage createWindow! When I set body onload=window.open…, window.open return un

    thank you for your reply, it works well! ah... I want to know how to reimplement QWebPage::createWindow to capture the open event!

Similar Threads

  1. Replies: 3
    Last Post: 23rd December 2010, 07:55
  2. Replies: 0
    Last Post: 10th September 2010, 14:23
  3. Replies: 1
    Last Post: 3rd March 2010, 09:04
  4. problem with window.location in QWebPage
    By speedy_gonzales in forum Qt Webkit
    Replies: 0
    Last Post: 24th February 2010, 06:40
  5. Open a window inside another window
    By passerb in forum Qt Programming
    Replies: 6
    Last Post: 18th October 2009, 14:24

Tags for this Thread

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.