PDA

View Full Version : connect() variable passing issue



hojoff79
31st January 2011, 23:18
I am having trouble passing a variable through a connection.

Within a function, I have a variable filename that I want to pass on to writefile(std::string), and I am trying to use a connection that follows:


connect(reply, SIGNAL(finished()), this, SLOT(writefile(filename)));


When I change that connection and the function to define the filename in the function writefile(), then it works (with the following connection):


connect(reply, SIGNAL(finished()), this, SLOT(writefile()));


But I need the function to be able to accept a filename variable when that signal slot connection is made, how can I make that work? Thank you in advance.

Zlatomir
31st January 2011, 23:51
Just declare both singal and slot to have a parameter (define the slot to use that parameter) and emit the signal something like this:

emit signal_name(some_string_variable);
Also the connect code will be something like this:

connect(reply, SIGNAL(finished(string)), this, SLOT(writefile(string))); //only type name here

More in the documentation of signals and slots (http://doc.qt.nokia.com/4.7/signalsandslots.html)

Also it's a good idea to use QString (http://doc.qt.nokia.com/4.7/qstring.html) instead of std::string, you will get many advantages (the most important one is that you don't need to convert strings all the time)

stampede
31st January 2011, 23:56
connect(reply, SIGNAL(finished()), this, SLOT(writefile(filename)));
You want to "pass a variable through a connection", but the "finished()" signal does not deliver any string data. For example, in order to send integer value, you would use:
SIGNAL( my_signal(int) )
So, if you want to send a string, you need to declare and use appropriate signal.

accept a filename variable when that signal slot connection is made
2) You dont use variable names when making connections, you need to use datatypes.
This one is not correct:
connect( SIGNAL( a_signal( const QString& myString ), ... ), ...
but this is ok:
connect( SINGAL( a_signal( const QString& ), ... ), ...

Maybe read this first:
Signals and slots (http://doc.qt.nokia.com/latest/signalsandslots.html)


pass on to writefile(std::string)
Why are you using a slot that accepts std::strings ? Maybe better question would be, why are you using std::strings in qt-based app, where you have such lovely QString interface ? :)

-------

edit: Zlatomir was faster ;)

hojoff79
1st February 2011, 01:21
Ok I understand the connection things now about passing data types, but I don't understand how I can get from a preset signal like readready() which does not pass on any data type to a connection that does include data types. So say I create something like SIGNAL(writesignal(std::string)), I cannot just emit it because I need it to wait for readready(). But again I cannot connect readready() to that signal because readready() does not pass along a data type and writesignal requires one.

How do I get past this fundamental difference?

Zlatomir
1st February 2011, 01:35
Are you talking about some specific readready() signal or some coded by you?

hojoff79
1st February 2011, 05:21
my fault type-o, I mean the readyread() signal that is standard with the a QNetworkReply instance. (it emit's automatically when the http response has data ready to be written somewhere.) It's not a signal i wrote, it comes with the QNetworkReply

Zlatomir
1st February 2011, 11:18
One idea* is:
Create a slot without parameters in some place with access to data string. Connect readyread() with this slot.
Declare a signal with a string parameter.
And in the first slot you can emit the other signal with parameter (which will be connected to your slot that expects a string)

*You must think about other ideas all depends on how your classes are designed
Or even direct function call, slots are member functions, so can be called just like any other member function.

//Consider using QString instead std::string.