[Solved] Problem with QFIle
-
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.
-
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]
-
I had made the QFile *File static but I prefer your solution..
thanks.
-
Hi, if this is solved, place [SOLVED] in front of your first post, that safes others from reading!
-
The following line
@QFile *file = new QFile;@
gets me those errors
C2065: 'DEBUG_NEW' : undeclared identifier
C2146: syntax error : missing ';' before identifier 'QFile'
-
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.
-
@bool Hex::LoadHexFile(void)
{
int iRet;
char HexRec[255];QFile *file = new QFile;
@...then the rest of the file.
-
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!! -
Sorry... but it is not working. I still get the same error. The only way I found was to make file a static QFile.
-
[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?
-
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
-
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
-
is bool Hex::LoadHexFile(void) declared as static in class definition?
-
Thanks for the infos.
bool Hex::LoadHexFile(void) is public and is not declared static.
Weird really!
-
no, it shouldn't be ... it must work as expected
could you paste your class definition? -
@#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@