[SOLVED] Displaying Table from plain text file using QTableView
-
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 -
Well, if you want a table-kind display, go for the QTableView. If want a custom form designed by you, go for the QDataWidgetMapper.
-
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(&file); 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.