Results 1 to 7 of 7

Thread: operator<< QString

  1. #1
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default operator<< QString

    Hello,

    i'm sure someone had the same problem before. I am trying to make one of my classes able to receive "<<"-Operations. this is my code:

    Qt Code:
    1. // messagepipe.h
    2. friend void operator<<(MessagePipe *pipe, QString string);
    3.  
    4. //messagepipe.cpp
    5. void operator<<(MessagePipe *pipe, QString string)
    6. {
    7. //do something
    8. }
    To copy to clipboard, switch view to plain text mode 

    MessagePipe is my class. This produces an error if used like this:

    Qt Code:
    1. MessagePipe *hint;
    2. hint << "Hi there.";
    To copy to clipboard, switch view to plain text mode 

    --> error: invalid operands of types 'MessagePipe*' and 'const char [10]' to binary 'operator<<'

    Hm, he isn't able to get a QString out of "Hi there". This seems to work only if used as a real parameter. Does anybody understand this behaviour? I don't want to write everytime QString("My Text"). Any ideas? Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: operator<< QString

    Try "const QString &" instead of QString.

  3. The following user says thank you to wysota for this useful post:

    kiker99 (5th May 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: operator<< QString

    Quote Originally Posted by wysota
    Try "const QString &" instead of QString.
    thanks for the fast reply, but this doesn't change anything here
    Is this supposed to help or are you guessing?

  5. #4
    Join Date
    Jan 2006
    Location
    Russia
    Posts
    16
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: operator<< QString

    'const QString&' should help.
    my original posting was incorrect
    Last edited by shad; 4th May 2006 at 22:17.

  6. #5
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: operator<< QString

    Quote Originally Posted by shad
    'const QString&' should help.
    my original posting was incorrect
    code looks like this now.
    Qt Code:
    1. // messagepipe.h
    2. friend void operator<<(MessagePipe *pipe, const QString& string);
    3.  
    4. // messagepipe.cpp
    5. void operator<<(MessagePipe *pipe, const QString& string)
    6. {
    7. pipe->message(string);
    8. }
    To copy to clipboard, switch view to plain text mode 

    the error is still exactly the same.
    I even recompiled the project, but that didn't help either.

  7. #6
    Join Date
    Jan 2006
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: operator<< QString

    Make the pointer a reference and it should work. You could also return the pipe to allow chaining.
    Qt Code:
    1. // messagepipe.h
    2. friend MessagePipe& operator<<(MessagePipe &pipe, const QString& string);
    3.  
    4. // messagepipe.cpp
    5. MessagePipe& operator<<(MessagePipe &pipe, const QString& string)
    6. {
    7. pipe.message(string);
    8. return pipe;
    9. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to orb9 for this useful post:

    kiker99 (5th May 2006)

  9. #7
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: operator<< QString

    yihaa, thx a lot @orb9. I had to change everything for references, but now it is working

    to tired to think about the reason for this...

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  2. easiest Way QString can do
    By baray98 in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2008, 20:49
  3. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41

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.