ok, i don't give up...
let me summarize the problem, I have defined a simple procedure outside classes as follows qloadini.cpp :
#include <QtCore>
#include <QtGui>
//****************************************************************
{
while (!is.atEnd()) {
s = is.readLine();
{
param = s.split( "=" );
if ( param[0] == sParam ) ret.push_back( param[1] );
}
}
}
if (ret.size()==0) ret.push_back( "Error loading: " + param[0] );
return ret;
file.close();
}
//****************************************************************
{
while (!is.atEnd()) {
s = is.readLine();
{
param = s.split( "=" );
if ( param[0] == sParam )
{
file.close();
return param[1];
}
}
}
}
file.close();
return "Error loading: " + sParam;
}
#include <QtCore>
#include <QtGui>
//****************************************************************
QStringList qLoadIniVecStr( const QString sParam )
{
QString s;
QStringList param;
QStringList ret;
QString fileName = "efrei.ini";
QFile file( fileName );
if (file.open(QIODevice::ReadOnly)) {
QTextStream is(&file);
while (!is.atEnd()) {
s = is.readLine();
if (s[0]!=QChar('#'))
{
param = s.split( "=" );
if ( param[0] == sParam ) ret.push_back( param[1] );
}
}
}
if (ret.size()==0) ret.push_back( "Error loading: " + param[0] );
return ret;
file.close();
}
//****************************************************************
QString qLoadIniStr( const QString sParam )
{
QString s;
QStringList param;
QString fileName = "efrei.ini";
QFile file( fileName );
if (file.open(QIODevice::ReadOnly)) {
QTextStream is(&file);
while (!is.atEnd()) {
s = is.readLine();
if (s[0]!=QChar('#'))
{
param = s.split( "=" );
if ( param[0] == sParam )
{
file.close();
return param[1];
}
}
}
}
file.close();
return "Error loading: " + sParam;
}
To copy to clipboard, switch view to plain text mode
The idea is to externalize constants and load them from an ini file when I need them.
Then I have have the qloadini.h :
#ifndef QLOADINI_H
#define QLOADINI_H
#include <QStringList>
#endif
#ifndef QLOADINI_H
#define QLOADINI_H
#include <QStringList>
QString qLoadIniStr( const QString sParam );
QStringList qLoadIniVecStr( const QString sParam );
#endif
To copy to clipboard, switch view to plain text mode
Finally I call this procedure from various GUI classes with :
TxtError = qLoadIniStr( "Unknown Error" );
ErrHttp = qLoadIniStr( "ErrHttp" );
// here for instance i don't even bother to check what is returned, the QMessageBox
// displays the default error parameter if the retrieve filed.
TxtError = qLoadIniStr( "Unknown Error" );
ErrHttp = qLoadIniStr( "ErrHttp" );
QMessageBox::warning( this, ErrHttp, TxtError );
// here for instance i don't even bother to check what is returned, the QMessageBox
// displays the default error parameter if the retrieve filed.
To copy to clipboard, switch view to plain text mode
As the qloadini procs are defined they return some message that tells me whether I was able to retrieve the constant or not. The problem is that I then have to handle the errors in the calling procedure which is cluttering the code. What I would have liked is to be able to display a QMessageBox inside the qloadini procedures themselves.
It would be a generic message telling that a constant parameter is not retrieved and that the ini file has something wrong with it.
But I don't know how to pass this "this" parameter so that the qloadini QMessageBox can address the current window. I could dupplicate qloadini in all classes that call it but that would hardly be an improvement.
Bookmarks