Results 1 to 6 of 6

Thread: QWebView with two instances of QApplication

  1. #1
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question QWebView with two instances of QApplication

    We are developing our own widget that inherits from QWebView; we are using TDD (Test Driven Development) to do this. For every test we run, we create a QApplication, run the test code and delete the QApplication, the idea behind this is to have tests that behave as much as possible as if they were completely independent from the rest of the tests.
    The problem we are having is that after the first test (the tests run every time in a random order, so every test can be the first test) when we set a piece of HTML code to our QWebView class (using setHtml(QString)) the code is not interpreted, it is shown as plain text (but without the HTML tags).
    To try this we made a test that creates an instance of our class, set the HTML code, shows the widget on screen (so we can see the results) and then deletes the instance it created. We create a duplicate of this test and run them in random order, no matter the order they run, always the one that runs first works, the second shows plain text.
    We went even further and modify both tests so after showing the widget on screen and deleting it, they create a new instance, set a different piece of HTML code, show it on screen again and delete the new instance. The results are exactly the same, the first test that runs shows the correct, interpreted, web page (both times), the second shows only uninterpreted HTML code without tags (both times).
    To isolate the problem, we created a main function using only QApplication and QWebView: we created an instance of a QApplication, an instance of QWebView, set a piece of HTML code, show it on screen, delete the QWebView, delete the QApplication and start over again. The results are exactly the same, the first time it is ok and the second shows only plain text. Here is the source code for this test, it only uses QApplication and QWebView, to see the result, after the first window is shown, just close it and the second one will appear.

    Qt Code:
    1. #include <QApplication>
    2. #include <QWebView>
    3. int main (int argc, char **argv)
    4. {
    5. // Declare and create the QApplication
    6. QApplication* theApplication;
    7. theApplication = new QApplication(argc, argv);
    8.  
    9. // Declare and create the QWebView, set a minimun size so we can see it OK
    10. QWebView* pkWebView = new QWebView();
    11. pkWebView->setMinimumSize(320,240);
    12.  
    13. // Set the HTML code
    14. pkWebView->setHtml("<html>\
    15. <head>\
    16. <title></title>\
    17. </head>\
    18. <body>\
    19. <!-- Put the body of your page below this line -->\
    20. <p><b>This text is bold</b></p>\
    21. <p><strong>This text is strong</strong></p>\
    22. <p><em>This text is emphasized</em></p>\
    23. <p><i>This text is italic</i></p>\
    24. <p><small>This text is small</small></p>\
    25. <p>This is<sub> subscript</sub> and <sup>superscript</sup></p>\
    26. <!-- Put the body of your page above this line -->\
    27. </body>\
    28. </html>");
    29.  
    30. // Show it on screen
    31. pkWebView->show();
    32. theApplication->exec();
    33.  
    34. // Delete the QWebView
    35. delete pkWebView;
    36. pkWebView = NULL;
    37.  
    38. // Exit and delete the QApplication
    39. qApp->exit(0);
    40. delete qApp;
    41.  
    42. // Create a new QApplication
    43. theApplication = new QApplication(argc, argv);
    44.  
    45. // Create a new QWebkit with the same size
    46. pkWebView = new QWebView();
    47. pkWebView->setMinimumSize(320,240);
    48.  
    49. // Set the same HTML code
    50. pkWebView->setHtml("<html>\
    51. <head>\
    52. <title></title>\
    53. </head>\
    54. <body>\
    55. <!-- Put the body of your page below this line -->\
    56. <p><b>This text is bold</b></p>\
    57. <p><strong>This text is strong</strong></p>\
    58. <p><em>This text is emphasized</em></p>\
    59. <p><i>This text is italic</i></p>\
    60. <p><small>This text is small</small></p>\
    61. <p>This is<sub> subscript</sub> and <sup>superscript</sup></p>\
    62. <!-- Put the body of your page above this line -->\
    63. </body>\
    64. </html>");
    65.  
    66. // Show it on screen
    67. pkWebView->show();
    68. theApplication->exec();
    69.  
    70. // Delete the QWebkit
    71. delete pkWebView;
    72. pkWebView = NULL;
    73. }
    To copy to clipboard, switch view to plain text mode 

    Another curious thing is that if we add create a second QWebView instance for each instance of QApplication we are using, the second theApplication->exec(); throws and exception, but if we comment this line the other 3 times that it is called it works OK, We really don't know if it is related or not, here is the code of this second test, with the line that generates the exception commented, uncomment it to see the exception:

    Qt Code:
    1. #include <QApplication>
    2. #include <QWebView>
    3. int main (int argc, char **argv)
    4. {
    5. // Declare and create the QApplication
    6. QApplication* theApplication;
    7. theApplication = new QApplication(argc, argv);
    8.  
    9. // Declare and create the QWebView, set a minimun size so we can see it OK
    10. QWebView* pkWebView = new QWebView();
    11. pkWebView->setMinimumSize(320,240);
    12.  
    13. // Set the HTML code
    14. pkWebView->setHtml("<html>\
    15. <head>\
    16. <title></title>\
    17. </head>\
    18. <body>\
    19. <!-- Put the body of your page below this line -->\
    20. <p><b>This text is bold</b></p>\
    21. <p><strong>This text is strong</strong></p>\
    22. <p><em>This text is emphasized</em></p>\
    23. <p><i>This text is italic</i></p>\
    24. <p><small>This text is small</small></p>\
    25. <p>This is<sub> subscript</sub> and <sup>superscript</sup></p>\
    26. <!-- Put the body of your page above this line -->\
    27. </body>\
    28. </html>");
    29.  
    30. // Show it on screen
    31. pkWebView->show();
    32. theApplication->exec();
    33.  
    34. // Delete the QWebView
    35. delete pkWebView;
    36. pkWebView = NULL;
    37.  
    38. // Create the QWebView again within the same instance of QApplication
    39. pkWebView = new QWebView();
    40. pkWebView->setMinimumSize(320,240);
    41.  
    42. // Set the HTML code
    43. pkWebView->setHtml("<html>\
    44. <head>\
    45. <title></title>\
    46. </head>\
    47. <body>\
    48. <!-- Put the body of your page below this line -->\
    49. <p><b>This text is bold</b></p>\
    50. <p><strong>This text is strong</strong></p>\
    51. <p><em>This text is emphasized</em></p>\
    52. <p><i>This text is italic</i></p>\
    53. <p><small>This text is small</small></p>\
    54. <p>This is<sub> subscript</sub> and <sup>superscript</sup></p>\
    55. <!-- Put the body of your page above this line -->\
    56. </body>\
    57. </html>");
    58.  
    59. // Show it on screen
    60. pkWebView->show();
    61. //------------------------------------------------------------------
    62. // UNCOMMENT THIS LINE TO SEE THE EXCEPTION
    63. //theApplication->exec();
    64. //------------------------------------------------------------------
    65.  
    66. // Delete the QWebView
    67. delete pkWebView;
    68. pkWebView = NULL;
    69.  
    70. // Exit and delete the QApplication
    71. qApp->exit(0);
    72. delete qApp;
    73.  
    74. // Create a new QApplication
    75. theApplication = new QApplication(argc, argv);
    76.  
    77. // Create a new QWebkit with the same size
    78. pkWebView = new QWebView();
    79. pkWebView->setMinimumSize(320,240);
    80.  
    81. // Set the same HTML code
    82. pkWebView->setHtml("<html>\
    83. <head>\
    84. <title></title>\
    85. </head>\
    86. <body>\
    87. <!-- Put the body of your page below this line -->\
    88. <p><b>This text is bold</b></p>\
    89. <p><strong>This text is strong</strong></p>\
    90. <p><em>This text is emphasized</em></p>\
    91. <p><i>This text is italic</i></p>\
    92. <p><small>This text is small</small></p>\
    93. <p>This is<sub> subscript</sub> and <sup>superscript</sup></p>\
    94. <!-- Put the body of your page above this line -->\
    95. </body>\
    96. </html>");
    97.  
    98. // Show it on screen
    99. pkWebView->show();
    100. theApplication->exec();
    101.  
    102. // Delete the QWebkit
    103. delete pkWebView;
    104. pkWebView = NULL;
    105.  
    106. // Create the QWebView again within the same instance of QApplication
    107. pkWebView = new QWebView();
    108. pkWebView->setMinimumSize(320,240);
    109.  
    110. // Set the HTML code
    111. pkWebView->setHtml("<html>\
    112. <head>\
    113. <title></title>\
    114. </head>\
    115. <body>\
    116. <!-- Put the body of your page below this line -->\
    117. <p><b>This text is bold</b></p>\
    118. <p><strong>This text is strong</strong></p>\
    119. <p><em>This text is emphasized</em></p>\
    120. <p><i>This text is italic</i></p>\
    121. <p><small>This text is small</small></p>\
    122. <p>This is<sub> subscript</sub> and <sup>superscript</sup></p>\
    123. <!-- Put the body of your page above this line -->\
    124. </body>\
    125. </html>");
    126.  
    127. // Show it on screen
    128. pkWebView->show();
    129. theApplication->exec();
    130.  
    131. // Delete the QWebView
    132. delete pkWebView;
    133. pkWebView = NULL;
    134. }
    To copy to clipboard, switch view to plain text mode 

    We tried this code with Qt 4.7.4 and 4.8.4 and the results were the same. We are using Visual Studio 2008 on Microsoft Windows 7 (64 bits).

    Any hint that can point us in the right direction will be appretiated.

    It would also help to know, if someone can try this on Mac or Unix, if this happens on another platfom/compiler combination.

    Thank you very much for you time.

    I also asking this on Stack Exchange and QtForum.org

    Daniel
    Last edited by khilin; 24th April 2013 at 17:16.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QWebView with two instances of QApplication

    QApplication (QCoreApplication) is a singleton design, it is not intended to be created multiple times in an application. As a matter of fact even if you create multiple instance, user code will access a different instance (may be the second instance) and the Qt library code will still be using the first instance and if you deleted the first instance or has gone out of scope your system will be UNSTABLE.

    You have to consider to change the test approach and base all you testing with one QApplication instance.

    Even if some one were to find the reason behind the behavior (the obvious reason is that Qt lib internally uses the first instance), you will still have to live with single QApplication instance.

    It would also help to know, if someone can try this on Mac or Unix, if this happens on another platfom/compiler combination.
    I think it is clear that platform will not play any role in this, it is just that Qt is designed that way.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    khilin (25th April 2013)

  4. #3
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWebView with two instances of QApplication

    Santosh, thank you very much for your answer.

    After we read your answer we are already working to change the test approach to use only one instance of QApplication. We have a question about it, is there any way to reset the state of the instance of QApplication, we want to be sure that the execution of one test does not affect the execution of the rest of the tests.

    Again, thank you for your time.

    Daniel

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QWebView with two instances of QApplication

    We have a question about it, is there any way to reset the state of the instance of QApplication,
    As such there is no direct way (API) to reset the QApplication. What you could do is individually reset the properties which are of your interest.

    we want to be sure that the execution of one test does not affect the execution of the rest of the tests.
    IMO, when it come to unit tests, each test is supposed to set up it's required settings with in the test itself, so the test case should pass what ever be the condition of QApplication, so I don't see any need to resetting QApplication state
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWebView with two instances of QApplication

    What exactly would you want to reset in the application object? If you are not modifying the object yourself then there is nothing to reset. Webviews will not contaminate the application object in any way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,418
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWebView with two instances of QApplication

    I would suggest to look into Qt's unit test framework QTestLib.

    It is trivial to use and you'll get a separate executable for all things you want to have separate.

    Cheers,
    _

Similar Threads

  1. Multiple instances of QWebView
    By darious in forum Qt Programming
    Replies: 1
    Last Post: 11th October 2011, 07:55
  2. Replies: 2
    Last Post: 1st August 2011, 06:30
  3. Chaining QIODevice Instances
    By wswartzendruber in forum Newbie
    Replies: 9
    Last Post: 29th July 2009, 20:23
  4. Multiple program instances
    By scwizard in forum Qt Programming
    Replies: 13
    Last Post: 1st April 2007, 17:42
  5. accessing my own class-instances
    By mikro in forum Newbie
    Replies: 3
    Last Post: 11th July 2006, 00:10

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.