QTableView, QStandardItemModel and underlying data in a QList
-
That's why I wrote "put everything in your QStandardItemModel" and you would only use that as data source.
Where else do you use your data structure ?
On a side note, as static variable for that is not a good idea.
-
@SGaist: Thank you for your answer, I want to be more precise:
Imagine that I have Qlist as my data source, with 100 Persons.
I have two different qTableViews, first with all persons, and second with persons user can decide. Thatswhy I thought that it is good idea tio have one source of truth: my Qlist. First qtable reads all persons in. You can mark a person, and then (reference) of that object is transfered to that second qtableview. Addiotioonally to that, there will be more and dynamic amount of qtableviews for that. That approach would effectively reduce my amount of memory.
I have to think about your approach, if it is possible. I don't know yet.
I have an external variable there, not static variable. Reason is: I have several widget classes, and I want to share that qlist with all of them by their functions. Do you have a better approach? I tried to make it "friend" once, and use a free function, but due to an error I couldn't handle that. As a noob there are so many things to learn, and sources of errors are endless :) -
Hi
To fix the sorting issues, you could use
https://doc.qt.io/qt-5/qstandarditem.html#setData
and put the Index of the person to a UserRole so that
you can use that to look up the person it represents.
However, that is only valid if you don't add or remove persons
while the view is active.However, im not sure that a QStandardItem model will make you very happy in the long run if you
plan to allow the user to edit a Person and need to write back the changed data to the
Persons list. For that, a QAbstractTableModel would be much more smooth.
You could reuse the model from
https://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-example.html
as its very close to what you have ( Contact vs Person) -
Hi!
Thank you very much for your help. I thought and I want to make little steps at first, so I changed my struct:
// person.h struct person { int id; QStandardItem* name; QStandardItem* family; }
Now I can directly use my members and append them.
I do not need write access, but to show my objects directly in QAbstractTableModel is indeed a sexy idea. I am trying around with QModelIndex, club_model->item(), club_model->itemData() and roles, and when I am done with that, maybe I'll try my own model.For now, my question is solved!
Regards Gerhard
-
The code in the starting post almost works, you just have to let
QSortFilterProxyModel
do the sorting.Club::Club(QWidget *parent) : QWidget(parent), uiclub(new Ui::Club) { club_model = new QStandardItemModel(this); sort_proxy = new QSortFilterProxyModel(this); sort_proxy->setSourceModel(club_model); uiclub->tableView(setModel(sort_proxy); QStringList header; header << "ID" << "Name" << "Family Name"; club_model->setHorizontalHeaderLabels(header); }
void Club::on_tableView_activated(const QModelIndex &index) { const int mappedRow = sort_proxy->mapToSource(index).row(); qDebug() << "user clicks on index row" << mappedRow ; do-something_with_person(const_cast<Person&>(list_of_people.at(mappedRow))); }
-
@VRonin Qt::ItemFlags GAS_tool::flags(const QModelIndex& index) const
{
//table 1 column 9 and 10 as noneditable
// QModelIndex index;if (index.column() == 9 || index.column() == 10) return ~Qt::ItemIsEditable; else return Qt::ItemIsEditable;
}
how should i make table 1 and table 2 column as non editable using flag? -
-
@n-2204
You can use @VRonin'ssetFlags()
if you want to set the editability on desired, explicitly specified items.If you want to do it your way by overriding
flags()
, your code should have read:if (index.column() == 9 || index.column() == 10) return QStandardItemModel::flags() & ~Qt::ItemIsEditable; else return QStandardItemModel::flags() | Qt::ItemIsEditable;
Your way would have switched off enablement and selectable.
You will have to derive from
QStandardItemModel()
if you are wanting tooverride
itsflags()
method. Similar if you choose to useQAbstractItemModel()
instead. -
@JonB said in QTableView, QStandardItemModel and underlying data in a QList:
noneditable is class extracting Qstandarditemmodel
Qt::ItemFlags nonedittablemodel::flags(const QModelIndex& index) const{
//table 1 if (index.column() == 0 || index.column() == 1) return QStandardItemModel::flags(index) & ~Qt::ItemIsEditable; else return QStandardItemModel::flags(index) | Qt::ItemIsEditable;
}
But still its not working for me
and where i should specify which model because i have 3 model 3tableview -
@n-2204 Qt::ItemFlags GAS_tool:: flags(const QModelIndex& index) const
{
//table 1
QStandardItemModel model;
if (index.column() == 0 || index.column() == 1)
return model:: flags(index) & ~Qt::ItemIsEditable;
else
return flags(index) | Qt::ItemIsEditable;
}@JonB where i should give like it will be applicable for table1 or table2 -
@n-2204
Please try to format your posts readably. For lines of code use the Code tag when posting, or but lines of 3-backticks above & below.You are also now asking this same question in a new thread you have created (https://forum.qt.io/topic/125774/qtableview-how-to-make-noneditable-column). You should stick to one thread, not create multiple ones.
where i should give like it will be applicable for table1 or table2
I don't know what you mean. To use the
flags()
approach you must sub-classQStandardItemModel
, which I assume is what yourGAS_tool
is, else this will have no effect.If you want to use the
setFlags()
approach, you do not need to sub-class, but must set the flags explicitly on each item which is to be non-editable.I also wrote this in your other thread.