Results 1 to 3 of 3

Thread: trying to read from a qfile to a unsigned short array

  1. #1
    Join Date
    Mar 2011
    Location
    West Jordan, Utah
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default trying to read from a qfile to a unsigned short array

    I have a binary image 1024 x 1024 or X x Y lets say, and want to save the data in a ushort array, how do I do that when all of the file.read methods use char* ?

  2. #2
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: trying to read from a qfile to a unsigned short array

    Have you looked at QDataStream? It allows like for instance

    Qt Code:
    1. QFile file("text.txt", ...);
    2. if (!file.open()) {
    3. ....
    4. }
    5.  
    6. QDataStream str(&file);
    7. qint16 val << str;
    8. ....
    9. ....
    10. ....
    To copy to clipboard, switch view to plain text mode 
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

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

    vmsgman (26th March 2011)

  4. #3
    Join Date
    Mar 2011
    Location
    West Jordan, Utah
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: trying to read from a qfile to a unsigned short array

    So I tried that and it seems to work, but I am getting some weird results from the function, the ushorts are 0 from index 0-1023 which is expected, 1024 = ~3000 also ok, 1025,6,7 etc, are ~45000 which is wrong, they should be ~3000 as well.

    Do you see anything wrong with the code?

    Qt Code:
    1. void MainWindow::on_actionOpen_activated()
    2. {
    3. QString filename = QFileDialog::getOpenFileName(
    4. this,
    5. tr("Open ViVA File"),
    6. QDir::currentPath(),
    7. tr("ViVA Image (*.viv *.raw)") );
    8.  
    9. if( !filename.isNull() )
    10. {
    11. qDebug( filename.toAscii() );
    12.  
    13. viv_header *hdr = new viv_header();
    14. int32_t xDim = 0, yDim = 0;
    15. QFile file(filename);
    16. if (file.open(QIODevice::ReadOnly))
    17. {
    18. if (filename.endsWith(".viv"))
    19. {
    20. qint64 bytes = file.read(reinterpret_cast<char *>(hdr), sizeof(viv_header));
    21. if (bytes != sizeof(viv_header)) {
    22. QMessageBox::information(this, tr("ViVA Header Error"),
    23. "The ViVA header is malformed file cannot be read!");
    24. return;
    25. }
    26. xDim = hdr->ImgWidth;
    27. yDim = hdr->ImgHeight;
    28. }
    29. else
    30. {
    31. image_size *i;
    32. for (i = known_image_sizes; i->width != -1; i++)
    33. {
    34. if(i->width * i->height * sizeof(unsigned short) == file.size())
    35. break;
    36. }
    37. if(i->width == -1)
    38. {
    39.  
    40. QMessageBox::information(this, tr("File Dimension Error"),
    41. "The file dimensions are not a recognized length!\n");
    42. return;
    43. }
    44. xDim = i->width;
    45. yDim = i->height;
    46.  
    47. if (file.pos() != 0)
    48. file.seek(0);
    49. }
    50.  
    51. uint64_t len = xDim * yDim;
    52. ia = new quint16[len];
    53.  
    54. QDataStream in(&file);
    55. for (uint64_t j = 0; j < len; j++)
    56. in >> ia[(int)j];
    57.  
    58. file.close();
    59. }
    60. }
    61. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How store a unsigned short into a database
    By franco.amato in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2011, 22:17
  2. converting QByteArray to unsigned short
    By sattu in forum Qt Programming
    Replies: 16
    Last Post: 28th September 2010, 13:51
  3. Replies: 5
    Last Post: 9th April 2007, 14:26
  4. Tiff in unsigned short
    By spawnwj in forum Qt Programming
    Replies: 14
    Last Post: 26th July 2006, 07:41
  5. How to create QPixmap from unsigned character array?
    By rashidbutt in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 18:25

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.