PDA

View Full Version : prob with QFile and QTextStream class



bajarangi
27th June 2009, 00:57
I'm not a n00b to QT or to C++, but this should be a very simple
problem. I'm writing a log class with a global file pointer, and
calling the methods to print a string via QFile.write(char*, length)
always works, but the stream output (for QStrings) only works in
the constructor. I really can't see the problem.

Any help greatly appreciated.

class RMlog {
public:

RMlog(QString logname );
~RMlog();
void rmputf(const char* fmt, ...);
void rmputstr(const QString &str);
private:
QFile *logfile;
QTextStream ts;
};

extern RMlog *rmlogfile;

RMlog::RMlog(QString logname)
{
bool openstatus;
logfile = new QFile(logname);
logfile->open(QIODevice::WriteOnly)
ts.setDevice(logfile);
// works fine
ts << "Testing QTextStream output in RMLog constructor" << endl;
}

void RMlog::rmputstr(const QString &str)
{
// fail
ts << str;
}

void RMlog::rmputf(const char* fmt, ...)
{
va_list va;
va_start(va, fmt);
QString tmp;
tmp.vsprintf(fmt, va); // convert to QString
// works fine
logfile->write(tmp.toAscii().data(), m_text.length());
va_end(va);
}

RMLog *rmlogfile;

main()
{
rmlogfile = new RMlog("app.log");
QString temp = "Testing RMLog rmputstr in main with QString\n";
// no log file output
rmlogfile->putstr(temp);
}