Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
project code is ..

C#
QFile logfile;
bool Candump::openConnection()
{
    QString CandumpFileName = "C:/xamDevelopment/readfiles/P_1.trc";
    QFile logfile(CandumpFileName);

    if (!logfile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        logger()->error("Error occured while opening CANdump file");
    }
QTimer *CanIOtimer = new QTimer(this);
    CanIOtimer->start(20);
    connect(CanIOtimer, SIGNAL(timeout()), this, SLOT(ReadTextFileData()));
}


Slot is as following

void Candump::ReadTextFileData()
{
 qDebug()<<logfile.isOpen(); //here file is going to close. I want file opened in this slot
}


how i can do this?? please share your experience and knowledge..

What I have tried:

I want to in following step
1: open file
2:start timer
3: connect sink timeout() with slot ReadTextFileData()

in ReadTextFileData
read one line
Posted
Updated 23-Jun-16 4:35am
v2

you must make the logFile a member of the class. The normal way is to declare it in the header or class interface.

Be careful in error handling: check whether the file exists and is open and close it somewhere (destructor).

Tip: Dont use hard code file names and path
 
Share this answer
 
Comments
Member 12587659 23-Jun-16 8:47am    
thanks for your suggestion
I did bbut i am getting error
no match for call to '(qfile) (qstring &)'
in .h FILE -> qFile logfile;
in .cpp file ->
QString CandumpFileName = "C:/XamDevelopment/readfiles/P_1.trc";
logfile(CandumpFileName); //getting error on this line
Karsten is right that you should make the QFile a member of a class instead of using a global instance.

But the real problem is that you have a global instance declared on top and use a local instance with the same name in your openConnection function. So the global instance never gets a file name assigned and is never opened.

It should work this way:
C++
#include <qfile>

// Better make this a member of your Candump class
QFile logfile;

bool Candump::openConnection()
{
    QString CandumpFileName = "C:/xamDevelopment/readfiles/P_1.trc";
    // This creates a new (local) QFile instance with the same name as the global one.
    //QFile logfile(CandumpFileName);
    // Use the global / class instance instead:
    logfile.setFileName(CandumpFileName);
 
    if (!logfile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        logger()->error("Error occured while opening CANdump file");
    }
    // ...
}</qfile>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900