Thread: Connecting QT code with standard C++ classes and functions

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Connecting QT code with standard C++ classes and functions

    If I only want QT for I/O, then what is the best way of doing so ?
    For example,
    If a widget..(say..a QLineEdit) emits a signal : changed(QString)
    How would I use that string for constructing an object of a class taking a std::String as an argument, (without declaring slots..etc etc) for that other class ?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You want to convert a QString to std::string? QString::toStdString does that for you

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    not only that ... I wan to access it from non-Qt code...
    My problem is that I can't find a way to catch the signal containing the QString from a normal function/class..(not a Q_OBJECT )

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok im not sure i am following, could you give a more concrete example?

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Say.. I have a widget
    QLineEdit myobject;
    and I have a function
    void foo(std::string bar);

    How do I connect myobject to foo?

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Do you mean connect as in catch a signal from QLineEdit or connect in some other way? Because if the first you just connect the signal to a member function of the class holding the QLineEdit (or wherever, normal Qt signal handling) and then call foo...I dont think I understand your issue...

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ok..sorry..I didn't think of that..
    but... How do I do the reverse?
    i.e Send the output of foo...say another std::string
    to a QLabel.
    I can't call foo() from a signal in this case..
    Last edited by manasij7479; 06-15-2011 at 09:29 AM.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    How come you can't call foo from a signal?

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    What good would it do?
    I am totally confused..
    I have a full program which takes a string input, evaluates it(if it makes sense) and gives an output.
    What would I do to make it graphical ?
    i.e. take the input from a Widget and then display the result...
    The first part I can understand now, but can't figure out what to so with the rest..

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok, take the following scenario: you have a function that is used to validate some input, returning true (good input) or false (bad input). Now lets say you want to graphically show wether input is good or bad using Qt. So lets say you have a dialog containing QLineEdit editBox and a QLabel validInputLabel. Now you could for instance connect to the textEdited signal, which is emitted every time you edit the text and in the connected method you call validateInput with the text in editBox and depending on if the returnvalue is true or false you set the text of the label accordingly (for instance "bad input" or "good input"). This is just a small demo of how you could do something like you wrote about.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Would the connect look like this?
    Code:
    QObject::connect(&editBox,SIGNAL(textEdited(QString),&validInputLabel,SLOT(myslot(QString)));
    ...where myslot() calls the required function ...and depending on its return... tells the QLabel what to show..

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by manasij7479 View Post
    My problem is that I can't find a way to catch the signal containing the QString from a normal function/class..(not a Q_OBJECT )
    You can't. If it isn't a Q_OBJECT it can't participate in signals.

    You made the choice to use Qt, don't complain now that you have to use its constructs. I warned about this earlier, on your OpenGL thread.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You made the choice to use Qt, don't complain now that you have to use its constructs. I warned about this earlier, on your OpenGL thread.
    I'd use OpenGL for any serious graphics...
    But it is popular opinion that it is an overkill for simple gui apps.(even to the point, saying that it'd be slow...)

    && I'm not complaining about having to use its constructs....I'm merely looking for a way to 'port' my somewhat successful programs till date to have a graphical interface.

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Here is the reference page for signals/slots: Qt 4.7: Signals & Slots I suggest you read it carefully and develop small applications as you go to get an understanding of how things works and how things are supposed to be used. Lots to take in but usable stuff.

    And you might not be able to port your applications right off, GUI programming is quite alot different than console programming.
    Last edited by Shakti; 06-15-2011 at 01:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where to find the code that defines standard Functions?
    By hammer1234 in forum C Programming
    Replies: 7
    Last Post: 04-21-2006, 02:37 AM
  2. Buffers in standard functions
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 07-03-2005, 08:07 PM
  3. Allegro and standard functions
    By Paninaro in forum Game Programming
    Replies: 1
    Last Post: 07-05-2002, 05:41 PM
  4. Source code of the standard library functions...
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 12:35 PM
  5. Standard C++ classes
    By CeeCee in forum C++ Programming
    Replies: 5
    Last Post: 12-23-2001, 04:43 PM

Tags for this Thread