Results 1 to 9 of 9

Thread: Problem with QMainWindow

  1. #1
    Join Date
    Jan 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Problem with QMainWindow

    Hi,

    My application generates a 3d-model using vtk and displays it in a qvtkwidget. When the user clicks the 'Generate' button in one of the main window's dialogs the program generates the model, which takes a significant amount of time. My problem is that while the model is loading, I can't access the main window at all; I can't maximize, minimize, move the window, or even click the 'Cancel' button on the QProgressDialog that appears to show the model's progress. How can I fix this?

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 69 Times in 67 Posts

    Default Re: Problem with QMainWindow

    The model is being loaded in the main thread. While it is loading the model, the GUI isn't updated. You should load the model in a separate thread if possible. This will keep your main thread and thereby your application responsive. (look into QtConcurrent).
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Oct 2009
    Posts
    364
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    10
    Thanked 37 Times in 36 Posts

    Default Re: Problem with QMainWindow

    You must be missing the QCoreApplication:rocessEvents() call inside your loop that loads the model.

    for example:
    Qt Code:
    1. QProgressDialog progress("Receiving File...", "Cancel",0,fileSize, this);
    2. progress.setWindowModality(Qt::WindowModal);
    3. progress.show();
    4.  
    5. m_timeout = 10;
    6. timer->start();
    7. QProgressDialog cancelXfer("Canceling transfer, please wait...",QString(),0,0,this);
    8. cancelXfer.setWindowModality(Qt::WindowModal);
    9. bool flag = true;
    10.  
    11. while(m_timeout > 0)
    12. {
    13. if(!progress.wasCanceled())
    14. {
    15. ret = FT_Read(fth, (unsigned char *)dbuf.data(), num2rd, &numrd);
    16. if(numrd > 0)
    17. {
    18. rxf.write(dbuf,numrd);
    19. byteCount += numrd;
    20. m_timeout = 4;
    21. progress.setValue(byteCount);
    22. }
    23. }
    24. else
    25. {
    26. if (flag)
    27. {
    28. ret = FT_Write(fth, (unsigned char * ) "a",1, &numwr);
    29. flag = false;
    30. cancelXfer.show();
    31. }
    32.  
    33. }
    34.  
    35.  
    36. QCoreApplication::processEvents();
    37. }
    38.  
    39. if (progress.wasCanceled())
    40. cancelXfer.reset();
    41. progress.reset();
    42.  
    43.  
    44. timer->stop();
    To copy to clipboard, switch view to plain text mode 
    Last edited by schnitzel; 2nd March 2010 at 20:41. Reason: better example

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 69 Times in 67 Posts

    Default Re: Problem with QMainWindow

    OP stated that the cancel button was unusable. If the cancel button doesn't even visibly react to the mouse click, you still need to do multithreading. If the button does react, you have to connect it's clicked() signal to a slot that handles the actual abort.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. #5
    Join Date
    Oct 2009
    Posts
    364
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    10
    Thanked 37 Times in 36 Posts

    Default Re: Problem with QMainWindow

    @franz
    cancel button is probably unusable due to absence of processEvents. I updated the sample code.
    Of course if there are any blocking calls when loading the 3d model, then yes one would have to resort to multithreading.
    Last edited by schnitzel; 2nd March 2010 at 20:53.

  6. #6
    Join Date
    Jan 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Default Re: Problem with QMainWindow

    The 'Cancel' button in the dialog does not react at all when clicked. I can see the progress bar working, but the entire window remains inaccessable while the model is being generated.

    I just found the following in the documentation:
    "Multithreaded programming is also a useful paradigm for performing time-consuming operations without freezing the user interface of an application."

    It looks like multithreading is the solution. Unfortunately, I have no idea how multithreading works . Any recommendations for what I could read to learn the basics? The documentation had some suggestions, but they were all in the form of paperback books that I'd have to buy from amazon.com, which I don't really want to do. It's not a very complicated operation; it's only a very small section of code in which I'd have to do multithreading. I can post the code as well, if you think that it would be a better use of my time.

  7. #7
    Join Date
    Oct 2009
    Posts
    364
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    10
    Thanked 37 Times in 36 Posts

    Default Re: Problem with QMainWindow

    Quote Originally Posted by bbad68 View Post
    The 'Cancel' button in the dialog does not react at all when clicked. I can see the progress bar working, but the entire window remains inaccessable while the model is being generated.

    I just found the following in the documentation:
    "Multithreaded programming is also a useful paradigm for performing time-consuming operations without freezing the user interface of an application."

    It looks like multithreading is the solution. Unfortunately, I have no idea how multithreading works . Any recommendations for what I could read to learn the basics? The documentation had some suggestions, but they were all in the form of paperback books that I'd have to buy from amazon.com, which I don't really want to do. It's not a very complicated operation; it's only a very small section of code in which I'd have to do multithreading. I can post the code as well, if you think that it would be a better use of my time.
    If the progressbar still updates, then you are just missing the processEvents call. Look at the example I provided.


    Multithreading is great, if you know what you are doing
    Last edited by schnitzel; 2nd March 2010 at 22:54.

  8. The following user says thank you to schnitzel for this useful post:

    bbad68 (2nd March 2010)

  9. #8
    Join Date
    Jan 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    7

    Post Re: Problem with QMainWindow

    I have no idea what QCoreApplication::processEvents() actually does, but somehow it works

    Thanks a lot!

  10. #9
    Join Date
    Oct 2009
    Posts
    364
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    10
    Thanked 37 Times in 36 Posts

    Default Re: Problem with QMainWindow

    Quote Originally Posted by bbad68 View Post
    I have no idea what QCoreApplication::processEvents() actually does, but somehow it works

    Thanks a lot!
    Glad I could help.

    Just look up the help info on QCoreApplication::processEvents, which describes what it does and also mentions exactly your issue.

    Multithreading is not that easy to use and misunderstood by many. I will always try to avoid it if possible.

    Making code thread-safe is an art and not to mention the complexity of debugging a multithreaded app.

Similar Threads

  1. Subclassing QMainWindow problem
    By lerwys in forum Qt Programming
    Replies: 7
    Last Post: 28th April 2009, 08:40
  2. Problem with using raise function on QMainWindow
    By yartov in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2008, 09:14
  3. QMainWindow: problem changing centralWidget
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 4th October 2007, 13:00
  4. QMainWindow resize problem
    By edb in forum Qt Programming
    Replies: 5
    Last Post: 12th January 2007, 10:31
  5. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49

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
  •  
Qt is a trademark of The Qt Company.