Results 1 to 10 of 10

Thread: char* to QString: conversion

  1. #1
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Question char* to QString: conversion

    Hi

    While calling dll file from vb.net I am passing a string value to the qt dllas follows:
    Qt Code:
    1. <DllImport("G:\Varen_CS\02_Projects\01_Softwares\highFreqTrad\test\debug\test.dll", _
    2. CharSet:=CharSet.Auto, _
    3. CallingConvention:=CallingConvention.StdCall)> _
    4. Private Shared Function _ZN11QtEngineDll7randIntEv(ByRef u As String) As String
    5. End Function
    To copy to clipboard, switch view to plain text mode 
    Now my qtcode goes as follows:

    Qt Code:
    1. QtEngineDll::QtEngineDll(const char* url)
    2. {
    3. m = url;
    4. }
    5.  
    6. const char* QtEngineDll::randInt()
    7. {
    8. QByteArray d(m);
    9. QString result = d.data();
    10. QString fileName = QDir::currentPath() + "/Test.txt";
    11. QFile file(fileName);
    12. file.open(QIODevice::Append | QIODevice::Text);
    13. file.write(result + "\n");
    14. return result;
    15. }
    To copy to clipboard, switch view to plain text mode 

    But the file output is something like this:
    [07/03/10 - 12:03:46], Abhijit U‰åVSƒìpE¸‰$èT Code:
    1. [07/03/10 - 12:03:46], Abhijit U‰åVSƒìpE¸‰$èT
    To copy to clipboard, switch view to plain text mode 

    Can you pls tell me what is the wrong that I am committing in the code?

    Thanks in advance.

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: char* to QString: conversion

    QByteArray stores raw bytes (including '\0's) and traditional 8-bit '\0'-terminated strings.
    You can't get Unicode character whit it.

  3. #3
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: char* to QString: conversion

    Use instead QString::fromUtf8 ( const char * str, int size = -1 ) .
    Read the Qt doc.

  4. #4
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Default Re: char* to QString: conversion

    Thanks toutarrive.

    However if I use the following:
    Qt Code:
    1. const char* test = "Abhijit";
    2. QByteArray d(test);
    3. result = QString::number(d.size());
    4. QString fileName = QDir::currentPath() + "/Test.txt";
    5. QFile file(fileName);
    6. file.open(QIODevice::Append | QIODevice::Text);
    7. file.write(QString(test) + "\n");
    8. return result;
    To copy to clipboard, switch view to plain text mode 

    everything is going fine. But if instead of defining test in the program itself, if I pass test value as an parameter from vb.net then some absurd result is thrown. Can anybody put some light on this?

    Thanks in advance.
    Last edited by abghosh; 7th March 2010 at 09:57. Reason: I was wrong

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

    Default Re: char* to QString: conversion

    Can You guarantee that in QtEngineDll::randInt() pointer delivered to QtEngineDll constructor is steel point to this same data ?
    Maybe it will be better to create QByteArray object in QtEngineDll constructor.

  6. #6
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Default Re: char* to QString: conversion

    Hi Lisiok

    Can u pls demonstrate the same in this context.

    Any way thnks for suggesting a better method.

  7. #7
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: char* to QString: conversion

    I don't see in your code where you are handling the unicode characters.

  8. #8
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Default Re: char* to QString: conversion

    Hi

    The problem as far as I have found is in passing the parameter may it be string or integer. The argument value is not at all passed properly. Even if I passed some integer value it is taking it as 1 and not the value passed actually.
    Cant understand whether it is a problem in my vb.net call or the dll function definition.

    I have attached the files required for building the DLL file in Qt 4. Note that the entry point name is taken from the dll file reader.

    If I have committed some mistake in making the DLL file can you pls modify it.


    Thanks in advance.
    Attached Files Attached Files

  9. #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: char* to QString: conversion

    I'm not familiar with mating .Net code to straight C++ code, but it seems that the parameter list here:
    Qt Code:
    1. Private Shared Function _ZN11QtEngineDll7randIntEv(ByRef u As String) As String
    To copy to clipboard, switch view to plain text mode 
    (one parameter) doesn't match the one here:
    Qt Code:
    1. const char* QtEngineDll::randInt()
    To copy to clipboard, switch view to plain text mode 
    (no parameter, but probably requires an implied this pointer)
    Any value passed by VB.Net in the u parameter has nowhere to go in the C++ (except perhaps being misinterpreted as a this pointer).

    Other things to ponder: Your randInt() tries to dump a string set in the constructor to a file, and not a parameter passed to the method. Where is your object constructed? How is VB.Net setting a "this" pointer when it calls the randInt() method?

  10. #10
    Join Date
    Feb 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Windows

    Default Re: char* to QString: conversion

    Hi ChrisW67

    The function mentioned by you is the older one. I have recently attached the new file where I just want to pass some integer value.
    Passing integer value is also having error.

    What I now think is to pass the parameter using some pointer. But I have no idea about reading pointer variables in Qt. Can you pls put some light on the same.

    Thanks in advance.

Similar Threads

  1. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  2. qreal -> QString conversion
    By MarkoSan in forum Newbie
    Replies: 2
    Last Post: 13th April 2008, 14:37
  3. QString iso 8859-1 conversion
    By mattia in forum Newbie
    Replies: 11
    Last Post: 21st January 2008, 14:17
  4. Conversion from unsigned char* to unsigned char
    By santosh.kumar in forum General Programming
    Replies: 1
    Last Post: 6th August 2007, 13:12
  5. conversion from QString to const uint8 *
    By vishal.chauhan in forum Qt Programming
    Replies: 3
    Last Post: 19th February 2007, 13:14

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.