Seite 1 von 1

Zeilenweises Einlesen einer txt-Datei in eine QComboBox

Verfasst: 5. Oktober 2008 18:52
von timo_81
Hallo,

ich möchte eine Datei zeilenweise abarbeiten und die Zeilen in eine ComboBox übernehmen. Es kommt kein Fehler, es wird aber auch nichts eingetragen. Hier ist mein Ansatz:

Code: Alles auswählen


//Datei einlesen

ifstream genopakDaten("C:\Code\mit_genopak.txt");
string zeile;
int i=0;
while(getline(genopakDaten,zeile))//zeilenweise einlesen
 {
QString(zeile);
comboBox_2->insertItem(i,zeile); 
i++;
 } 

Finde jemand den Fehler?

Verfasst: 5. Oktober 2008 18:57
von MiKla
Geht es damit?

Code: Alles auswählen

ifstream genopakDaten("C:\\Code\\mit_genopak.txt"); 

Verfasst: 5. Oktober 2008 19:56
von timo_81
Hey,
nun kann ich durch die leere ComboBox scrollen...
Vorher konnte ich nicht durch die CombBox scrollen...
an was könnte das liegen?

Danke für die Hilfe bislang!

Verfasst: 5. Oktober 2008 22:30
von MiKla
Das hier funktioniert bei mir, vorrausgesetzt die Datei ist vorhanden!

Code: Alles auswählen

	QFile file("c:\\tmp\\test.txt"); 
	QString line;
	if (file.exists()){
		if (!file.open(QFile::ReadOnly | QFile::Text)) {
			QMessageBox::warning(this, tr("Datei oeffnen"),
								 tr("Kann Datei %1: nicht lesen\n%2.")
								 .arg("c:\\tmp\test.txt")
								 .arg(file.errorString()));
		}else{
			QTextStream in(&file);		
			while (!in.atEnd()) {
				line = in.readLine();
				comboBox->addItem(line);
			}
		}
	}

Verfasst: 6. Oktober 2008 13:23
von timo_81
Danke schön!!
Hat super geklappt!!

Verfasst: 6. Oktober 2008 14:54
von timo_81
Nun wollte ich es so erweitern, dass ich mehrere Dateien einlesen kann.
Mein Ansatz war wie folgt:

Code: Alles auswählen


 QFile file("c:\\Code\\adressen.txt");
 QFile file2("c:\\Code\\mit_genopak");
 QFile file3("c:\\Code\\pauschal.txt");
 
 QString line;
 QString line2;
 QString line3;
 
 file.open(QFile::ReadOnly) ; 
 file2.open(QFile::ReadOnly) ; 
 file3.open(QFile::ReadOnly) ; 
  
 QTextStream in(&file);      
    while (!in.atEnd()) {
           line = in.readLine();
           comboBox->addItem(line);
         }
 QTextStream in(&file2);      
    while (!in.atEnd()) 
	     {
           line2 = in.readLine();
           comboBox_2->addItem(line2);
		   comboBox_3->addItem(line3);
          }

	QTextStream in(&file3);      
    while (!in.atEnd()) 
	     {
           line3 = in.readLine();
           comboBox_4->addItem(line3);
		   
         }
         };
  
error C2374: 'in': Neudefinition; Mehrfachinitialisierung
irgendeinen Tipp?

Verfasst: 6. Oktober 2008 15:18
von MiKla
dann mach doch:

Code: Alles auswählen

			while (!in.atEnd()) {
				line = in.readLine();
				comboBox->addItem(line);
			}
			QTextStream in2(&file2);     
			while (!in2.atEnd())
				{
				   line2 = in2.readLine();
				   comboBox->addItem(line2);
				 comboBox->addItem(line3);
				  }

			QTextStream in3(&file3);     
			while (!in3.atEnd())
				{
				   line3 = in3.readLine();
				   comboBox->addItem(line3);
		         
				 } 

Verfasst: 6. Oktober 2008 16:22
von timo_81
Danke ich dachte in wäre eine Art Schlüsselwort.
Habs kapiert vielen Dank!!

Verfasst: 7. Oktober 2008 22:29
von Undefined
Also noch Umständlicher gehts wohl nicht mehr.
Leute wir sind bei Qt :wink:

Code: Alles auswählen

QComboBox box = new QComboBox;
QFile fp("C:\Code\mit_genopak.txt");
if ( fp.open( QIODevice::ReadOnly ) )
{
  QTextStream stream ( &fp );
  QString data = stream.readAll();
  box->addItems( data.split( QRegExp( "[\\n\\r]+" ) ) );
  fp.close();
}
Und wenn du die Daten prüfen möchtest kannst du das mit einer foreach machen.