Results 1 to 4 of 4

Thread: Please help with an "if statement" condition

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Please help with an "if statement" condition

    Hi there, I want to compare two strings in an if statement condition as follows
    Qt Code:
    1. if(meta->className() == "MyClass"){ //then do something}
    To copy to clipboard, switch view to plain text mode 
    but the problem I am having is that meta->className() returns a string of type const char* which is incompatible to QString. Because of this, the "if" condition does not even evaluate instead it gives a warning - see attached picturewarning.png

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

    Default Re: Please help with an "if statement" condition

    Qt Code:
    1. if(QString(meta->className()) == "MyClass"){ //then do something}
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Please help with an "if statement" condition

    Quote Originally Posted by ayanda83 View Post
    but the problem I am having is that meta->className() returns a string of type const char* which is incompatible to QString.
    The other operand is also const char*, there is no QString involved here.

    Comparison of character arrays can be done via strcmp() functions such as http://qt-project.org/doc/qt-5/qbytearray.html#qstrncmp

    Cheers,
    _

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

    Default Re: Please help with an "if statement" condition

    Quote Originally Posted by ayanda83 View Post
    Hi there, I want to compare two strings in an if statement condition as follows
    Qt Code:
    1. if(meta->className() == "MyClass"){ //then do something}
    To copy to clipboard, switch view to plain text mode 
    but the problem I am having is that meta->className() returns a string of type const char* which is incompatible to QString. Because of this, the "if" condition does not even evaluate instead it gives a warning - see attached picturewarning.png
    Qt4/Qt5 way:

    Qt Code:
    1. if(meta->className() == QLatin1String("MyClass")) { ... }
    To copy to clipboard, switch view to plain text mode 

    Qt5 only way:
    Qt Code:
    1. if(meta->className() == QStringLiteral("MyClass")) { ... }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 7th March 2014, 06:47
  2. Replies: 4
    Last Post: 5th March 2010, 18:03
  3. Replies: 3
    Last Post: 15th February 2010, 17:27
  4. Replies: 3
    Last Post: 8th July 2008, 19:37
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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.