Getting only a particular set of values from a text file!
Solved
General and Desktop
-
In my QT c++ program I want to load a set of ages into the combo box from a text file when the application is opened! The text file has a set of names also! I want only to get the set of ages into the combo box but I have no idea!
following is my code
{
ui->setupUi(this);QFile file(QString("%1/Details.txt").arg(QCoreApplication::applicationDirPath())); qDebug() << QCoreApplication::applicationFilePath(); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); while (!in.atEnd()) { QString age = in.readLine(); if(age=="Ages"){ ui->comboBox->addItem( age); } } }
following is contents of my text file!
Names
John
Smith
Marie
Anne
Jonathan
Michael
<break>Ages
5
10
15
20
<break>for my code I only get [Age] in my combo box but I want to get 5,10,15,20 in the combo box! How can Adjust my code to achieve it?
-
Maybe you can try this
QTextStream in(&file); QString sectionName; while (!in.atEnd()) { QString age = in.readLine(); if (age == "Ages") { sectionName = "Ages"; continue; } if (sectionName == "Ages") { ui->comboBox->addItem(age); } }
The other solution is that use uniform format like JSON(http://doc.qt.io/qt-5/qjsondocument.html) or XML(http://doc.qt.io/qt-5/qtxml-module.html) to format your code.
-
@FloatFlower.Huang Thanx alot mate