Results 1 to 13 of 13

Thread: Qstring conversion UTF8

  1. #1
    Join Date
    Jun 2010
    Posts
    41
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Question Qstring conversion UTF8

    I am trying to convert a Qstring to UTF8 for a mysql query. I am getting the following error.

    1>.\thewge.cpp(54) : error C2039: 'utf8' : is not a member of 'QString'
    1> c:\qt\4.6.2\include\qtcore\../../src/corelib/tools/qstring.h(91) : see declaration of 'QString'
    1>.\thewge.cpp(55) : error C2039: 'utf8' : is not a member of 'QString'
    1> c:\qt\4.6.2\include\qtcore\../../src/corelib/tools/qstring.h(91) : see declaration of 'QString'

    Qt Code:
    1. QString Qstr1 = ui.usrLineEdit->text();
    2. QString Qstr2 = ui.pwdLineEdit->text();
    3.  
    4. char *acsUserName = Qstr1.utf8();
    5. char *acsPassword = Qstr2.utf8();
    To copy to clipboard, switch view to plain text mode 

    I also tried this.
    Qt Code:
    1. QString Qstr1 = ui.usrLineEdit->text();
    2. QString Qstr2 = ui.pwdLineEdit->text();
    3.  
    4. char *acsUserName = Qstr1.toUtf8();
    5. char *acsPassword = Qstr2.toUtf8();
    To copy to clipboard, switch view to plain text mode 

    and got these errors:

    1>.\thewge.cpp(53) : error C2440: 'initializing' : cannot convert from 'QByteArray' to 'char *'
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    1>.\thewge.cpp(54) : error C2440: 'initializing' : cannot convert from 'QByteArray' to 'char *'
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Last edited by harleyskater; 24th June 2010 at 09:01.

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring conversion UTF8

    toUtf8() is a QByteArray, created temporary, that's why you can't do like this:
    Qt Code:
    1. const char *acsUserName = Qstr1.toUtf8().constData();
    To copy to clipboard, switch view to plain text mode 
    because at the next line acsUserName pointer will point to memory of destroyed temporary object.
    you only can send this construction to a function by argument like this:
    Qt Code:
    1. void foo( const char * userName, const char * password )
    2. {
    3. ...
    4. }
    5. ...
    6. foo( Qstr1.toUtf8().constData(), Qstr2.toUtf8().constData() );
    To copy to clipboard, switch view to plain text mode 
    or you should make new memory for this C-style string like this:
    Qt Code:
    1. char *acsUserName = new char[ Qstr1.length() + 1 ]; // + 1 for zero in the end of string
    2. memcpy( acsUserName, Qstr1.toUtf8().constData() );
    3. char *acsPassword = new char[ Qstr2.length() + 1 ]; // + 1 for zero in the end of string
    4. memcpy( acsPassword, Qstr2.toUtf8().constData() );
    5. foo( acsUserName, acsPassword );
    6. delete []acsUserName;
    7. delete []acsPassword;
    To copy to clipboard, switch view to plain text mode 

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

    harleyskater (24th June 2010)

  4. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qstring conversion UTF8

    Can You show code generating Yours SQL ? It is some strange what You are doing.
    I think that You want to insert record into SQL table.

  5. #4
    Join Date
    Jun 2010
    Posts
    41
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring conversion UTF8

    Lesiok, I am using mysqlpp to do a select statement.

    query << "SELECT username, password FROM db_user WHERE username = '" << acsUserName << "' and password = '" << md5(acsPassword) << "'";

    Borisbn, I will give your solution a try now

    The first solution I ran into a problem with the function and then chars inside the function not visable to the query string.

    the second solution i get this error:

    error C2660: 'memcpy' : function does not take 2 arguments

    Qt Code:
    1. QString Qstr1 = ui.usrLineEdit->text();
    2. QString Qstr2 = ui.pwdLineEdit->text();
    3.  
    4. //char *acsUserName = Qstr1.toUtf8();
    5. //char *acsPassword = Qstr2.toUtf8();
    6.  
    7. char *acsUserName = new char[ Qstr1.length() + 1 ]; // + 1 for zero in the end of string
    8. memcpy( acsUserName, Qstr1.toUtf8().constData() );
    9. char *acsPassword = new char[ Qstr2.length() + 1 ]; // + 1 for zero in the end of string
    10. memcpy( acsPassword, Qstr2.toUtf8().constData() );
    11.  
    12.  
    13. /* std::string acsUserName1 = Qstr1.toLatin1();
    14. std::string acsPassword1 = Qstr2.toLatin1();
    15.  
    16. const int kInputBufSize = 100;
    17. char acsUserName[kInputBufSize];
    18. char acsPassword[kInputBufSize];
    19.  
    20. ToUTF8(acsUserName, kInputBufSize, acsUserName1);
    21. ToUTF8(acsPassword, kInputBufSize, acsPassword1); */
    22.  
    23.  
    24. query << "SELECT username, password FROM db_user WHERE username = '" << acsUserName << "' and password = '" << md5(acsPassword) << "'";
    To copy to clipboard, switch view to plain text mode 

    I will research memcopy to see if I can find the a problem
    Last edited by harleyskater; 24th June 2010 at 18:53.

  6. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring conversion UTF8

    memcpy() requires the length, so use strcpy() which doesn't as long as your string does not contain embedded nulls (which username and password should not anyway)

  7. The following user says thank you to squidge for this useful post:

    harleyskater (24th June 2010)

  8. #6
    Join Date
    Jun 2010
    Posts
    41
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring conversion UTF8

    QString Qstr1 = ui.usrLineEdit->text();
    QString Qstr2 = ui.pwdLineEdit->text();

    char *acsUserName = new char[ Qstr1.length() + 1 ]; // + 1 for zero in the end of string
    memcpy( acsUserName, Qstr1.toUtf8().constData(),Qstr1.length() + 1 );
    char *acsPassword = new char[ Qstr2.length() + 1 ]; // + 1 for zero in the end of string
    memcpy( acsPassword, Qstr2.toUtf8().constData(), Qstr2.length() + 1 );

    this compiles but my code still isn't working properly. I'm kind of annoyed because I can not create breakpoints in Visual Studios when using QT. It makes it difficult to problem solve but I know its not executing properly because I am checking the number of rows returned from my query is not greater than zero


    Qt Code:
    1. if (res.num_rows() > 0)
    2. {
    3. qApp->exit(0);
    4. //goodlogin
    5. }
    6. else
    7. {
    8. //badlogin
    9. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring conversion UTF8

    If you can't do breakpoints in Visual Studios when using QT then your VS installation is broken.

    Secondly, since your converting to UTF8, the number of bytes to copy might be more than the number of characters in the string, so you can't use Qstr1.length() (one character could be upto 4 bytes in the output). This is why a QString is an array of QChar and not Char.

    try and use qDebug for your string

  10. #8
    Join Date
    Jun 2010
    Posts
    41
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring conversion UTF8

    I will reinstall my VS 2005, the breakpoints work great in non-qt apps, I also opened up a thread in the newbie qt to get some help with this issue.

    is QT compatible with 2008 express?

  11. #9
    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 UTF8

    Since you are using Qt and making a single atomic use of the UTF8 strings I think that:
    Qt Code:
    1. char *acsUserName = new char[ Qstr1.length() + 1 ]; // + 1 for zero in the end of string
    2. memcpy( acsUserName, Qstr1.toUtf8().constData() );
    3. char *acsPassword = new char[ Qstr2.length() + 1 ]; // + 1 for zero in the end of string
    4. memcpy( acsPassword, Qstr2.toUtf8().constData() );
    5. foo( acsUserName, acsPassword );
    6. delete []acsUserName;
    7. delete []acsPassword;
    To copy to clipboard, switch view to plain text mode 
    is probably more elegantly put without intervening temporary variables and manual memory shuffle:
    Qt Code:
    1. foo( Qstr1.toUtf8().constData(), Qstr2.toUtf8().constData() );
    To copy to clipboard, switch view to plain text mode 
    The temporary QByteArray object scope extends to the end of the foo() call. If you need to make multiple uses of the UTF8 string:
    Qt Code:
    1. QByteArray username = QStr1.toUtf8();
    2. QByteArray password = QStr2.toUtf8();
    3. foo( username.constData(), password.constData() );
    4. bar( username.constData() );
    To copy to clipboard, switch view to plain text mode 
    and let QByteArray worry about NUL termination, memory allocation, and necessary size.

  12. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring conversion UTF8

    Quote Originally Posted by harleyskater View Post
    is QT compatible with 2008 express?
    Qt is, but Express editions of VS have no support for plugins, so you will have to do everything yourself.

  13. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qstring conversion UTF8

    Because I'm lazy all SQL queries I have making like this :
    Qt Code:
    1. QSqlQuery query( *db );
    2.  
    3. query.prepare("SELECT username, password FROM db_user WHERE username = :uname and password = :passwd";
    4. query.bindValue( ":uname", uname );
    5. query.bindValue( ":passwd", passwd );
    6. query.exec();
    To copy to clipboard, switch view to plain text mode 
    where uname and passwd are QString. With this SQL driver is worrying about correct building of query.

  14. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstring conversion UTF8

    If you don't know if the query is being build correctly, send it to the debug console using query.lastQuery()

  15. #13
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qstring conversion UTF8

    Quote Originally Posted by fatjuicymole View Post
    If you don't know if the query is being build correctly, send it to the debug console using query.lastQuery()
    I know if the query is build correctly or no . What I want to say is that using QSqlQuery::prepare is a good solution for problems with ie. string coding. I don't need to know about code page used by connection to server.

Similar Threads

  1. BSTR to QString Conversion
    By bismitapadhy in forum Qt Programming
    Replies: 9
    Last Post: 16th February 2012, 12:51
  2. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  3. qreal -> QString conversion
    By MarkoSan in forum Newbie
    Replies: 2
    Last Post: 13th April 2008, 14:37
  4. QString iso 8859-1 conversion
    By mattia in forum Newbie
    Replies: 11
    Last Post: 21st January 2008, 14:17
  5. UTF8 to ISO 8859... conversion
    By claudio in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2007, 21:24

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.