PDA

View Full Version : How to use a file to setup a QProgressDialog, Thanks



HelloDan
21st February 2009, 02:14
Hi, all.
I want to read a plaintext file in my program, and show a QProgressDialog during reading. But i don't know how to detect the size of my file to set it up.

It must be a very simple question. But i have not idea about it. Could anyone help?
Thanks for any help!

talk2amulya
21st February 2009, 07:10
use size() of QFile..next, depending upon the number of bytes read compared to total size, call setValue()

HelloDan
21st February 2009, 12:16
use size() of QFile..next, depending upon the number of bytes read compared to total size, call setValue()

Thanks! I will try.

HelloDan
21st February 2009, 12:42
use size() of QFile..next, depending upon the number of bytes read compared to total size, call setValue()

That's a problem, is that any way to try my thinking in a console window? I want to try QProgressDialog in a console window.

Thanks

talk2amulya
21st February 2009, 13:05
why would u want a QProgressDialog in a console window?

HelloDan
21st February 2009, 13:12
Try to test if my code works.

Do you have any good ways to test?

HelloDan
21st February 2009, 13:14
why would u want a QProgressDialog in a console window?

Is it OK? I wrote it in this way, just for try



QFile file("in.txt");
QTextStream in(&file);

QProgressDialog progressDialog();
progressDialog.setCancelButtonText(tr("&Cancel"));
progressDialog.setRange(0, file.size());
progressDialog.setWindowTitle(tr("Loading..."));
QString line;
while(file.pos()<file.size())
{
progressDialog.setValue(file.pos());
progressDialog.setLabelText(tr("Searching file number %1 of %2...")
.arg(file.pos()).arg(file.size()));
qApp->processEvents();

if (progressDialog.wasCanceled())
break;
line=in.readLine();

}

talk2amulya
21st February 2009, 13:22
looks good..isnt it working?

HelloDan
21st February 2009, 13:49
looks good..isnt it working?

Thanks. I have try the code in my app. That segment of my code may be OK.
But that is still some problems.

1 I use QFileDialog::getOpenFileName to get a file. But i doesn't reveal immediately in the QComboBox, but it does in the comboBox which I can use my mouse to select it from the drop down list. I post my code in the following. Why?



void DataConvertor::inputFileNameBrowse()
{
QString initialName=inputFileNameComboBox->currentText();
if(initialName.isEmpty())
initialName=QDir::homePath();
QString inputFileName=QFileDialog::getOpenFileName(this,
tr("Open file"), initialName);
inputFileName=QDir::toNativeSeparators(inputFileNa me);


if(!inputFileName.isEmpty())
{
inputFileNameComboBox->addItem(inputFileName);
//when it's empty, the index is 0.
inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->currentIndex() + 1);
//inputFileNameComboBox->activated(inputFileNameComboBox->currentIndex());

// set output file the same location with the inputFile if it is empty.
if(outputFileDirectoryComboBox->currentText().isEmpty())
{
outputFileDirectoryComboBox->addItem(QDir::toNativeSeparators(QFileInfo(inputFi leName).path()));
outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->currentIndex() + 1);
//outputFileDirectoryComboBox->activated(outputFileDirectoryComboBox->currentIndex());
}

okButton->setEnabled(true); // enable the ok button
}
}

HelloDan
21st February 2009, 13:54
looks good..isnt it working?

2 This's problem is about the QProgressDialog. Only When I click the OK button for several times could my program works, it will turn up several QprogressDialogs. Also, That is not output file, I cann't find it.

Thanks!



DataConvertor::DataConvertor(QWidget *parent):QDialog(parent)
{
setupUi(this);
okButton->setEnabled(false); // disable OK button

connect(inputFileBrowseButton,SIGNAL(clicked()),th is,SLOT(inputFileNameBrowse()));
connect(outputFileBrowseButton,SIGNAL(clicked()),t his,SLOT(outputFileDirectoryBrowse()));
connect(aboutButton,SIGNAL(clicked()),this,SLOT(ab outinfo()));
connect(okButton,SIGNAL(clicked()),this,SLOT(proce ssFile()));

}


void DataConvertor::processFile()
{
QString fileName=inputFileNameComboBox->currentText();
if(fileName.isEmpty())
{
QMessageBox::information(this,tr("Data Convertor"),tr("Not such a file, Please reselect a file."),QMessageBox::Ok);
}
else
{
QFile file(fileName);
//Open read file.
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(this,tr("Data Convertor"),tr("Cann't read file %1:\n%2.").arg(file.fileName()).arg(file.errorString()),QMe ssageBox::Cancel);
return;
}
//Open write directory.
QString outDirName=outputFileDirectoryComboBox->currentText();
QDir outDir(outDirName);
///
//if the directory is not existed, create one.
if(!outDir.exists())
{
outDir.mkpath(outDirName);
}

QTextStream in(&file);

// form outfile name.
QString path=outDirName+QDir::separator()+"abc.txt";
QFile outFile(path);
QTextStream out(&outFile);

// setup QProgressDailog
QProgressDialog progressDialog(this);
progressDialog.setCancelButtonText(tr("&Cancel"));
progressDialog.setRange(0, file.size());
progressDialog.setWindowTitle(tr("Loading..."));
QString line;

while(file.pos()<file.size())
{
progressDialog.setValue(file.pos());
progressDialog.setLabelText(tr("Searching file number %1 of %2...")
.arg(file.pos()).arg(file.size()));
qApp->processEvents();

if (progressDialog.wasCanceled())
break;
line=in.readLine();
out<<line;
}
}
}

talk2amulya
21st February 2009, 14:09
although i dont understand clearly what ur problem is, but use:


void QComboBox::setEditText ( const QString & text ) [slot]

Sets the text in the combobox's text edit.

instead of setCurrentIndex() in both combo boxes..ur problem should solve

talk2amulya
21st February 2009, 14:13
dont disable OK button in DataConverter, disable it at the start of processFile() and enable it again at the end of it.

HelloDan
21st February 2009, 15:33
dont disable OK button in DataConverter, disable it at the start of processFile() and enable it again at the end of it.

Thanks very much! I had tried. It works.

But the QComboBox drop down list problem is still that.

By the way, how can i start a new line in the file, what about in this way: out<<line<<"\n";
I find the following code doesn't work properly, the data is out of order and miss some


while(file.pos()<file.size())
{
progressDialog.setValue(file.pos());
progressDialog.setLabelText(tr("Processing file number %1 of %2...")
.arg(file.pos()).arg(file.size()));
qApp->processEvents();

if (progressDialog.wasCanceled())
break;
line=in.readLine();
out<<line<<"\n";
}

talk2amulya
21st February 2009, 16:29
could u tell EXACTLY what the problem is..i m still not clear about it

HelloDan
21st February 2009, 16:52
could u tell EXACTLY what the problem is..i m still not clear about it



void DataConvertor::inputFileNameBrowse()
{
QString initialName=inputFileNameComboBox->currentText();
if(initialName.isEmpty())
initialName=QDir::homePath();
QString inputFileName=QFileDialog::getOpenFileName(this,
tr("Open file"), initialName);
inputFileName=QDir::toNativeSeparators(inputFileNa me);


if(!inputFileName.isEmpty())
{
inputFileNameComboBox->addItem(inputFileName);
//when it's empty, the index is 0.
inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->currentIndex() + 1);
//inputFileNameComboBox->activated(inputFileNameComboBox->currentIndex());

// set output file the same location with the inputFile if it is empty.
if(outputFileDirectoryComboBox->currentText().isEmpty())
{
outputFileDirectoryComboBox->addItem(QDir::toNativeSeparators(QFileInfo(inputFi leName).path()));
outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->currentIndex() + 1);
//outputFileDirectoryComboBox->activated(outputFileDirectoryComboBox->currentIndex());
}

okButton->setEnabled(true); // enable the ok button
}
}


In this function, after i use QFileDialog::getOpenFileName to select a file, the combox should contain the file path. But the QComboBox is empty. But I can find the file which i select using the mouse in the drop down list. At the same time, I set the same path to outputFileDirectoryComboBox, it was also hidden in the drop down list. Why?


By the way, how can i start a new line in the file, what about in this way: out<<line<<"\n";
I find the following code doesn't work properly, the data is out of order and miss some


Thanks

talk2amulya
21st February 2009, 17:02
ok, regarding combobox problem, use


inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->count() - 1);
and
outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->count() - 1);

for other problem,try


out<<line<<'\r\n'

HelloDan
21st February 2009, 17:24
ok, regarding combobox problem, use


inputFileNameComboBox->setCurrentIndex(inputFileNameComboBox->count() - 1);
and
outputFileDirectoryComboBox->setCurrentIndex(outputFileDirectoryComboBox->count() - 1);

for other problem,try


out<<line<<'\r\n'

Thanks! I had tried.

The drop down list one works well.

The output one in this way : out<<line<<"\r\n"; is more better.
In this way,the order is OK, but still miss the last part of the data.

talk2amulya
21st February 2009, 17:44
could you show the written file..i wanna see where it is missing..or u can use qDebug to print all lines that u r reading and showing...then show me the output

HelloDan
22nd February 2009, 01:30
could you show the written file..i wanna see where it is missing..or u can use qDebug to print all lines that u r reading and showing...then show me the output

Thanks!
for example, the inputfile format is :
20040101 C 0 C 0 C 0 ENE 10 -1 11 0 0 -1 11 0 0
20040102 WSW 10 NW 30 NNW 30 NNW 10 -1 7 0 0 -1 6 0 0
20040103 NNW 20 C 0 SSE 20 SE 30 -1 8 0 0 -1 0 0 0
20040104 C 0 NE 10 S 20 SSW 10 -1 7 6 0 -1 0 0 0
20040105 E 10 C 0 S 20 C 0 -1 9 0 0 -1 0 0 0
20040106 E 10 NNW 20 SW 10 NNW 10 -1 10 6 0 -1 10 0 0


but the output one is only one line when the input data size is small.:
20040101 C 0 C 0 C 0 ENE 10 -1 11 0 0 -1 11 0 0


How to use a qDebug() in a GUI programming? I can make it in a console window, but fail in a GUI one. Maybe it's to fast, how I can make it pause? like system("pause");

Thanks!

HelloDan
22nd February 2009, 03:18
while(file.pos()<file.size()) //-->
while(in.pos()<file.size())


Somebody had helped me to solve the problem.


By the way, it's that any way to reuse output file name and QTextStream name? Such as:

if(month!=list.at(0))
{
QFile monthFile(outDirName+QDir::separator()+list.at(0)+".txt");
QTextStream monthOut(&monthFile);
month=list.at(0);
}

monthOut<<list.at(0)<<'\t'<<list.at(1)<<'\t'<<list.at(2)<<"\n";

In my previous standard C++ code, I do it in this way:
while(getline(ifile, str))
{
filename = str.substr(0, 6)+".txt";
ofstream ofile(filename.c_str(), ios_base::app);
str.erase(0,10);
ofile << str << endl;
ofile.close();
}

But I do think that reopen and reclose a file in this way is not a wise choice. But i can not work out a better method.

There are lots of problem to learn in practical experiment. Thanks sincerely to all the friends help me.

wysota
22nd February 2009, 07:55
If you store the name in some variable, you can use it later. Or you can call QFile::fileName() to retrieve the name from the QFile object.