Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting only a particular set of values from a text file!

Getting only a particular set of values from a text file!

Scheduled Pinned Locked Moved Solved General and Desktop
qtcreatorqtextstreamqfilecombobox
3 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lasith
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      FloatFlower.Huang
      wrote on last edited by
      #2

      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.

      L 1 Reply Last reply
      2
      • F FloatFlower.Huang

        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.

        L Offline
        L Offline
        Lasith
        wrote on last edited by
        #3

        @FloatFlower.Huang Thanx alot mate

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved