QAbstractItemModel with QTableView
-
Hello again.
I created a model class which inherits QAbstractItemModel, I have a list of data there. and I also have a tableView. I want the data to be displayed on the view, exactly the same items as in model, not an exstra rows or columns.
I think I have a problem with model's rowCount() and columnCount() methods, I cannot set the number of rows and columns correctly. for example I have 3 objects in data list and I want to have a table with 4 columns, when I add items from view I want the rows to add after it reaches the last column in previous row. how can I solve it? do you have any idea?thank you!!!
-
Hi!
for your purpose easy use the QAbstractTableModel
but in both cases when you inherits your own class from Abstract class
you must- describe some data structure to describe your model, for example
QVector <QVector <QVariant> > m_data;
- reimplement virtual methods such as rowCownt(),columnCount(), data(), setData() appenRows(), removeRows() and so one
And you yourself must describe policy of your model
E.g.
@int MyModel::columnCount()
{
if (m_data.size())
return m_data.at(0).count;
else
return 3;
}@@int MyModel::rowCount()
{
return m_data.size();
}@I'm correctly understood your question?
-
[quote author="tokafr" date="1421255724"]Hello again.
I created a model class which inherits QAbstractItemModel, I have a list of data there. and I also have a tableView. I want the data to be displayed on the view, exactly the same items as in model, not an exstra rows or columns.
I think I have a problem with model's rowCount() and columnCount() methods, I cannot set the number of rows and columns correctly. for example I have 3 objects in data list and I want to have a table with 4 columns, when I add items from view I want the rows to add after it reaches the last column in previous row. how can I solve it? do you have any idea?thank you!!![/quote]
So, you want your items to basically wrap around, like an exporer window does when displaying files as icons right?
That is easy: that is just displaying a list-type model on a QListView. However, the list view needs to be set in the right mode to display like that. Set it to IconMode and you're in business.
-
Selecting should work. Do you have the right selection mode set on the view?
The size of the items I think (not sure, don't use it often) can be set in several ways. You can use the sizeHintRole from the model, use a delegate, or use the setGridSize method on the QListView.
If your items are all the same size, it's a good idea to call setUniformItemSizes(true) as well for performance reasons.
-