Results 1 to 7 of 7

Thread: connect() variable passing issue

  1. #1
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default connect() variable passing issue

    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:
    Qt Code:
    1. connect(reply, SIGNAL(finished()), this, SLOT(writefile(filename)));
    To copy to clipboard, switch view to plain text mode 

    When I change that connection and the function to define the filename in the function writefile(), then it works (with the following connection):
    Qt Code:
    1. connect(reply, SIGNAL(finished()), this, SLOT(writefile()));
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect() variable passing issue

    Just declare both singal and slot to have a parameter (define the slot to use that parameter) and emit the signal something like this:
    Qt Code:
    1. emit signal_name(some_string_variable);
    To copy to clipboard, switch view to plain text mode 
    Also the connect code will be something like this:
    Qt Code:
    1. connect(reply, SIGNAL(finished(string)), this, SLOT(writefile(string))); //only type name here
    To copy to clipboard, switch view to plain text mode 

    More in the documentation of signals and slots

    Also it's a good idea to use QString 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)

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect() variable passing issue

    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

    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

  4. #4
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: connect() variable passing issue

    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?

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect() variable passing issue

    Are you talking about some specific readready() signal or some coded by you?

  6. #6
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: connect() variable passing issue

    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

  7. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: connect() variable passing issue

    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.

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2010, 12:53
  2. Replies: 1
    Last Post: 28th March 2010, 05:52
  3. Issue using connect, what am I missing?
    By technoViking in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2010, 05:11
  4. qt signal/slot auto-connect issue
    By parnedo in forum Qt Programming
    Replies: 9
    Last Post: 16th July 2009, 12:55
  5. Replies: 3
    Last Post: 8th June 2007, 20:13

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
  •  
Qt is a trademark of The Qt Company.