Results 1 to 5 of 5

Thread: Qstring convert to...

  1. #1
    Join Date
    Nov 2013
    Posts
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Qstring convert to...

    Hi Guys,

    I'm working with Qt since 2 weeks now and it's really interesting to me. My first task is to read out a image(raw-file) and to display it. So it take a while, but now I made some progress. I included the raw-file as a resource to my programm, made a QImage with an 8bit grayscale colourmap and read the file data to a string. Oh I forgot, the raw-file is very simple and contains only pixeldata, the size is about 131072 signs and this value fits to the resolution of 512x256 pixels. The pixels are stored as ascii-signs within the file, I was able to read the pixel value(0-255) with an hex-editor and guess it is ascii.
    So the next step is to read out the file-data to a QString(or similar) and convert it to int or const char(value 0-255). Therefore I wrote this code:

    QFile file(":/new/1.raw");
    if(file.open(QIODevice::ReadOnly|QIODevice::Text)) {qDebug("File opened");}
    else{qDebug("File not opened");}
    QTextstream out(&file);
    QString str= out.readAll();

    Now I'm really not sure about the datatype of str and how to get my pixel value's or even create an output for the ascii data on a label. Hope someone can give me a hint on this?

    Best reagards,

    Olli

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qstring convert to...

    Here is an example
    Qt Code:
    1. QString inputData = "FF00F100B200FF00";
    2. QList<int> outputData;
    3.  
    4. for(int i = 0; i < inputData.size(); i+=2)
    5. outputData << inputData.mid(i, 2).toUInt(0, 16);
    6.  
    7. qDebug() << outputData; // will print (255, 0, 241, 0, 178, 0, 255, 0)
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2013
    Posts
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qstring convert to...

    Thanks alot for the reply! I tested your code and it works, like you say. There is only one problem left, I dont got the pixel value in hex "FF" for "255". I got f.ex. "÷" that means value "247". Is there is a way to change the input format? But anyway it's a good hint for me!

    Best regards,

    Olli

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qstring convert to...

    In that case read the file in binary mode (instead of text mode) and read into QByteArray
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Nov 2013
    Posts
    10
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qstring convert to...

    thx I'll take a look on it!

Similar Threads

  1. How convert Qt::Key to QString?
    By lenny in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2012, 12:26
  2. convert Qstring into int
    By sabbu in forum Newbie
    Replies: 8
    Last Post: 8th June 2011, 16:31
  3. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 09:10
  4. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 08:07
  5. How to convert Int to QString in QT4?
    By drake1983 in forum Newbie
    Replies: 2
    Last Post: 11th March 2007, 06:58

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.