PDA

View Full Version : QStringList



dragon
4th June 2007, 17:35
Hello anyone,

I have Qt-4.2.3 on Windows XP
The problem is QStringList
I want to read a line with string items in QStringList through a Dialog .
I did this with QtSql and it works fine but i don't want to use QtSql.
I want to use a file.

mainwindow.cpp


oid MainWindow::zoek()
{
zoekDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {

QString treksterkte, koeling, snijsnelheid, voedingpertand, snedediepte, snedebreedte,

QString materiaal = dlg.matComboBox->currentText();
QString freestype = dlg.freesComboBox->currentText();
QString diameter = dlg.diaComboBox->currentText();
QString tand = dlg.tandComboBox->currentText();
QString bewerking = dlg.bewerkComboBox->currentText();

QFile file("C:/Qt/Qt-prog/richtwaarden/frees.txt");
if( !file.open(QFile::ReadOnly))
return;

QTextStream stream( &file);
QString line;
do {

line = stream.readLine();
if(!line.isEmpty())
{
QStringList frees = line.split('\t', QString::SkipEmptyParts);
QStringList::iterator it = frees.begin();
if(it != frees.end())
{
if ( *it == materiaal )
leMateriaal->setText(*it); //put the text in the linedit.
++it;
}
}
}while(!line.isEmpty());


file.close();
}

This works but he only find one string materiaal.
I want to find all the string items wich i choose in the comboboxes in the zoekdialog.

Thanks in advance

marcel
4th June 2007, 20:29
OK. What is the desired behavior when you put the same text in the line edit( lines 29&30).
Have you verified the text file and see what is in it?
What about if you print( with qDebug ) what readLine returns? What is the result of that?

Regards

dragon
8th June 2007, 17:26
Sorry for delay for reply i was on hollyday.

first the frees.txt file have 11 string items on the row seperated with tabs.
see example.
Koolstofstaal 350-850 EC 5 3 Ja Contour frezen 80-100 0.015-0.030 max. 2xD <2.5
Gereedschapstaal 800-1200 EC 8 3 Ja Gleuf frezen 80-100 0.015-0.030 max. 2xD <2.5
These file will be updated with 600 rows.

In my zoekDialog i have 5 comboboxes in each combobox different items.
When i choose items i want to show the items in Mainwindow lineedits.
example:
combobox 1 = Koolstofstaal
combobox 2 = EC
combobox 3 = 5
combobox 4 = 3
combobox 5 = Contour Frezen
The rest of the row in frees.txt must fill up in the other Mainwindow lineedits.

The problem is the iterator on stringlist show only 1 string item.
Normally i want to use in if 5 statements but the pick only 1 the first one
See example.
if ( mat == dlg.matComboBox->currentText() && frees == dlg.freesComboBox->currenText() && etc ... ) every statement must be true.

see MainWindow.cpp


void MainWindow::zoek()
{
zoekDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) { // Open a Dialog with 5 Comboboxen filled with items.

QString treksterkte, koeling, voedingpertand, snijsnelheid, snedediepte, snedebreedte;
QString mat = dlg.matComboBox->currentText(); //Choose a item in the combobox and put in the string mat.
QString frees = dlg.freesComboBox->currentText();
QString tand = dlg.tandComboBox->currentText();
QString dia = dlg.diaComboBox->currentText();
QString bewrk = dlg.bewerkComboBox->currentText();

QFile file("C:/Qt/Qt-prog/richtwaarden/frees.txt"); //Open the file with strings seperated with tabs.
if( !file.open(QFile::ReadOnly))
return;

QTextStream stream( &file);
QString line;
do {

line = stream.readLine();
if(!line.isEmpty())
{
QStringList frees = line.split('\t', QString::SkipEmptyParts); // Make a Stringlist
QListIterator<QString> i(frees); // Iterate over the Stringlist frees.
while (i.hasNext())
{

if ( mat == i.next()) // It search for that item in the Stringlist en put the item
leMateriaal->setText( mat ); // in the Mainwindow Lineedit.
if ( freestype == i.next())
leFrees->setText( freestype ); // This don't work
}
}
}while(!line.isEmpty());


file.close();
}


B.T.W. i did this with QtSql and QTSqlQuerry no problem works fine.
see little example of code:


void MainWindow::zoek()
{
zoekDialog dlg(this);
if( dlg.exec() == QDialog::Accepted ) {

QString materiaal;

QSqlQuery query("SELECT * FROM frezen WHERE materiaal='" + dlg.matComboBox->currentText() + "'AND freesdiameter='" + dlg.diaComboBox->currentText() + "'AND freestype='" + dlg.freesComboBox->currentText() + "'AND aantaltanden='" + dlg.tandComboBox->currentText() + "'AND freesbewerking='" + dlg.bewerkComboBox->currentText() + "';");
if (query.next())
materiaal= query.value(0).toString();
leMateriaal->setText(materiaal); QString treksterkte = query.value(1).toString();
leTrek->setText(treksterkte);
QString freestype = query.value(2).toString();
leFrees->setText(freestype);
QString freesdiameter = query.value(3).toString();
leDiam->setText(freesdiameter);
QString aantaltanden = query.value(4).toString();
leAantal->setText(aantaltanden);
QString koeling = query.value(5).toString();
leKoeling->setText(koeling);
QString freesbewerking = query.value(6).toString();
leFreesb->setText(freesbewerking);
QString snijsnelheid = query.value(7).toString();
leSnijsnelheid->setText(snijsnelheid);
QString voedingpertand = query.value(8).toString();
leAanzet->setText(voedingpertand);
QString snedediepte = query.value(9).toString();
leSneded->setText(snedediepte);
QString snedebreedte = query.value(10).toString();
leSnedeb->setText(snedebreedte);

Maby i must stick with QTSql.

Thaks in advance.