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. [SOLVED] Displaying Table from plain text file using QTableView
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Displaying Table from plain text file using QTableView

Scheduled Pinned Locked Moved General and Desktop
8 Posts 6 Posters 10.0k Views 1 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.
  • A Offline
    A Offline
    amban
    wrote on last edited by
    #1

    Hi,
    I am retrieving information in the form of plain text files and i want to just display these tables in a neat way - which of the above is a better way. Any other suggestions ?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      QTableView displays a model as tabel. QDataWidgetMapper ampps the data of a model to a bunch of widgets.
      So displaying data in a table is a typical task of QTableView and a "QAbstractTableModel":http://doc.qt.nokia.com/4.7/qabstracttablemodel.html derived class. But you have to parse your text and create the model. "See here for an example inside Qt documentation":http://doc.qt.nokia.com/4.7/model-view-programming.html

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sfilippidis
        wrote on last edited by
        #3

        Well, if you want a table-kind display, go for the QTableView. If want a custom form designed by you, go for the QDataWidgetMapper.

        https://www.filippidis.name/

        1 Reply Last reply
        0
        • H Offline
          H Offline
          HuXiKa
          wrote on last edited by
          #4

          If you are just displaying data parsed from a file, the I think QTableView is a better choice.

          If you can find faults of spelling in the text above, you can keep them.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            amban
            wrote on last edited by
            #5

            Thanks for all your suggestions. I decided to use QTableView.
            Following a previous post http://www.qtcentre.org/threads/17377-QTableView-memory-consumption , i have come up with the following code :
            @
            Widget::Widget(QWidget *parent) :
            QWidget(parent),
            {

            QTableView *mytable = new QTableView();
            QStandardItemModel *mytablemodel = new QStandardItemModel();
            mytablemodel->setRowCount(0);
            
            int Max_num_of_Columns(8);
            int Max_Number_of_Lines(0);
            mytablemodel->setColumnCount(Max_num_of_Columns);
            
            QFile file("/path/to/txt/file");
            if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
                qDebug() << "Error opening file";
            
            QTextStream InputDataFile&#40;&file&#41;;
            while (!InputDataFile.atEnd())
            {
            
                QString line = InputDataFile.readLine();
                QStringList fields = line.split(" ");
            
                if (fields.size() == Max_num_of_Columns)
                {
                    for (int column=0; column< Max_num_of_Columns; column++)
                    {
                        QStandardItem *item = new QStandardItem(fields[column]);
                        mytablemodel->setItem(Max_Number_of_Lines, column, item);
                    }
                    Max_Number_of_Lines++ ;
                }
            
            
            }
            
            file.close();
            
            mytable->setModel(mytablemodel);
            mytable->show();
            

            }
            @

            I am unable to follow the model/view very well. Please help me figure out how to make this work. Just read a plain text file and display it in a tabular fashion.

            Thanks a lot.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              amban
              wrote on last edited by
              #6

              This code works fine. It was messed up because of irregular spaces.
              I used @ str = "Some text\n\twith strange whitespace.";
              list = str.split(QRegExp("\s+"));@ to remove them.

              Hope this helps. Thanks.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KierraHar
                wrote on last edited by
                #7

                Did you ever get this program to work because I can't seem to...where would i put:
                @
                str = "Some text\n\twith strange whitespace.";
                list = str.split(QRegExp("\s+"));
                @

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andreyc
                  wrote on last edited by
                  #8

                  You should put these lines at line 21 in the example above
                  It should look like
                  @
                  QString line = InputDataFile.readLine();
                  QStringList fields = line.split(QRegExp("\s+"));
                  @

                  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