Results 1 to 20 of 29

Thread: Need 2 widgets without a parent to comunicate

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need 2 widgets without a parent to comunicate

    Quote Originally Posted by boblatino View Post
    I now dont have visual studio in my laptop.
    You can use MinGW, we won't mind
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Need 2 widgets without a parent to comunicate

    Quote Originally Posted by wysota View Post
    You can use MinGW, we won't mind
    Here is an example:

    Qt Code:
    1. class window : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. window(QWidget *parent = 0, Qt::WFlags flags = 0) : QWidget(parent, flags)
    7. {
    8.  
    9. }
    10.  
    11. public slots:
    12. void showMe()
    13. {
    14. show();
    15. }
    16.  
    17. void hideMe()
    18. {
    19. hide();
    20. }
    21.  
    22. };
    23.  
    24. class consumerThread : public QThread
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. consumerThread(fifo<int> & f):f(f)
    30. {
    31. start();
    32. }
    33.  
    34. void run()
    35. {
    36. while(true)
    37. {
    38. int a;
    39. f.pop(a);
    40. if(a == 0)
    41. {
    42. emit showMe1();
    43. emit hideMe1();
    44. }
    45. else
    46. {
    47. emit showMe2();
    48. emit hideMe2();
    49. }
    50. }
    51. }
    52.  
    53. signals:
    54. void showMe1();
    55. void showMe2();
    56. void hideMe1();
    57. void hideMe2();
    58. private:
    59.  
    60. fifo<int> & f;
    61.  
    62. };
    63.  
    64. class producerThread : public QThread
    65. {
    66. public:
    67. producerThread(fifo<int> & f):f(f)
    68. {
    69. start();
    70. }
    71.  
    72. void run()
    73. {
    74. while(true)
    75. {
    76. f.push(0);
    77. Sleep(1000);
    78. f.push(1);
    79. Sleep(1000);
    80. }
    81. }
    82. private:
    83.  
    84. fifo<int> & f;
    85.  
    86. };
    87.  
    88. class dispatcher : public QObject
    89. {
    90. Q_OBJECT
    91.  
    92. public:
    93. dispatcher()
    94. {
    95. consumerThread* consumer = new consumerThread(queue);
    96. producerThread* producer = new producerThread(queue);
    97. window1 = new window();
    98. window2 = new window();
    99.  
    100. connect(consumer, SIGNAL(showMe1()), window1, SLOT(showMe()));
    101. connect(consumer, SIGNAL(showMe2()), window2, SLOT(showMe()));
    102. connect(consumer, SIGNAL(hideMe1()), window1, SLOT(hideMe()));
    103. connect(consumer, SIGNAL(hideMe2()), window2, SLOT(hideMe()));
    104. }
    105. private:
    106. fifo<int> queue;
    107. window* window1;
    108. window* window2;
    109.  
    110. };
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Ramiro

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need 2 widgets without a parent to comunicate

    Starting a thread in its constructor is in general a bad idea. But back to the main issue... you are aware that slots of a QThread subclass are executed in the context of the main thread and not the thread controlled by the QThread object, right? Please also explain what is the point in emitting showMe1() and immediately after that emitting hideMe1().

    It could be simpler to explain what effect you want to achieve and we will suggest a better approach.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Need 2 widgets without a parent to comunicate

    Quote Originally Posted by wysota View Post
    you are aware that slots of a QThread subclass are executed in the context of the main thread and not the thread controlled by the QThread object, right?
    No, I didnt know that, but in the real code the thread is a OS thread (via CreateThread).

    Please also explain what is the point in emitting showMe1() and immediately after that emitting hideMe1()
    It has no point sorry. It should be

    Qt Code:
    1. emit showMe1();
    2. emit hideMe2();
    To copy to clipboard, switch view to plain text mode 

    The idea is an application that has more than 1 top level window and a dispatcher that controls which window is shown. Sometimes it could be no window shown and sometimes 2 at the same time. The issue that we are facing now is that when a widget is hidden and other is put in the front (show), the first time you touch the window (we are using touchscreen), the event is processed by the hidden widget. The second time, the shown widget captures the event normaly. I have called activateWindow also but with no luck. Is there a better approach on this? I dont want to create and close (destroy) the widgets all the time so I have created them once and show / hide accordly. If I create them and destroy every time I dont have this problem.

    Thanks
    Ramiro

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need 2 widgets without a parent to comunicate

    I don't think we can help you if you show us code that does something different than the one you are actually using...

    If the two windows are never shown together maybe you should use a QStackedWidget instead of two widgets?

    Anyway... if I wanted to have a mechanism for switching windows, I'd do it more or less like this:
    Qt Code:
    1. class Dispatcher : public QObject {
    2. Q_OBJECT
    3. public:
    4. void addWindow(QWidget *win) {
    5. m_windows << win;
    6. if(m_current == -1) {
    7. setWindow(0);
    8. }
    9. }
    10. public slots:
    11. void next() {
    12. setWindow(m_current+1 % m_windows.count());
    13. }
    14. void prev() {
    15. setWindow(m_current-1 % m_windows.count());
    16. }
    17. void setWindow(int which) {
    18. if(m_current == which) return;
    19. if(m_current!=-1) {
    20. m_windows.at(m_current)->hide();
    21. }
    22. m_current = which;
    23. m_windows.at(m_current)->show();
    24. qApp->processEvents(); // optionally force event processing (don't unless sure it's required)
    25. }
    26. private:
    27. QList<QWidget *> m_windows;
    28. int current = -1;
    29. };
    To copy to clipboard, switch view to plain text mode 

    Then it's just a matter of connecting a signal to one of the slots defined in the dispatcher.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    boblatino (26th July 2010)

  7. #6
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Need 2 widgets without a parent to comunicate

    Thanks wysota, so that will be another way of show / hide the windows, but why the inactive window (hidden) takes the focus instead of the one that is shown the first time? After just a click the shown window it will regain focus and work. I have also called activateWindow but it doesnt take the control of the focus. Also I dont understand why a hidden window can take the user event of a press.


    Thanks
    Ramiro

  8. #7
    Join Date
    Jul 2010
    Posts
    53
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need 2 widgets without a parent to comunicate

    when you have QueuedConnection emit post event. that event must be processed by event loop in concrete widget; because only one parentless widet wokrs with main event loop at same time(the signal is posted but nobody cares, because main event loop processing another widget's events) you should create event loops for both threads.

    OR

    you should inherit from QDialog and use exec(); instead show(); because exec creates own dialog's event loop.
    Last edited by GreenScape; 27th July 2010 at 00:38.

  9. #8
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Need 2 widgets without a parent to comunicate

    Quote Originally Posted by GreenScape View Post
    when you have QueuedConnection emit post event. that event must be processed by event loop in concrete widget; because only one parentless widet wokrs with main event loop at same time(the signal is posted but nobody cares, because main event loop processing another widget's events) you should create event loops for both threads.
    So I must create 2 separate threads one per window? That will cause synchronization issues. Is possible to create multiple loops without a new thread?

    Thanks
    Ramiro

  10. #9
    Join Date
    Jul 2010
    Posts
    53
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need 2 widgets without a parent to comunicate

    no, u just should to create event loop for each widget.

    example:

    create 2 threads, inside each thread create widget, and event loop.
    this will look like 2 applications, each widget in its own thread, but in fact it will be one app
    Last edited by GreenScape; 27th July 2010 at 00:45.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need 2 widgets without a parent to comunicate

    Quote Originally Posted by GreenScape View Post
    create 2 threads, inside each thread create widget, and event loop.
    this will look like 2 applications, each widget in its own thread, but in fact it will be one app
    If you do that your app will crash. Why? Search google (or the forum) for Qt+widget+thread. I'm a bit tired repeating this over and over...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Jul 2010
    Posts
    23
    Thanks
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Need 2 widgets without a parent to comunicate

    Quote Originally Posted by wysota View Post
    If you do that your app will crash.
    I also have the same idea, only one thread should be the graphical thread. So how can I solve the problem:

    inactive window (hidden) takes the focus instead of the one that is shown the first time? After just a click the shown window it will regain focus and work.
    Thanks
    Ramiro

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need 2 widgets without a parent to comunicate

    Quote Originally Posted by boblatino View Post
    So how can I solve the problem
    First determine what exactly happens. It could be you are not letting the events be processed and something doesn't know it should release (or gain) focus, i.e. you can't have a focus on a widget which is hidden, so if you do:
    Qt Code:
    1. widget->show();
    2. widget->setFocus(Qt::OtherFocusReason);
    To copy to clipboard, switch view to plain text mode 
    it will probably not work but this might:
    Qt Code:
    1. widget->show();
    2. QCoreApplication::processEvents();
    3. widget->setFocus(Qt::OtherFocusReason);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    boblatino (27th July 2010)

Similar Threads

  1. Replies: 7
    Last Post: 12th January 2011, 22:01
  2. Replies: 1
    Last Post: 22nd April 2010, 14:34
  3. Replies: 2
    Last Post: 22nd April 2010, 11:44
  4. Replies: 3
    Last Post: 29th May 2008, 13:50
  5. initialize child widgets within parent?
    By ucomesdag in forum Newbie
    Replies: 6
    Last Post: 6th June 2006, 08:11

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