Results 1 to 3 of 3

Thread: What is the "main thread"? Why is it special?

  1. #1
    Join Date
    Aug 2008
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default What is the "main thread"? Why is it special?

    Many places in the Qt docs we are warned that the QApplication must only be created on the "main thread", and similarly for all GUI objects. But there is no explanation. Does "main thread" really mean the one created by the OS to start a new process? And if so, why does QApplication require that? Couldn't it just as well live on any thread of a process?

    I'm asking because (like many others) I would like to add a Qt-based GUI to an existing command line program, something like this: the app creates a new thread to run the gui initialization routine, which creates a QApplication and calls its exec(). Then the app 'main' thread creates a QObject that sets up queued signal connections to gui functions, and emits those signals in fns callable from the app. Any required blocking/unblocking of the main thread is handled outside Qt (e.g. in libpthread). It seems to me this satisfies the thread-safeness rules of Qt, except possibly "the QApplication must live on the main thread". Why shouldn't it work?

    Thanks for your advice
    -- Tom

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What is the "main thread"? Why is it special?

    You can put it in a seperate thread if you wish, as long as only a single thread interfaces with the UI.

    However, this might not be portable (which is the idea of Qt). For example, I've heard that such applications do not work properly on Mac OS (Note however that I do not have first hand experience of this, it's only what I've read)

  3. #3
    Join Date
    Aug 2008
    Posts
    29
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is the "main thread"? Why is it special?

    Thanks squidge,

    In fact the scheme I described is working, on Windows with the MinGW build tools (Qt 4.6 opensource). But still at an early stage of development, and it is pretty easy to write things that crash.

    The project is an interactive video transcoder derived from ffmpeg. I'm hoping the present scheme (ffmpeg on main thread) will be viable on Win32 and Linux (I've had problems with Qt on OSX before, so I'm not counting on that platform). But if it turns out that QApplication really does need to own the main thread, I can arrange to run ffmpeg on a worker thread.

    -- Tom

Similar Threads

  1. Replies: 6
    Last Post: 13th February 2014, 12:46
  2. Replies: 7
    Last Post: 22nd October 2010, 12:17
  3. Replies: 2
    Last Post: 11th May 2009, 16:50
  4. QSystemTrayIcon as "main window" design issue
    By nooky59 in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 13:15
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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.