Results 1 to 6 of 6

Thread: Why do I require a new event loop from a GUI?

  1. #1
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Why do I require a new event loop from a GUI?

    I have some simple code to read a web page. You have seen the code before, it is very simple:
    Qt Code:
    1. QNetworkRequest request;
    2. QUrl hostUrl;
    3. hostUrl.setUrl("http://qt.nokia.com");
    4. request.setUrl(hostUrl);
    5. QNetworkReply* reply = networkAccessManager->get(request);
    To copy to clipboard, switch view to plain text mode 
    Next, I connect more connections than seems reasonable. My question is, how should I do this in a GUI? My initial solution was to run this code in a slot that was called from a menu item. Of course, I have no response unless i start an event loop:
    Qt Code:
    1. loop.exec();
    To copy to clipboard, switch view to plain text mode 
    I had expected that this would use the GUI's event loop (a GUI always has an event loop, right?) You know, started this way
    Qt Code:
    1. QApplication a(argc, argv);
    2. MainWindow w;
    3. w.show();
    4. return a.exec();
    To copy to clipboard, switch view to plain text mode 
    That said, I find it disturbing that I started an event loop in a handler for a menu command.

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

    Default Re: Why do I require a new event loop from a GUI?

    You shouldn't have to create a new event loop unless you want to call slots in a different thread. I'm not sure what the problem is, but as far as I know the main event loop should take care of this.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    pitonyak (15th February 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why do I require a new event loop from a GUI?

    It is useful to know that I should not need to manually start an event loop, especially if I am setting things from a handler already. I suppose that I should find some material on event loops and such. I did notice that some items may not be called until after I kill the application and things are shutting down.

    I originally wrote my code in Java, where my calls to gather information are blocking, so the structure of my code is more suited for this scenario; for example, I make a call and I know how to parse the returned values because I know the context of the call. I loose this with the slots and call backs without seriously changing my logic.

    I suppose that I must school myself more seriously in the inner works of the QT event loop, since I expected there to be an event loop in my GUI and for things called in the slots to be called from that event loop.

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

    Default Re: Why do I require a new event loop from a GUI?

    Could you post the code you are using (you said it isn't much). Maybe I can pinpoint what the problem is. The code you posted before is for me not enough to determine what the exact problem is (i.e. what part of the documentation you need to read ).
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    pitonyak (16th February 2010)

  7. #5
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why do I require a new event loop from a GUI?

    Based on your willingness to look at my code, I started to extract the small amount of working code for this particular piece. While performing this exercise, the solution hit me in the side of the head.

    First, let me say that the code is really as simple as it looks. All of the displayed code sits in a class named SmugMugController. This is my code to connect:
    Qt Code:
    1. void MainWindow::on_actionConnect_activated()
    2. {
    3. QString emailAddress = "andrew@pitonyak.org";
    4. QString password = "fake_value";
    5. SmugMugController controller;
    6. SmugMugLoginEntity* login = controller.login(emailAddress, password);
    7. }
    To copy to clipboard, switch view to plain text mode 
    My initial code was in Java, and everything was synchronous. The QT code is asynchronous, which means that the returned object is always NULL. That is not the problem, however. The problem is that my controller is a local variable, and the moment that I return from on_actionConnect_activated, the controller variable goes out of scope, its destructor is called, and all of the contained objects are destroyed. This includes my get request.

    By adding an internal event loop, the loop is started before I return and the class can be destructed. Although this did work, it is the wrong solution.

    Moving my controller variable out to the main class, solves the problem. That said, I will likely change the architecture so that it is more in line with the way that QT does things.

    Thanks for the offer Franz. If you ever make it to Columbus, Ohio in the USA, let me know and I will provide you with a tasty beverage of your choice.

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

    Default Re: Why do I require a new event loop from a GUI?

    Great. I'll try to remember that .
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Replies: 10
    Last Post: 15th January 2010, 14:35
  2. Event Loop
    By node_ex in forum Qt Programming
    Replies: 1
    Last Post: 12th December 2008, 07:52
  3. Replies: 0
    Last Post: 23rd October 2008, 12:43
  4. calling event through in loop
    By raghvendramisra in forum Qt Programming
    Replies: 3
    Last Post: 28th February 2008, 05:41
  5. Glib event loop
    By Brandybuck in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2006, 17:19

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.