PDA

View Full Version : Qt DataGridView & Objects



janck
19th February 2014, 08:12
Hello!

I want to create Qt program with similar layout as C# DataGridView. I'm choosing between QTableWidget and QTableView - which one should be more suitable? In C# it's practice to create class for DGV and list where are multiple objects from that class stored. How to achieve the same in Qt?

Thank you!

anda_skoa
19th February 2014, 09:15
A QTableWidget is a QTableView that stores the data in itself.

So it mostly depends on how you plan on accessing the data and how much data you have.

Cheers,
_

janck
19th February 2014, 09:31
I want to store music playlist into table, the file has average 1000 lines of plain text. It is possible to create objects like in C# and push it into some list?

anda_skoa
19th February 2014, 12:19
You can do that with both widgets so my suggestion would be to try QTableWidget first, it is generally easier to get used to.

Cheers,
_

janck
19th February 2014, 14:41
Ok. In C# I need some class in which I store parts of data and that data is almost independent of application. When data is imported from file there's just a call from datagridview to source which is for example list with class objects. How to do the same with Qt? Can you please provide some example code? I'm searching for example of that for almost three hours and I can't find anything since I'm not familiar with Qt. Thank you!

anda_skoa
19th February 2014, 18:33
There are two approaches:

1) QTableWidget: something that loads the data create QTableWidgetItem instances and adds them to the widget

2) QTableView: a class derived from QAbstractTableModel provides access to the data by providing a row count, a column count and data for a given row/column index when being asked for it.

Which one of the two do you want to go with?

Cheers,
_

janck
19th February 2014, 21:52
I started working with QTableWidget and it seems pretty similar to C# DGV. The main problem on which I encountered was defining of rows & columns. I can't define them before I put data into list or something like that, because I can't guess how many lines is in file. If there is no defined rows & columns at the start, QTableWidgetItem doesn't appear in QTableWidget when adding it. Another thing is that I would like to put parts of data into class's properties because I need to save/export it into file. What do I have to do now?

It seems that there's a big difference between Qt and Visual C#... I didn't expect that :) Thank you for help!

anda_skoa
20th February 2014, 09:00
If there is no defined rows & columns at the start, QTableWidgetItem doesn't appear in QTableWidget when adding it.

See insertRow()



Another thing is that I would like to put parts of data into class's properties because I need to save/export it into file. What do I have to do now?

Two options:
1) QTableWidgetItem::setData()
2) Derive you own class fom QTableWidgetItem, use whatever variables you need for your data

Cheers,
_