PDA

View Full Version : multiple slots with one signal



kja
20th November 2010, 18:29
Hi,

I have a signal that is connected to three slots, I was wondering if I could make it so that if slot 1 is wrong (like the user put in the wrong date) the other two would not be called.

Is it possible to have a slot emit a signal that could then be connected to another slot? For example if my slot 1 works it will emit a signal, but if it fails the signal will not be emitted?

What I am trying to do is have a user enter a file path and start and end dates and then plot them. I want to check if the dates are valid and also if the file path is valid before I call the plotting function.

Any ideas? Thanks!

tbscope
20th November 2010, 18:53
Is it possible to have a slot emit a signal that could then be connected to another slot? For example if my slot 1 works it will emit a signal, but if it fails the signal will not be emitted?

Yes, that's the correct way.

squidge
20th November 2010, 19:02
Sure, you can have a slot that emits another signal that is connected to another slot.

Or you can just call that other slot directly from your first one.

MattPhillips
20th November 2010, 19:09
This should be pretty easy. Slots are executed in the order they are connected. So one way to do this would be to create a flag e.g. 'input_is_ok' which is accessible from all the slots, starts out ==true, gets set to false on bad input, and causes your subsequent plotter slot to abort ('if (!input_ok) return;'). If all your slots are methods of a single object, this would just be a boolean member. A much more complicated way to do it would be with event filters and custom events, but I can't see any reason to do this except perhaps aesthetics (in this way you would block the events from ever reaching the slots at all).

Matt

tbscope
20th November 2010, 19:14
This should be pretty easy. Slots are executed in the order they are connected. So one way to do this would be to create a flag e.g. 'input_is_ok' which is accessible from all the slots, starts out ==true, gets set to false on bad input, and causes your subsequent plotter slot to abort ('if (!input_ok) return;'). If all your slots are methods of a single object, this would just be a boolean member.

In my personal opinion that would complicate the code. Well, at least for me it would be more difficult to read the code and the flow of the code. While technically ok of course.

kja
22nd November 2010, 17:04
So I'm trying to have my slot emit a signal but I am getting the error " C2065 emit undeclared identifier." I think I have all the includes I need. I'm probably just doing something stupid, here is my code:



class MyWidget : public QWidget
{
Q_OBJECT
.....

Q_SIGNALS:
void validFile();

public Q_SLOTS:
void enterFile();
void getDates();
void slotButtonLoads();
};

MyWidget::MyWidget(QWidget * parent):QWidget(parent){
...
push = new QPushButton("Plot", this);
connect(push, SIGNAL(clicked()), this, SLOT(enterFile()));
connect(push, SIGNAL(validFile()),this, SLOT(getDates())); ///also I don't know what my sender should be for this new signal, 'push' is probably not right
}

void MyWidget::enterFile()
{
QString textEntered = lineedit->text();
FileName = qstrdup( textEntered.toLatin1() );
if (fopen (FileName, "rb") == NULL){
emit validFile();
}else{
//something } //// a third question.. how can I allow the user to try again with the filename. I don't think I should
////// push->disconnect() because it will break my connection
}



Thanks for all the help

squidge
22nd November 2010, 17:33
emit() is declared in an include file, so you do not have all the appropriate include files, though it is only syntactic sugar.

kja
22nd November 2010, 20:01
What include could it be, I included <QtGui> just to make sure but I still get the error.

Also does anyone know what the sender would be for my signal when I connect it?


connect(push, SIGNAL(validFile()),this, SLOT(getDates())); ///'push' is probably not right

would I have to create a new qobject to be the sender?

Thanks

squidge
22nd November 2010, 22:37
It's defined in qobjectdefs.h, but you shouldn't normally include that file directly. Most files (such as qobject.h) will include it for you. Or you can just "#define emit" which just exactly the same thing.

Yes, if you want to use signals and slots, the class should derive from QObject and have the Q_OBJECT macro.