Results 1 to 9 of 9

Thread: QString conversion function

  1. #1
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    5
    Qt products
    Qt5

    Default QString conversion function

    Hello. I'm trying to make my work easier by overloading QString() conversion function:
    Qt Code:
    1. private:
    2. QString* c_string;
    3. public:
    4. operator QString() {
    5. if( c_type == STRING )
    6. return *c_string;
    7. else
    8. return toString();
    9. }
    To copy to clipboard, switch view to plain text mode 
    then I'm trying to use it like this
    Qt Code:
    1. QString line(int i) const {
    2. return c_labels[i] + ": " + c_values[i]; // c_labels[i] returns QString, c_values[i] returns the object with the above conversion function.
    3. }
    To copy to clipboard, switch view to plain text mode 
    and I'm getting error: call of overloaded 'QString(const IniParam&)' is ambiguous
    I also tried putting ": " in QString(), same for c_values[i], <QString> before c_values[i] doesn't help either;
    Any Solutions?
    Last edited by Bziur; 4th March 2014 at 20:56.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString conversion function

    We currently cannot see enough. Please post a small, complete example that fails to compile in this way.

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

    Bziur (5th March 2014)

  4. #3
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    5
    Qt products
    Qt5

    Default Re: QString conversion function

    Ok so I figured out it happens when you overload some different conversion functions;
    Any solution to that? I wanna be able to easily output to multiple formats.

    convertable.h
    Qt Code:
    1. #ifndef CONVERTABLE_H
    2. #define CONVERTABLE_H
    3.  
    4. #include <QString>
    5.  
    6. class Convertable
    7. {
    8. private:
    9. int i;
    10. QString c_string;
    11. public:
    12. Convertable(int in=0, const QString string = QString()):c_string(string),i(in) {}
    13.  
    14. operator int()
    15. {
    16. return i;
    17. }
    18.  
    19. operator QString()
    20. {
    21. return c_string;
    22. }
    23. };
    24.  
    25. #endif // CONVERTABLE_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include <QCoreApplication>
    2.  
    3. #include "convertable.h"
    4. #include <QDebug>
    5.  
    6. QString toString(const QString &in1, Convertable &in2)
    7. {
    8. return in1 + ": " + in2;
    9. }
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QCoreApplication a(argc, argv);
    14.  
    15. QString A("Print");
    16. Convertable B("something");
    17. qDebug() << toString(A,B);
    18.  
    19. return a.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Bziur; 5th March 2014 at 10:54.

  5. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString conversion function

    Please specify what all is wrong now. At least, the following will not pass:
    Qt Code:
    1. Convertable B("something");
    To copy to clipboard, switch view to plain text mode 
    You do not have a ctor for it. Try
    Qt Code:
    1. Convertable B(0,"something");
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Radek for this useful post:

    Bziur (5th March 2014)

  7. #5
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    5
    Qt products
    Qt5

    Default Re: QString conversion function

    I forgot to change that but it doesn't matter much as it's further down the issue list.
    The error I'm getting:
    C:\Test\main.cpp:8: error: ambiguous overload for 'operator+' (operand types are 'const QString' and 'Convertable')
    Qt Code:
    1. return in1 + ": " + in2;
    2. ^
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString conversion function

    This can be caused by the int conversion of Convertable because there exists operator + ( const QString& str, char chr ). Try:

    (1) Comment out the int conversion operator for now (only to see what's wrong).
    (2) Make the 2nd param of toString() const Convertable&.

  9. The following user says thank you to Radek for this useful post:

    Bziur (5th March 2014)

  10. #7
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    5
    Qt products
    Qt5

    Default Re: QString conversion function

    added: const to Convertable & - same problem
    then removed int conversion:

    main.cpp:8: error: invalid user-defined conversion from 'const Convertable' to 'const QString&' [-fpermissive]

    Qt Code:
    1. return in1 + ": " + in2;
    2. ^
    To copy to clipboard, switch view to plain text mode 

    main.cpp:3: In file included from ..\Test\main.cpp:3:0:
    convertable.h:19: candidate is: Convertable::operator QString() <near match>
    convertable.h:19: note: no known conversion for implicit 'this' parameter from 'const Convertable*' to 'Convertable*'
    main.cpp:8: error: passing 'const Convertable' as 'this' argument of 'Convertable::operator QString()' discards qualifiers [-fpermissive]


    Qt Code:
    1. return in1 + ": " + in2;
    2. ^
    To copy to clipboard, switch view to plain text mode 

  11. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QString conversion function

    Okay, try adding

    Qt Code:
    1. operator const QString() const
    2. {
    3. return c_string;
    4. }
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to Radek for this useful post:

    Bziur (5th March 2014)

  13. #9
    Join Date
    Mar 2014
    Posts
    9
    Thanks
    5
    Qt products
    Qt5

    Default Re: QString conversion function

    Sooo. It works.
    But then again if I uncomment the int and add const to it. I get the same ambiguous overload warning


    Added after 1 48 minutes:


    So Lessons learned today
    You shouldn't overload too many conversion functions as they lay confusion on c++ compilers when dealing with operators that accept multiple variable types. Stick to the necessary ones to avoid ambiguity and disarray.
    Last edited by Bziur; 5th March 2014 at 18:41.

Similar Threads

  1. Enum To QString Conversion
    By yagabey in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2012, 14:28
  2. BSTR to QString Conversion
    By bismitapadhy in forum Qt Programming
    Replies: 9
    Last Post: 16th February 2012, 12:51
  3. Conversion from QString to quint32
    By sksingh73 in forum Newbie
    Replies: 9
    Last Post: 2nd July 2010, 16:01
  4. QString Unicode conversion
    By user_mail07 in forum Qt Programming
    Replies: 5
    Last Post: 15th April 2010, 22:16
  5. QString iso 8859-1 conversion
    By mattia in forum Newbie
    Replies: 11
    Last Post: 21st January 2008, 14:17

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.