June 22, 2011

amban amban
Lab Rat
76 posts

[SOLVED] Displaying Table from plain text file using QTableView

 

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 ?

5 replies

June 22, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

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 [doc.qt.nokia.com] derived class. But you have to parse your text and create the model. See here for an example inside Qt documentation [doc.qt.nokia.com]

 Signature 

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

June 22, 2011

Stavros Filippidis Stavros Filippidis
Ant Farmer
354 posts

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

June 22, 2011

HuXiKa HuXiKa
Lab Rat
83 posts

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

 Signature 

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

June 22, 2011

amban amban
Lab Rat
76 posts

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 :

  1. Widget::Widget(QWidget *parent) :
  2.     QWidget(parent),
  3. {
  4.  
  5.     QTableView *mytable = new QTableView();
  6.     QStandardItemModel *mytablemodel = new QStandardItemModel();
  7.     mytablemodel->setRowCount(0);
  8.  
  9.     int Max_num_of_Columns(8);
  10.     int Max_Number_of_Lines(0);
  11.     mytablemodel->setColumnCount(Max_num_of_Columns);
  12.  
  13.     QFile file("/path/to/txt/file");
  14.     if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
  15.         qDebug() << "Error opening file";
  16.  
  17.     QTextStream InputDataFile(&file);
  18.     while (!InputDataFile.atEnd())
  19.     {
  20.  
  21.         QString line = InputDataFile.readLine();
  22.         QStringList fields = line.split(" ");
  23.  
  24.         if (fields.size() == Max_num_of_Columns)
  25.         {
  26.             for (int column=0; column< Max_num_of_Columns; column++)
  27.             {
  28.                 QStandardItem *item = new QStandardItem(fields[column]);
  29.                 mytablemodel->setItem(Max_Number_of_Lines, column, item);
  30.             }
  31.             Max_Number_of_Lines++ ;
  32.         }
  33.  
  34.  
  35.     }
  36.  
  37.     file.close();
  38.  
  39.     mytable->setModel(mytablemodel);
  40.     mytable->show();
  41.  
  42. }

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.

June 22, 2011

amban amban
Lab Rat
76 posts

This code works fine. It was messed up because of irregular spaces.
I used

  1.  str = "Some  text\n\twith  strange whitespace.";
  2.      list = str.split(QRegExp("\\s+"));
to remove them.

Hope this helps. Thanks.

 
  ‹‹ [SOLVED] QTcpSocket successfully connects, but not to my server. What is it connecting to?      How dynamic plugins work with resources? ››

You must log in to post a reply. Not a member yet? Register here!