Connecting QT code with standard C++ classes and functions

This is a discussion on Connecting QT code with standard C++ classes and functions within the Tech Board forums, part of the Community Boards category; If I only want QT for I/O, then what is the best way of doing so ? For example, If ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 )
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Say.. I have a widget
    QLineEdit myobject;
    and I have a function
    void foo(std::string bar);

    How do I connect myobject to foo?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    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, 11:35 AM
  5. Standard C++ classes
    By CeeCee in forum C++ Programming
    Replies: 5
    Last Post: 12-23-2001, 03:43 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21