Results 1 to 4 of 4

Thread: Char* <-> QString implicit conversion

  1. #1
    Join Date
    Jan 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Char* <-> QString implicit conversion

    Hey there,

    for some reason, if i have a function which has QString Arguments and I pass it a char*, sometimes it works and sometimes I get an Error?

    Why is this and how to work around?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Char* <-> QString implicit conversion

    I get an Error?
    What error?
    Can you paste it here?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Char* <-> QString implicit conversion

    The function declaration:
    Qt Code:
    1. QList<QStringList> select(QString& from, QString& where, QStringList* what=0);
    To copy to clipboard, switch view to plain text mode 

    How I call it:
    Qt Code:
    1. QList<QStringList> dirs = svh->select("dirs", QString().setNum(dir->getId()));
    To copy to clipboard, switch view to plain text mode 

    The Error I get:
    Qt Code:
    1. /home/finn/client_gallery/dirparser.cpp:24: error: no matching function for call to ‘SqlViaHttp::select(const char [5], QString&)’
    To copy to clipboard, switch view to plain text mode 


    //EDIT:
    Ahh, ok, I know so the solution.
    I have to use QString instead of QString& in the Declaration.
    Why is that?
    Last edited by kingfinn; 12th March 2010 at 17:51.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Char* <-> QString implicit conversion

    I have to use QString instead of QString& in the Declaration.
    Why is that?
    You don't have to change the declaration, you can change the way you use call the function.
    You declared it with a reference, so you need to pass a reference, but you have passed a literal string.
    Your declaration will work if you call it like this:
    Qt Code:
    1. QString strDirs = "dirs";
    2. QList<QStringList> dirs = svh->select(strDirs , QString().setNum(dir->getId()));
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  2. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  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. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13:12

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.