Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-75899

QDataStream setDevice() doesn't work on a default contructed QDataStream Object

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 5.12.3
    • Core: I/O
    • None
    • MinGW 64 bit (GCC 8.1.0)
      Windows 10 64 bit pro
    • Windows

    Description

      Here is a simple program I wrote, that, in theory, should compile without issues. I create an object using the QDataStream Default constructor.

       

      #include <QtCore/QDataStream>
      #include <QtCore/QIODevice>
      #include <QtCore/QFile>
      int main( int argc, char **argv )
      {
       QDataStream out_stream(); // we will serialize the data into the file
       QFile file("file.dat");
       file.open(QIODevice::WriteOnly);
       out_stream.setDevice(&file);
      }
       
      

      When attempting to compile this program, I get the following error:

      datastream_test.cpp: In function 'int main(int, char**)':
      datastream_test.cpp:12:16: error: request for member 'setDevice' in 'out_stream', which is of non-class type 'QDataStream()'
           out_stream.setDevice(&file);
                      ^~~~~~~~~

       

      This error makes no sense, as out is initialized as a QDataStream object. From what I can tell, calling the QDataStream constructor without passing an argument results in  an object ("out_stream") that is not initialized as a QDataStream object.

       

      The only way I can use setDevice successfully is by passing a QIODevice into the QDataStream constructor. This is demonstrated in the below code, which compiles without issues:

       

      #include <QtCore/QDataStream>
      #include <QtCore/QIODevice>
      #include <QtCore/QFile>
      int main( int argc, char **argv )
      {
       QFile dummy_file("asdf.dat");    //dummy that is used to initialize the datastream object
       dummy_file.open(QIODevice::WriteOnly);
       QDataStream out_stream(); // we will serialize the data into the file
       QFile file("file.dat");
       file.open(QIODevice::WriteOnly);
       out_stream.setDevice(&file);
      }
      

       

       

      This workaround gets Qt to (correctly) recognize "out_stream" as a QDataStream object. However, calling the QDataStream Constructor without passing arguments does not result in an object that is recognized as a QDataStream object.

       

       

      When I create a default constructed QDatastream object and try to call setDevice() on it, I get

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            jrj99 jrj99
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes