Results 1 to 9 of 9

Thread: QString conversion function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Thanks
    5

    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.

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