Results 1 to 4 of 4

Thread: setData() with QAbstractTableModel in QtQuick TableView

  1. #1
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default setData() with QAbstractTableModel in QtQuick TableView

    Hey all,

    I'm trying to connect a TableView in QtQuick Controls with a QAbstractTableModel in C++. The tableview correctly displays the model data, but I cannot figure out how to update the model data from the tableview. I've made a simple example in QML/C++ to illustrate my problem. There are 3 roles in the model, and a checkbox, a button, and a textfield in the tableview. I'd like to be able to write the checkbox state and the textfield text to the model. The model should be updated where the "*** Save X to model here! ***" comments are. Does any know how to do this?

    /* main.qml */
    Qt Code:
    1. import QtQuick 2.2
    2. import QtQuick.Controls 1.1
    3.  
    4. ApplicationWindow {
    5. id: applicationWindow1
    6. visible: true
    7. width: 640
    8. height: 480
    9. title: qsTr("Hello World")
    10.  
    11. menuBar: MenuBar {
    12. Menu {
    13. title: qsTr("File")
    14. MenuItem {
    15. text: qsTr("Exit")
    16. onTriggered: Qt.quit();
    17. }
    18. }
    19. }
    20.  
    21. TableView {
    22.  
    23. id: metadataTaskTable
    24. anchors.fill: parent
    25. alternatingRowColors: false
    26. model: theModel
    27.  
    28. // Row Delegate
    29. rowDelegate: Item {
    30.  
    31. height: 20 + 15
    32. Rectangle {
    33. anchors {
    34. left: parent.left
    35. right: parent.right
    36. verticalCenter: parent.verticalCenter
    37. }
    38. height: parent.height
    39. color: styleData.selected ? 'lightblue' : 'white'
    40. }
    41. }
    42.  
    43. // Column 0
    44. TableViewColumn {
    45. role: "role_enable";
    46. width: 25;
    47. resizable: false;
    48.  
    49. delegate:
    50. CheckBox {
    51. id: checkBoxDelegate
    52. anchors.verticalCenter: parent.verticalCenter
    53. anchors.left: parent.left
    54. anchors.right: parent.right
    55. anchors.margins: 5
    56. checked: styleData.value
    57.  
    58. onCheckedChanged: {
    59.  
    60. // *** Save check state to model here! ***
    61.  
    62. // Print Model
    63. theModel.print_rows()
    64. }
    65. }
    66. }
    67.  
    68. // Column 1
    69. TableViewColumn {
    70. role: "role_m_one";
    71. width: 70
    72.  
    73. delegate:
    74. Button {
    75. anchors.verticalCenter: parent.verticalCenter
    76. text: "Click Me!"
    77. width: parent.width
    78.  
    79. onClicked: {
    80. console.log(styleData.value)
    81. }
    82. }
    83. }
    84.  
    85.  
    86. // Column 2
    87. TableViewColumn {
    88. title: "Some Text";
    89. role: "role_m_two";
    90. width: 70
    91.  
    92. delegate:
    93. TextField {
    94. anchors.fill: parent
    95. text: styleData.value
    96.  
    97. onTextChanged: {
    98.  
    99. // *** Save text to model here! ***
    100.  
    101. // Print Model
    102. theModel.print_rows()
    103. }
    104. }
    105. }
    106. }
    107. }
    To copy to clipboard, switch view to plain text mode 

    /* mymodel.h */
    Qt Code:
    1. #ifndef MYMODEL_H
    2. #define MYMODEL_H
    3.  
    4. #include <QAbstractTableModel>
    5. #include <QDebug>
    6.  
    7.  
    8. struct SimpleData {
    9. bool enable;
    10. QString m_one;
    11. QString m_two;
    12.  
    13. SimpleData() {
    14. enable = true;
    15. m_one = "1";
    16. m_two = "2";
    17. }
    18.  
    19. SimpleData(bool enable, const QString m_one, const QString m_two) :
    20. enable(enable), m_one(m_one), m_two(m_two) {}
    21. };
    22.  
    23.  
    24. class MyModel : public QAbstractTableModel
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. explicit MyModel(QObject *parent = 0);
    30.  
    31. int rowCount(const QModelIndex &parent = QModelIndex()) const ;
    32. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    33. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    34. bool setData(const QModelIndex & index, const QVariant &value, int role = Qt::EditRole);
    35. QHash<int, QByteArray> roleNames() const;
    36.  
    37. QList<SimpleData*> m_data_list;
    38.  
    39. enum MyRoles {
    40. RoleOne = Qt::UserRole + 1,
    41. RoleTwo,
    42. RoleThree
    43. };
    44.  
    45. signals:
    46.  
    47. public slots:
    48. void print_rows();
    49. };
    50.  
    51. #endif // MYMODEL_H
    To copy to clipboard, switch view to plain text mode 

    /* mymodel.cpp */
    Qt Code:
    1. #include "mymodel.h"
    2.  
    3. MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent) {
    4.  
    5. SimpleData *m_simpledata;
    6.  
    7. // Create junk model data
    8. m_simpledata = new SimpleData(true, "a1", "b1");
    9. m_data_list.append(m_simpledata);
    10. m_simpledata = new SimpleData(false, "a2", "b2");
    11. m_data_list.append(m_simpledata);
    12. }
    13.  
    14. int MyModel::rowCount(const QModelIndex & /*parent*/) const {
    15. return m_data_list.length();
    16. }
    17.  
    18. int MyModel::columnCount(const QModelIndex & /*parent*/) const {
    19. return 3;
    20. }
    21.  
    22. QVariant MyModel::data(const QModelIndex &index, int role) const {
    23.  
    24. switch(role)
    25. {
    26. case RoleOne:
    27. return m_data_list.at(index.row())->enable;
    28. case RoleTwo:
    29. return m_data_list.at(index.row())->m_one;
    30. case RoleThree:
    31. return m_data_list.at(index.row())->m_two;
    32. }
    33.  
    34. return QVariant();
    35. }
    36.  
    37. bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role) {
    38.  
    39. qDebug() << "setData() called with:" << value;
    40.  
    41. switch(role)
    42. {
    43. case RoleOne:
    44. m_data_list[index.row()]->enable = value.toBool();
    45. case RoleTwo:
    46. m_data_list[index.row()]->m_one = value.toString();
    47. case RoleThree:
    48. m_data_list[index.row()]->m_two = value.toString();
    49. }
    50.  
    51. return true;
    52. }
    53.  
    54. QHash<int, QByteArray> MyModel::roleNames() const {
    55.  
    56. QHash<int, QByteArray> roles;
    57. roles[RoleOne] = "role_enable";
    58. roles[RoleTwo] = "role_m_one";
    59. roles[RoleThree] = "role_m_two";
    60. return roles;
    61. }
    62.  
    63. void MyModel::print_rows() {
    64.  
    65. qDebug() << "Model Data:";
    66.  
    67. for (int i=0; i < m_data_list.length(); i++) {
    68. qDebug() << "At" << i << ":" << m_data_list.at(i)->enable << m_data_list.at(i)->m_one << m_data_list.at(i)->m_two;
    69. }
    70.  
    71. }
    To copy to clipboard, switch view to plain text mode 

    /* main.cpp */
    Qt Code:
    1. #include <QApplication>
    2. #include <QQmlApplicationEngine>
    3. #include <QQmlContext>
    4.  
    5. #include "mymodel.h"
    6.  
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11. QQmlApplicationEngine engine;
    12. MyModel model;
    13.  
    14. engine.rootContext()->setContextProperty("theModel", &model);
    15. engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: setData() with QAbstractTableModel in QtQuick TableView

    You can either create one method for each thing you want to change, e.g. setEnabled(int row, int column, bool enabled), or a more generic form, e.g. setData(int row, int column, const QString &role, const QVariant &value).

    Either as Q_INVOKABLE or as a slot.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    zergedu (26th August 2014)

  4. #3
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: setData() with QAbstractTableModel in QtQuick TableView

    Edit: *** Nevermind *** That information is in the styleData. Thanks!!

    Ok, thanks! I had thought of doing it this way, but I wasn't sure how to determine the row/col from the TableView. I tried using TableView.currentRow, but that only works if I click the row 1st and then item I want to change. Any thoughts?
    Last edited by zergedu; 26th August 2014 at 20:12.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: setData() with QAbstractTableModel in QtQuick TableView

    Each delegate contains a property "index" which points to the row number in the model the delegate represents.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QtQuick TableView creation - how to?
    By MarkoSan in forum Qt Quick
    Replies: 1
    Last Post: 16th June 2014, 09:45
  2. Replies: 3
    Last Post: 6th March 2014, 10:09
  3. TableView and QAbstractTableModel
    By MattieB in forum Newbie
    Replies: 1
    Last Post: 11th December 2013, 20:57
  4. setData in a model derived from QAbstractTableModel
    By enno in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2010, 14:11
  5. Slow update in QAbstractTableModel in setData
    By cevou in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2010, 14:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.