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:
Code:
class Dispatcher
: public QObject { Q_OBJECT
public:
m_windows << win;
if(m_current == -1) {
setWindow(0);
}
}
public slots:
void next() {
setWindow(m_current+1 % m_windows.count());
}
void prev() {
setWindow(m_current-1 % m_windows.count());
}
void setWindow(int which) {
if(m_current == which) return;
if(m_current!=-1) {
m_windows.at(m_current)->hide();
}
m_current = which;
m_windows.at(m_current)->show();
qApp->processEvents(); // optionally force event processing (don't unless sure it's required)
}
private:
QList<QWidget *> m_windows;
int current = -1;
};
Then it's just a matter of connecting a signal to one of the slots defined in the dispatcher.
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
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.
Re: Need 2 widgets without a parent to comunicate
Quote:
Originally Posted by
GreenScape
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
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
Re: Need 2 widgets without a parent to comunicate
Quote:
Originally Posted by
GreenScape
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...
Re: Need 2 widgets without a parent to comunicate
Quote:
Originally Posted by
wysota
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:
Quote:
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
Re: Need 2 widgets without a parent to comunicate
Quote:
Originally Posted by
boblatino
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:
Code:
widget->show();
widget->setFocus(Qt::OtherFocusReason);
it will probably not work but this might:
Code:
widget->show();
widget->setFocus(Qt::OtherFocusReason);
Re: Need 2 widgets without a parent to comunicate
Quote:
Originally Posted by
wysota
it will probably not work but this might:
Code:
widget->show();
widget->setFocus(Qt::OtherFocusReason);
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