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. Populating a QcomboBox using lines from a file
Forum Updated to NodeBB v4.3 + New Features

Populating a QcomboBox using lines from a file

Scheduled Pinned Locked Moved Solved General and Desktop
qcombobox
6 Posts 3 Posters 5.4k Views 2 Watching
  • 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    I need to populate a QcomboBox from a text file using the following code:

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QtCore>
    #include <QtGui>
    #include <QMessageBox>
    #include <QCompleter>
    #include <QTextStream>
    #include <QComboBox>

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    ui->comboBox->setMaxVisibleItems (8);
    ui->comboBox->setEditable (true);
    
    QFile file("C:/Programming/widget/QCombobox/combo.txt");
    QTextStream stream(&file);
    QString line;
    
    if(file.open (QIODevice::ReadOnly | QIODevice::Text))
    {
    	if(!line.isNull ())
        {
    		line = stream.readLine ();
            ui->comboBox->addItem (line);
        }
    
        stream.flush ();
        file.close ();
    }
    

    }

    After running it, nothing happens. Please help me to find out what is wrong with it.
    Thank you.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      The readline is never executed. Change

          if(!line.isNull ())
          {
              line = stream.readLine ();
              ui->comboBox->addItem (line);
          }
      

      to

          line = stream.readLine ();
          if(!line.isNull ())
          {
              ui->comboBox->addItem (line);
          }
      

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        hi
        Well you read only first line (after @koahnig fix)

        QFile inputFile(fileName);
        if (inputFile.open(QIODevice::ReadOnly))
        {
          QTextStream in(&inputFile);
          while (!in.atEnd())
          {
             QString line = in.readLine();
            ui->comboBox->addItem (line);
          }
          inputFile.close();
        }
        
        K 1 Reply Last reply
        1
        • mrjjM mrjj

          hi
          Well you read only first line (after @koahnig fix)

          QFile inputFile(fileName);
          if (inputFile.open(QIODevice::ReadOnly))
          {
            QTextStream in(&inputFile);
            while (!in.atEnd())
            {
               QString line = in.readLine();
              ui->comboBox->addItem (line);
            }
            inputFile.close();
          }
          
          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @mrjj
          Sorry, to object, but even the first line is not read.
          However, your source looks certainly better for adding a couple of lines than mine. ;)

          Vote the answer(s) that helped you to solve your issue(s)

          mrjjM 1 Reply Last reply
          0
          • K koahnig

            @mrjj
            Sorry, to object, but even the first line is not read.
            However, your source looks certainly better for adding a couple of lines than mine. ;)

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @koahnig
            yeah I edited after I saw you post. I totally missed that not even first will be read :)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gabor53
              wrote on last edited by
              #6

              Thank you it worked.

              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