Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Problem with QFIle
Forum Updated to NodeBB v4.3 + New Features

[Solved] Problem with QFIle

Scheduled Pinned Locked Moved General and Desktop
16 Posts 4 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bareil76
    wrote on last edited by
    #1

    Hi all,

    I have a problem with QFIle. In my hex.h file I have the following private variables

    @private:
    QString HexFilePath;
    QFile *HexFilePtr;@

    Then in my hex. c file which includes
    @#include <QFileDialog>
    #include "hex.h"
    #include <QFile>@

    I have

    @HexFilePath = QFileDialog::getOpenFileName(0, ("Open File"), NULL, ("hex File(*.hex)"));
    HexFilePtr->setFileName(HexFilePath);
    @

    However, the second line will give me a read access error . What am I doing wrong. If instead I do the following
    @QFile file;
    HexFilePath = QFileDialog::getOpenFileName(0, ("Open File"), NULL, ("hex File(*.hex)"));
    file.setFileName(HexFilePath);
    HexFilePtr=&file;@

    it works be I get the same problem when accessing HexFilePtr in other functions later.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Presumably you are not allocating memory for QFile anywhere.
      [quote author="bareil76" date="1383307520"]
      @private:
      QString HexFilePath;
      QFile HexFilePtr;@
      [/quote]
      You declared the pointer of QFile.
      [quote author="bareil76" date="1383307520"]
      @HexFilePath = QFileDialog::getOpenFileName(0, ("Open File"), NULL, ("hex File(
      .hex)"));
      HexFilePtr->setFileName(HexFilePath);
      @
      [/quote]
      This uses the pointer, but you did not show that you allocated memory. You got a pointer pointing somewhere. That gives you the error message.
      [quote author="bareil76" date="1383307520"]
      @QFile file;
      HexFilePath = QFileDialog::getOpenFileName(0, ("Open File"), NULL, ("hex File(*.hex)"));
      file.setFileName(HexFilePath);
      HexFilePtr=&file;@
      [/quote]
      The first line is actually providing the memory. Therefore, it can work so far. However, you are storing a pointer to a local object. As soon as it looses the focus, it will be deleted. Afterwards the pointer is pointing something, but no longer to the object.

      Familiarize yourself a bit with the "new":http://www.cplusplus.com/reference/new/ and delete statements of C++.

      Rewriting your code slightly:
      @QFile file;
      HexFilePath = QFileDialog::getOpenFileName(0, ("Open File"), NULL, ("hex File(*.hex)"));
      HexFilePtr=&file;
      HexFilePtr->setFileName(HexFilePath);
      @

      The above code should work as it does at the moment in your code.

      The following code will probably crash already
      @
      {
      QFile file;
      HexFilePath = QFileDialog::getOpenFileName(0, ("Open File"), NULL, ("hex File(*.hex)"));
      HexFilePtr=&file;
      } // focus lost to file and the object will be destroyed
      HexFilePtr->setFileName(HexFilePath);
      @

      This should work again
      @
      {
      QFile file = new QFile;
      HexFilePath = QFileDialog::getOpenFileName(0, ("Open File"), NULL, ("hex File(
      .hex)"));
      HexFilePtr=file;
      } // focus lost to pointer file
      HexFilePtr->setFileName(HexFilePath);
      @
      Only the pointer file will be destroyed but the object remains in memory.

      [edit, copy paste error corrected, koahnig]

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bareil76
        wrote on last edited by
        #3

        I had made the QFile *File static but I prefer your solution..

        thanks.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hi, if this is solved, place [SOLVED] in front of your first post, that safes others from reading!

          Greetz, Jeroen

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bareil76
            wrote on last edited by
            #5

            The following line

            @QFile *file = new QFile;@

            gets me those errors

            C2065: 'DEBUG_NEW' : undeclared identifier

            C2146: syntax error : missing ';' before identifier 'QFile'

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

              What are the lines above this code?? It says that it's missing something BEFORE this QFile!! Not after it, or on this line. It just encounters code that should be preceded by a ; The compiler is unable to determine where the ; is missing.

              Greetz, Jeroen

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bareil76
                wrote on last edited by
                #7

                @bool Hex::LoadHexFile(void)
                {
                int iRet;
                char HexRec[255];

                QFile *file = new QFile;
                @

                ...then the rest of the file.

                1 Reply Last reply
                0
                • JeroentjehomeJ Offline
                  JeroentjehomeJ Offline
                  Jeroentjehome
                  wrote on last edited by
                  #8

                  Ok,
                  Your QFile may not be without arguments!
                  so
                  @
                  QFile * file = new QFile(this); // Is valid
                  QFile * file = new QFile (SomeFileString); // Is Valid
                  QFile * file = new QFile; // NOT valid!!
                  @
                  You can also see that Qtcreator itself signals the problem by not highlighting the QFile after the new!!

                  Greetz, Jeroen

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    bareil76
                    wrote on last edited by
                    #9

                    Sorry... but it is not working. I still get the same error. The only way I found was to make file a static QFile.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #10

                      [quote author="koahnig" date="1383309034"]
                      @
                      {
                      QFile file = new QFile (this);
                      HexFilePath = QFileDialog::getOpenFileName(0, ("Open File"), NULL, ("hex File(
                      .hex)"));
                      HexFilePtr=file;
                      } // focus lost to pointer file
                      HexFilePtr->setFileName(HexFilePath);
                      @
                      [/quote]

                      Did you use this example above or something similar?

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        bareil76
                        wrote on last edited by
                        #11

                        I use the example above. I also tried other things.

                        Right now I have declared static QFile *file and then assign it to HexFilePtr. This is the only way to make things work right now.

                        I don't think we can pass "this" into QFile constructor

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          NicuPopescu
                          wrote on last edited by
                          #12

                          bq. Ok,
                          Your QFile may not be without arguments!
                          so

                          @QFile * file = new QFile(this); // Is valid
                          QFile * file = new QFile (SomeFileString); // Is Valid
                          QFile * file = new QFile; // NOT valid!!@

                          imho that's not true, see QFile class definition: it has a default constructor with no arguments even if is not listed in doc

                          bq. I don’t think we can pass “this” into QFile constructor

                          yes you can and the QFile object will be distroyed automatically by its parent destructor

                          1 Reply Last reply
                          0
                          • N Offline
                            N Offline
                            NicuPopescu
                            wrote on last edited by
                            #13

                            is bool Hex::LoadHexFile(void) declared as static in class definition?

                            1 Reply Last reply
                            0
                            • B Offline
                              B Offline
                              bareil76
                              wrote on last edited by
                              #14

                              Thanks for the infos.

                              bool Hex::LoadHexFile(void) is public and is not declared static.

                              Weird really!

                              1 Reply Last reply
                              0
                              • N Offline
                                N Offline
                                NicuPopescu
                                wrote on last edited by
                                #15

                                no, it shouldn't be ... it must work as expected
                                could you paste your class definition?

                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  bareil76
                                  wrote on last edited by
                                  #16

                                  @#ifndef HEX_H
                                  #define HEX_H

                                  #pragma once
                                  //#include "stdio.h" #include <stdio.h>
                                  #include <QObject>
                                  #include "QFile"

                                  typedef struct
                                  {
                                  unsigned char RecDataLen;
                                  unsigned int Address;
                                  unsigned int MaxAddress;
                                  unsigned int MinAddress;
                                  unsigned char RecType;
                                  unsigned char* Data;
                                  unsigned char CheckSum;
                                  unsigned int ExtSegAddress;
                                  unsigned int ExtLinAddress;
                                  }T_HEX_RECORD;

                                  // Hex Manager class
                                  class CHexManager
                                  {

                                  public:
                                  unsigned int HexTotalLines;
                                  unsigned int HexCurrLineNo;
                                  bool ResetHexFilePointer(void);
                                  bool LoadHexFile(void);
                                  bool LoadHexFileChannel(void);
                                  unsigned short GetNextHexRecord(char HexRec, unsigned int BuffLen);
                                  unsigned short ConvertAsciiToHex(void VdAscii, void VdHexRec);
                                  void VerifyFlash(unsigned int
                                  StartAdress, unsigned int
                                  ProgLen, unsigned short
                                  crc);

                                  //Constructor
                                  CHexManager()
                                  {
                                      //HexFilePtr = NULL;
                                  }
                                  //Destructor
                                  ~CHexManager()
                                  {
                                      // If hex file is open close it.
                                  
                                  }
                                  

                                  private:
                                  QString HexFilePath;
                                  QFile *HexFilePtr;

                                  };

                                  #endif // HEX_H@

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved