Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29

Thread: Need 2 widgets without a parent to comunicate

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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

    boblatino (26th July 2010)

  3. #22
    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

  4. #23
    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.

  5. #24
    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

  6. #25
    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.

  7. #26
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


  8. #27
    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

  9. #28
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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.


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

    boblatino (27th July 2010)

  11. #29
    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
    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 
    Thanks wysota, it does work now perfectly. I dont know why processEvents is required to avoid sending events to the hidden window but it WORKS! Thanks a lot to all the forum members that have posted here.

    Ramiro

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.