Results 1 to 14 of 14

Thread: event loop already running

  1. #1
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default event loop already running

    I am trying to run a QApplication on button click from Main window.
    However I am getting this error:
    QCoreApplication::exec: The event loop is already running
    My code inside button click is this:
    Qt Code:
    1. if (!g_thread_supported ())
    2. g_thread_init (NULL);
    3.  
    4. gst_init (NULL, NULL);
    5. QApplication app(NULL, NULL);
    6. app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit ()));
    7.  
    8. // prepare the pipeline
    9.  
    10. GstElement *pipeline = gst_pipeline_new ("xvoverlay");
    11. GstElement *src = gst_element_factory_make ("souphttpsrc",NULL);
    12. GstElement *sink = gst_element_factory_make ("xvimagesink", NULL);
    13. GstElement *jpegdec = gst_element_factory_make("jpegdec",NULL);
    14. //assert(jpegdec);
    15. gst_bin_add_many (GST_BIN (pipeline), src,jpegdec, sink, NULL);
    16. gst_element_link_many(src,jpegdec, sink);
    17.  
    18. g_object_set (src, "location", "http://169.254.75.39/video2.mjpg", NULL);
    19. // prepare the ui
    20.  
    21. QWidget window;
    22. window.setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
    23. window.move(0,400);
    24. window.setFixedSize(320, 200);
    25. window.show();
    26.  
    27. WId xwinid = window.winId();
    28. gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), xwinid);
    29.  
    30. // run the pipeline
    31.  
    32. GstStateChangeReturn sret = gst_element_set_state (pipeline,
    33. GST_STATE_PLAYING);
    34. if (sret == GST_STATE_CHANGE_FAILURE) {
    35. gst_element_set_state (pipeline, GST_STATE_NULL);
    36. gst_object_unref (pipeline);
    37. // Exit application
    38. QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
    39. }
    40.  
    41. int ret = app.exec();
    42.  
    43. window.hide();
    44. gst_element_set_state (pipeline, GST_STATE_NULL);
    45. gst_object_unref (pipeline);
    To copy to clipboard, switch view to plain text mode 

    I am able to run this code,if I put it after
    Qt Code:
    1. ui->setupUi(this);
    To copy to clipboard, switch view to plain text mode 
    in the MainWindow

    How can I resolve this problem?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event loop already running

    From QApplication doc : For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. You can not have multiple QApplication objects.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: event loop already running

    How does your main() function look like? Do you create and run a QApplication in there already?

    Cheers,
    _

  4. #4
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: event loop already running

    My main() function is like this:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow w;
    5. w.show();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    I have created 1 QApplication object there also

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: event loop already running

    Then this is enough.
    Just remove the QApplication stuff from your slot.

    Cheers,
    _

  6. #6
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: event loop already running

    I have removed the QApplication stuff from my code.
    But now nothing is being shown.No error also.
    Here is the modified code:
    Qt Code:
    1. if (!g_thread_supported ())
    2. g_thread_init (NULL);
    3.  
    4. gst_init (NULL, NULL);
    5. //QApplication app(NULL, NULL);
    6. //a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit ()));
    7.  
    8. // prepare the pipeline
    9.  
    10. GstElement *pipeline = gst_pipeline_new ("xvoverlay");
    11. GstElement *src = gst_element_factory_make ("souphttpsrc",NULL);
    12. GstElement *sink = gst_element_factory_make ("xvimagesink", NULL);
    13. GstElement *jpegdec = gst_element_factory_make("jpegdec",NULL);
    14. gst_bin_add_many (GST_BIN (pipeline), src,jpegdec, sink, NULL);
    15. gst_element_link_many(src,jpegdec, sink);
    16. g_object_set (src, "location", "http://169.254.75.39/video2.mjpg", NULL);
    17. // prepare the ui
    18.  
    19. QWidget window;
    20. window.setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
    21. window.move(1000,480);
    22. window.setFixedSize(320, 200);
    23. window.show();
    24.  
    25. WId xwinid = window.winId();
    26. gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), xwinid);
    27.  
    28. // run the pipeline
    29.  
    30. GstStateChangeReturn sret = gst_element_set_state (pipeline,
    31. GST_STATE_PLAYING);
    32. if (sret == GST_STATE_CHANGE_FAILURE) {
    33. gst_element_set_state (pipeline, GST_STATE_NULL);
    34. gst_object_unref (pipeline);
    35. // Exit application
    36. //QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
    37. }
    38.  
    39. //int ret = a.exec();
    40.  
    41. window.hide();
    42. gst_element_set_state (pipeline, GST_STATE_NULL);
    43. gst_object_unref (pipeline);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event loop already running

    Quote Originally Posted by prkhr4u View Post
    I have removed the QApplication stuff from my code.
    But now nothing is being shown.No error also.
    Because in line 41 You hide new window.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: event loop already running

    Because in line 41 You hide new window.
    Worse than that. The window is created on the stack in line 19, so as soon as control exits the if() clause starting on line 1, the window goes out of scope and is destroyed. Poof! Now you see it, now you don't!

  9. The following user says thank you to d_stranz for this useful post:

    prkhr4u (12th February 2014)

  10. #9
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: event loop already running

    @Lesiok I agree with your fact that only one QApplication object can be formed , but can you explain how in this case I am able to make 2 QApplication object and run it.
    Here is the code:

    //main.cpp
    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 

    //mainwindow.cpp
    Qt Code:
    1. ui->setupUi(this);
    2. if (!g_thread_supported ())
    3. g_thread_init (NULL);
    4.  
    5. gst_init (NULL, NULL);
    6. QApplication app(NULL, NULL);
    7. app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit ()));
    8.  
    9. // prepare the pipeline
    10.  
    11. GstElement *pipeline = gst_pipeline_new ("xvoverlay");
    12. GstElement *src = gst_element_factory_make ("souphttpsrc",NULL);
    13. GstElement *sink = gst_element_factory_make ("xvimagesink", NULL);
    14. GstElement *jpegdec = gst_element_factory_make("jpegdec",NULL);
    15. //assert(jpegdec);
    16. gst_bin_add_many (GST_BIN (pipeline), src,jpegdec, sink, NULL);
    17. gst_element_link_many(src,jpegdec, sink);
    18. g_object_set (src, "location", "http://169.254.75.39/video2.mjpg", NULL);
    19. // prepare the ui
    20.  
    21. QWidget window;
    22. window.move(0,0);
    23. window.resize(320, 240);
    24. window.show();
    25.  
    26. WId xwinid = window.winId();
    27. gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), xwinid);
    28.  
    29. // run the pipeline
    30.  
    31. GstStateChangeReturn sret = gst_element_set_state (pipeline,
    32. GST_STATE_PLAYING);
    33. if (sret == GST_STATE_CHANGE_FAILURE) {
    34. gst_element_set_state (pipeline, GST_STATE_NULL);
    35. gst_object_unref (pipeline);
    36. // Exit application
    37. QTimer::singleShot(0, QApplication::activeWindow(), SLOT(quit()));
    38. }
    39.  
    40. int ret = app.exec();
    To copy to clipboard, switch view to plain text mode 

    I am able to run successfully and able to get the desired output.



    Added after 12 minutes:


    I have made some changes and am still unable to get the QWidget window.Here is my code on button click event:
    Qt Code:
    1. gst_init (NULL, NULL);
    2. // prepare the pipeline
    3.  
    4. GstElement *pipeline = gst_pipeline_new ("xvoverlay");
    5. GstElement *src = gst_element_factory_make ("souphttpsrc",NULL);
    6. GstElement *sink = gst_element_factory_make ("xvimagesink", NULL);
    7. GstElement *jpegdec = gst_element_factory_make("jpegdec",NULL);
    8.  
    9. gst_bin_add_many (GST_BIN (pipeline), src,jpegdec, sink, NULL);
    10. gst_element_link_many(src,jpegdec, sink);
    11. gst_element_set_state(sink, GST_STATE_READY);
    12. g_object_set (src, "location", "http://169.254.75.39/video2.mjpg", NULL);
    13.  
    14. // prepare the ui
    15. QWidget widg1;
    16. widg1.move(0,0);
    17. widg1.setFixedSize(320,200);
    18. widg1.show();
    19.  
    20. WId xwinid = widg1.winId();
    21. QApplication::syncX();
    22. gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), xwinid);
    23.  
    24. GstStateChangeReturn sret = gst_element_set_state (pipeline,
    25. GST_STATE_PLAYING);
    26. if (sret == GST_STATE_CHANGE_FAILURE) {
    27. gst_element_set_state (pipeline, GST_STATE_NULL);
    28. gst_object_unref (pipeline);
    29. }
    To copy to clipboard, switch view to plain text mode 

    After debugging, I found that an empty QWidget window is created after QApplication::syncX(),but is destroyed as it exits the function.
    Also the State is changing successfully as it does not enter the if condition and directly exit.

    What should I do to retain the window and successfully overlay the gstreamer stream over it??
    Last edited by prkhr4u; 7th February 2014 at 08:22. Reason: duplicate contents

  11. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event loop already running

    What you really want to do ?

  12. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: event loop already running

    Quote Originally Posted by prkhr4u View Post
    What should I do to retain the window and successfully overlay the gstreamer stream over it??
    C++ basics: if you want an object to live across a block scope boundary, create it on the heap.

    As d_stranz already wrote but which you seem to have ignored: you create widg1 on the stack inside the block scope of an if(). It is destroyed when the scope ends.

    Cheers,
    _

  13. The following user says thank you to anda_skoa for this useful post:

    prkhr4u (12th February 2014)

  14. #12
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: event loop already running

    @Lesiok,I want to create a QWidget to play a video stream using gstreamer on button click event.
    But pl tell how are 2 QApplication objects created in the above example ??

    @anda_skoa, point noted. Sorry I neglected the stack point
    I will try to create the QWidget on the heap and see if it persists.

  15. #13
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: event loop already running

    Quote Originally Posted by prkhr4u View Post
    @Lesiok,I want to create a QWidget to play a video stream using gstreamer on button click event.
    But pl tell how are 2 QApplication objects created in the above example ??
    So create QWidget on heap (i.e. with new opeartor) and show them. That is all. You don't need next QApplication.
    You are making a mistake like many others here : trying to use an extensive library (Qt) without knowing the basics of programming language (C++).

  16. The following user says thank you to Lesiok for this useful post:

    prkhr4u (12th February 2014)

  17. #14
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: event loop already running

    Thank You all,the issue has been solved.
    I used declared QWidget like this:
    Qt Code:
    1. QWidget *wid = new QWidget(this);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Not able to receive the custom event from the event loop
    By Jagadeeshakr in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2012, 12:47
  2. Qt/MFC Event Loop - stop MFC receiving event?
    By Kaylx in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2012, 19:57
  3. Replies: 4
    Last Post: 6th August 2011, 02:40
  4. Replies: 10
    Last Post: 15th January 2010, 15:35
  5. Replies: 0
    Last Post: 23rd October 2008, 13:43

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.