Signal and slot related problem?
-
Suppose in a single function i want to use the same button as a on off switch like. I mean when once i click the button a new window should open and when i again click the same button the same window should close.
@connect(button, SIGNAL(clicked()), window, SLOT(open()));
connect(button, SIGNAL(clicked()), window, SLOT(close()));@
Here, my problem is that if i will use signal and slot like the code given above the window will open and suddenly it will close because
both the signals are same so how can i solved it? -
i think you could use something like this :
@
window::window() {
connect(button, SIGNAL(clicked()), this, SLOT(open()));
}
void window::open() {
disconnect(button, SIGNAL(clicked()), this, SLOT(open()));
connect(button, SIGNAL(clicked()), this, SLOT(close()));
}
void window::close() {
disconnect(button, SIGNAL(clicked()), this, SLOT(close()));
connect(button, SIGNAL(clicked()), this, SLOT(open()));
}
@not tested yet though...
-
You could do
@
connect(button, SIGNAL(clicked()), window, SLOT(toggleOpenClose()));
@with a window class which has something like
@class Window ...
{
Q_OBJECT
...
public:
Window() { is_open = false; }void open() { // do opening stuff is_open = true; } void close() { // do closing stuff is_open = false; } public slots: void toggleOpenClose() { if (is_open) close(); else open(); } private: bool is_open;
...
}
@Edit: which is pretty much what Gerolf suggested above