Results 1 to 9 of 9

Thread: Create QMainWindow from different thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Create QMainWindow from different thread

    Hey community,

    I try to create something like a window pool. You can use these windows everywhere in your program to display graphics and plot diagrams etc. The widgets working well but the main problem at the moment is the frustrated tries, to create the pool. One non QObject-Object should represent a QMainWindow to cut the bindings to Qt.

    I cannot create a widget -> I tried invokeMethode, connect and QTimer but nothing works. Sometimes the methods don´t get called or I am not in the gui thread... Any idea?

    header:

    Qt Code:
    1. #pragma once
    2. #include <QMainWindow>
    3.  
    4. class MyWindow : QObject
    5. {
    6. Q_OBJECT
    7. public:
    8. MyWindow();
    9. signals:
    10. void start();
    11. private:
    12. QWindow *mWin;
    13. };
    14.  
    15. class QWindowPool : public QObject
    16. {
    17. Q_OBJECT
    18. public:
    19. QWindowPool();
    20. public slots:
    21. void createWindow();
    22. };
    23.  
    24. class QWindow : public QMainWindow
    25. {
    26. Q_OBJECT
    27. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <utils/MatWindow.h>
    2.  
    3. #include <QApplication>
    4. #include <QTimer>
    5.  
    6. #include <thread>
    7.  
    8. QWindowPool pool;
    9.  
    10. QWindowPool::QWindowPool()
    11. {
    12. int c = 0;
    13. new QApplication(c, NULL);
    14. }
    15.  
    16. void QWindowPool::createWindow()
    17. {
    18. printf("window created\n");
    19. new QWindow();
    20. }
    21.  
    22. MyWindow::MyWindow()
    23. {
    24. connect(this, SIGNAL(start()), &pool, SLOT(createWindow()));
    25. QTimer::singleShot(0, &pool, SLOT(createWindow()));
    26. QMetaObject::invokeMethod(&pool, "createWindow", Qt::BlockingQueuedConnection);
    27. emit start();
    28. }
    29.  
    30.  
    31. int main()
    32. {
    33. std::thread t1([](){
    34. MyWindow mw;
    35. qApp->processEvents(QEventLoop::AllEvents); // <- not working
    36. });
    37. // qApp->processEvents(QEventLoop::AllEvents); // work
    38. t1.join();
    39.  
    40. return 0;
    41. }
    To copy to clipboard, switch view to plain text mode 

    So, it does not work (createWindow does not get called) unless I uncomment the last processEvents in the main thread but that should not be the solution.

    What I want is in the final application: The user should get the possibility to write anywhere in his code and in any thread:
    Qt Code:
    1. MyWindow mw(dataToDisplay)
    To copy to clipboard, switch view to plain text mode 
    and the window should be created and showed to him. Currently it only works if he would call:
    Qt Code:
    1. qApp->processEvents(QEventLoop::AllEvents);
    To copy to clipboard, switch view to plain text mode 
    in the main thread. But I want do do this for him.... HOW?

    Thank you.

    BTW: I "cross posted" this question. First I asked at stackoverflow but didn´t get a right solution. http://stackoverflow.com/questions/2...fferent-thread
    Last edited by Viatorus; 20th February 2015 at 19:57.

Similar Threads

  1. Replies: 2
    Last Post: 17th August 2013, 16:43
  2. QMainWindow: create onStartup-like function [SOLVED]
    By thomaspu in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2012, 15:42
  3. Create QMainWindow in runtime
    By qt_developer in forum Newbie
    Replies: 2
    Last Post: 25th June 2012, 20:22
  4. Replies: 6
    Last Post: 22nd December 2011, 22:03
  5. Replies: 1
    Last Post: 24th December 2010, 00:32

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.