Solved Help with QML TableView
-
Hi,
I am new to Qt and QML and want to make a table view, which has two columns:
- parameter name ( a string)
- parameter value (editable)
The parameter value could be an integer, float, string or bool value. For the data model I tried this code:
class mystandardmodel: public QStandardItemModel { public: mystandardmodel(); enum Role { role1=Qt::UserRole, role2 }; explicit mystandardmodel(QObject * parent = 0): QStandardItemModel(parent){} //explicit mystandardmodel( int rows, int columns, QObject * parent = 0 ) // : QStandardItemModel(rows, columns, parent){} QHash<int, QByteArray> roleNames() const{ QHash<int, QByteArray> roles; roles[role1] = "one"; roles[role2] = "two"; return roles; } };
Is this how you would make the model or is there a better way? Also, is it possible to make a model where an item is a struct defined by me?
Here is how I try to fill the model with an example parameter:
mystandardmodel* mysmodel=new mystandardmodel(0); QStandardItem* it = new QStandardItem(); it->setData("data1", mystandardmodel::role1); it->setData(true, mystandardmodel::role2);
As far as I understand, the data-type is always a QVariant, so depending on what I provide as the first argument in setData it will be handled correctly?
Here my example TableView:
TableView { id: tableView2 x: 121 y: 317 TableViewColumn { title: "Parameter Name" role: "one" } TableViewColumn { title: "Value" role: "two" } model: myTestModel }
Now, I would like to make the parameter value editable. Also, I would like to have bool values represented as a checkbox. Ideally, a string parameter value could be a drop-down list of possible values. How could this be achieved?
Thank you for your help!
-
I wanted to do something similar recently. I noticed tableview is not in qt controls 2.0 and only in 1.x. I found a post suggesting tableview has performance issues and one might be better off using listview. This is worth mentioning as it may mean you will have more luck finding examples for functionally achieving what you are after by searching for multi column listview and editing listview.
-
Thank you to mention this. I think performance will not be an issue for me, as I will have only around 20 entries.
-
To have different delegates for different types I now used the following code:
TreeView { id: tableView2 x: 69 y: 316 width: 318 height: 150 TableViewColumn { title: "Parameter Name" role: "one" } TableViewColumn { title: "Value" role: "two" //delegate: myDelegate } model: myTestModel } Component { id: myDelegate Loader { property var roleTwo: model.two sourceComponent: if(typeof(roleTwo)=='boolean') { checkBoxDelegate} else { stringDelegate} } } Component { id: checkBoxDelegate CheckBox{text: roleTwo} } Component { id: stringDelegate Text {text: roleTwo } }
How can I make this model editable?
-
Following this it works
https://stackoverflow.com/questions/49529314/edit-qstandarditemmodel-via-tableview-with-custom-delegateThe solution is to simply call an updateValue function in the delegate, e.g. for the string delegate
onEditingFinished: {
myTestModel.updateValue(thisIndex, text, "two")
} -
@AFuzzyBadger Thank you so much. After searching the whole google and forum results about why qt creator didnt understand the tableview, i found your comment!
Thanks again :)