Results 1 to 12 of 12

Thread: QTableView + QAbstractItemModel and adding rows

  1. #1
    Join Date
    Jan 2011
    Posts
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: QTableView + QAbstractItemModel and adding rows

    I have a problem with QTableView. I want to have a table with QComboBox and QCheckBox in every row. That is why I created special class for checkbox and delegate.

    BooleanWidget
    Qt Code:
    1. #ifndef BOOLEANWIDGET_H
    2. #define BOOLEANWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QCheckBox>
    6.  
    7. class BooleanWidget : public QWidget
    8. {
    9. Q_OBJECT
    10. QCheckBox * checkBox;
    11.  
    12. public:
    13. BooleanWidget(QWidget * parent = 0);
    14.  
    15. bool isChecked(){return checkBox->isChecked();}
    16. void setChecked(bool value){checkBox->setChecked(value);}
    17. };
    18.  
    19. #endif // BOOLEANWIDGET_H
    20.  
    21.  
    22. #include "booleanwidget.h"
    23. #include <QHBoxLayout>
    24.  
    25. BooleanWidget::BooleanWidget(QWidget *parent) :
    26. QWidget(parent)
    27. {
    28. checkBox = new QCheckBox(this);
    29. QHBoxLayout * layout = new QHBoxLayout(this);
    30. layout->addWidget(checkBox,0, Qt::AlignCenter);
    31. }
    To copy to clipboard, switch view to plain text mode 

    ComboBoxDelegate
    Qt Code:
    1. #ifndef COMBOBOXDELEGATE_H
    2. #define COMBOBOXDELEGATE_H
    3.  
    4. #include <QStyledItemDelegate>
    5. #include <QTableView>
    6.  
    7. class ComboBoxDelegate : public QStyledItemDelegate
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit ComboBoxDelegate(QTableView *tableView, QObject *parent = 0);
    12.  
    13. virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    14. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    15. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    16. QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) const;
    17. void updateEditorGeometry(QWidget *editor,
    18. const QStyleOptionViewItem &option,
    19. const QModelIndex &index) const;
    20. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    21.  
    22. private:
    23. QTableView *tableView_;
    24.  
    25. signals:
    26.  
    27. public slots:
    28.  
    29. };
    30.  
    31. #endif // COMBOBOXDELEGATE_H
    32.  
    33.  
    34.  
    35. #include "comboboxdelegate.h"
    36. #include <QComboBox>
    37. #include <QCheckBox>
    38. #include <QRect>
    39. #include <QApplication>
    40. #include <QSignalMapper>
    41. #include "booleanwidget.h"
    42.  
    43. ComboBoxDelegate::ComboBoxDelegate(QTableView *tableView, QObject *parent) :
    44. QStyledItemDelegate(parent), tableView_(tableView)
    45. {
    46. }
    47.  
    48. QWidget* ComboBoxDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    49. {
    50. // ComboBox ony in column 1
    51. if(index.column() == 0) {
    52. QSignalMapper* signalMapper = new QSignalMapper(const_cast<ComboBoxDelegate*>(this));
    53.  
    54. // Create the combobox and populate it
    55. QComboBox *cb = new QComboBox(parent);
    56. cb->addItem(QString(""));
    57. cb->addItem(QString("Produkt 1"));
    58. cb->addItem(QString("Produkt 2"));
    59. cb->addItem(QString("Produkt 3"));
    60. connect(cb, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT(map()));
    61. signalMapper->setMapping(cb, index.row());
    62. connect(signalMapper, SIGNAL(mapped(int)),
    63. tableView_->model(), SLOT(changed(const int)));
    64. return cb;
    65. } else if (index.column() == 6)
    66. return new BooleanWidget(parent);
    67. else
    68. return QStyledItemDelegate::createEditor(parent, option, index);
    69. }
    70.  
    71. void ComboBoxDelegate::setEditorData ( QWidget *editor, const QModelIndex &index ) const
    72. {
    73. if(QComboBox *cb = qobject_cast<QComboBox *>(editor)) {
    74. // get the index of the text in the combobox that matches the current value of the itenm
    75. QString currentText = index.data(Qt::EditRole).toString();
    76. int cbIndex = cb->findText(currentText);
    77. // if it is valid, adjust the combobox
    78. if(cbIndex >= 0)
    79. cb->setCurrentIndex(cbIndex);
    80. } if (BooleanWidget *cb = qobject_cast<BooleanWidget *>(editor)) {
    81. cb->setChecked(index.data(Qt::DisplayRole).toInt() == 1);
    82. } else {
    83. QStyledItemDelegate::setEditorData(editor, index);
    84. }
    85. }
    86.  
    87. void ComboBoxDelegate::setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
    88. {
    89. if(QComboBox *cb = qobject_cast<QComboBox *>(editor))
    90. // save the current text of the combo box as the current value of the item
    91. model->setData(index, cb->currentText(), Qt::EditRole);
    92. else if (BooleanWidget *cb = qobject_cast<BooleanWidget *>(editor)) {
    93. model->setData(index, cb->isChecked() ? 1 : 0);
    94. } else
    95. QStyledItemDelegate::setModelData(editor, model, index);
    96. }
    97.  
    98. QRect ComboBoxDelegate::CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) const {
    99.  
    100. QStyleOptionButton check_box_style_option;
    101. QRect check_box_rect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &check_box_style_option);
    102. QPoint check_box_point(view_item_style_options.rect.x() + view_item_style_options.rect.width() / 2 - check_box_rect.width() / 2,
    103. view_item_style_options.rect.y() + view_item_style_options.rect.height() / 2 -
    104. check_box_rect.height() / 2);
    105. return QRect(check_box_point, check_box_rect.size());
    106. }
    107.  
    108. void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
    109. const QStyleOptionViewItem &option,
    110. const QModelIndex &index) const {
    111.  
    112. editor->setGeometry(option.rect);
    113. }
    114.  
    115. void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    116.  
    117. if (index.column() == 6) {
    118. int value = index.model()->data(index, Qt::DisplayRole).toInt();
    119.  
    120. QStyleOptionButton check_box_style_option;
    121. check_box_style_option.state |= QStyle::State_Enabled;
    122. if (value == 1) {
    123. check_box_style_option.state |= QStyle::State_On;
    124. } else {
    125. check_box_style_option.state |= QStyle::State_Off;
    126. }
    127. check_box_style_option.rect = ComboBoxDelegate::CheckBoxRect(option);
    128.  
    129. QApplication::style()->drawControl(QStyle::CE_CheckBox, &check_box_style_option, painter);
    130. } else
    131. QStyledItemDelegate::paint(painter, option, index);
    132. }
    To copy to clipboard, switch view to plain text mode 

    It works ok! But now I want to add new row to the table every time I change a value of combobox in last row. That I do in my Model:

    Qt Code:
    1. #ifndef DATAMODEL_H
    2. #define DATAMODEL_H
    3.  
    4. #include <QAbstractTableModel>
    5. #include <QStringList>
    6. #include <QList>
    7. #include "product.h"
    8.  
    9. class DataModel : public QAbstractTableModel
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit DataModel(QObject *parent = 0);
    14. int rowCount(const QModelIndex &parent = QModelIndex()) const ;
    15. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    16. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    17. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    18. bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
    19. Qt::ItemFlags flags(const QModelIndex & index) const ;
    20.  
    21. private:
    22. QStringList headers_;
    23. QList<Product*> dataBase_;
    24.  
    25. signals:
    26. void editCompleted(const QString &);
    27.  
    28. public slots:
    29. void changed(const int);
    30. };
    31.  
    32. #endif // DATAMODEL_H
    33.  
    34.  
    35.  
    36. #include "datamodel.h"
    37. #include <QStringList>
    38. #include <iostream>
    39.  
    40. DataModel::DataModel(QObject *parent) :
    41. {
    42. headers_ = QStringList();
    43. headers_ << "Nazwa, opis" << "Kod towaru" << "Ilość" << "JM" << "Cena netto" << "Wartość netto" << "Refaktura";
    44. dataBase_.push_back(new Product);
    45. }
    46.  
    47. int DataModel::rowCount(const QModelIndex & /*parent*/) const
    48. {
    49. return dataBase_.size();
    50. }
    51.  
    52. int DataModel::columnCount(const QModelIndex & /*parent*/) const
    53. {
    54. return 7;
    55. }
    56.  
    57. QVariant DataModel::headerData(int section, Qt::Orientation orientation, int role) const
    58. {
    59. if (orientation == Qt::Horizontal) {
    60. if( role == Qt::DisplayRole) {
    61. return headers_.at(section);
    62. }
    63. } else {
    64. if( role == Qt::DisplayRole) {
    65. return section + 1;
    66. }
    67. }
    68. }
    69.  
    70. QVariant DataModel::data(const QModelIndex &index, int role) const
    71. {
    72. if (role == Qt::DisplayRole)
    73. {
    74. std::cout << index.row() << ", " << index.column() << std::endl;
    75. return dataBase_.at(index.row())->getElement(index.column());
    76. }
    77. return QVariant();
    78. }
    79.  
    80. bool DataModel::setData(const QModelIndex & index, const QVariant & value, int role)
    81. {
    82. if (role == Qt::EditRole)
    83. {
    84. dataBase_.value(index.row())->setElement(index.column(), value.toString());
    85. }
    86. return true;
    87. }
    88.  
    89. Qt::ItemFlags DataModel::flags(const QModelIndex &index) const
    90. {
    91. if (index.column() == 0 || index.column() == 2 || index.column() == 6)
    92. return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
    93. else
    94. return QAbstractTableModel::flags(index);
    95. }
    96.  
    97. void DataModel::changed(const int row) {
    98.  
    99. dataBase_.value(row)->setElement(1, "set");
    100. emit dataChanged(index(row, 1), index(row, 6));
    101. }
    To copy to clipboard, switch view to plain text mode 

    I tried to make it in changed(const int row), but it doesn't work.

    And here is code of product

    Qt Code:
    1. #ifndef PRODUCT_H
    2. #define PRODUCT_H
    3.  
    4. #include <QObject>
    5. #include <QVariant>
    6.  
    7. class Product
    8. {
    9. public:
    10. Product();
    11. Product(QString indexName, QString productIndex, QString index,
    12. QString unit, QString place, int quantity, double sellingPrice,
    13. double buyingPrice, QString category);
    14.  
    15. QVariant getElement(int element) const;
    16. void setElement(int element, QVariant value);
    17.  
    18. QString getIndexName() const {
    19. return indexName_;
    20. }
    21.  
    22. QString getProductIndex() const {
    23. return productIndex_;
    24. }
    25.  
    26. QString getIndex() const {
    27. return index_;
    28. }
    29.  
    30. QString getUnit() const {
    31. return unit_;
    32. }
    33.  
    34. QString getPlace() const {
    35. return place_;
    36. }
    37.  
    38. int getQuantity() const {
    39. return quantity_;
    40. }
    41.  
    42. double getSellingPrice() const {
    43. return sellingPrice_;
    44. }
    45.  
    46. double getBuyingPrice() const {
    47. return buyingPrice_;
    48. }
    49.  
    50. QString getCategory() const {
    51. return category_;
    52. }
    53.  
    54. bool getReInvoice() const {
    55. return re_invoice_;
    56. }
    57.  
    58. private:
    59. QString indexName_;
    60. QString productIndex_;
    61. QString index_;
    62. QString unit_;
    63. QString place_;
    64. int quantity_;
    65. double sellingPrice_;
    66. double buyingPrice_;
    67. QString category_;
    68. bool re_invoice_;
    69.  
    70. signals:
    71.  
    72. public slots:
    73.  
    74. };
    75.  
    76. #endif // PRODUCT_H
    77.  
    78.  
    79. #include "product.h"
    80.  
    81. Product::Product() : quantity_(0), sellingPrice_(0), buyingPrice_(0), re_invoice_(false) {
    82.  
    83. }
    84.  
    85. Product::Product(QString indexName, QString productIndex, QString index,
    86. QString unit, QString place, int quantity, double sellingPrice,
    87. double buyingPrice, QString category) :
    88. indexName_(indexName), productIndex_(productIndex), index_(index),
    89. unit_(unit), place_(place), quantity_(quantity), sellingPrice_(sellingPrice),
    90. buyingPrice_(buyingPrice), category_(category), re_invoice_(false) {
    91.  
    92. }
    93.  
    94. QVariant Product::getElement(int element) const {
    95. switch(element){
    96. case 0:
    97. return indexName_;
    98. case 1:
    99. return index_;
    100. case 2:
    101. return quantity_;
    102. case 3:
    103. return unit_;
    104. case 4:
    105. return sellingPrice_;
    106. case 5:
    107. return buyingPrice_;
    108. case 6:
    109. return re_invoice_;
    110. }
    111. return QVariant();
    112. }
    113.  
    114. void Product::setElement(int element, QVariant value) {
    115. switch(element){
    116. case 0:
    117. indexName_ = value.toString();
    118. break;
    119. case 1:
    120. index_ = value.toString();
    121. break;
    122. case 2:
    123. quantity_ = value.toInt();
    124. break;
    125. case 3:
    126. unit_ = value.toString();
    127. break;
    128. case 4:
    129. sellingPrice_ = value.toDouble();
    130. break;
    131. case 5:
    132. buyingPrice_ = value.toDouble();
    133. break;
    134. case 6:
    135. re_invoice_ = value.toBool();
    136. break;
    137. }
    138. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by karlkar; 5th July 2013 at 19:50.

  2. #2
    Join Date
    Jan 2011
    Posts
    17
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default QTableView and custom QAbstractItemModel

    Hi!
    I have a TableView with my own model.
    I want to add new row. My model contains QList<Product> where each row is one Product.
    What I want to do is to add new row. I do it by adding new element to list and then calling insertRow(int). But it does not change a thing... How can it be done? Please give me exaple of code, because I've lost hope to find an answer on my own...

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView and custom QAbstractItemModel

    You will need to provide more information about your QAbstractItemModel implementation and how you are updating the model.

    If you are inserting a row via the QAbstractItemModel interface then you call insertRow() and then set the values in the model using setData(). Your implementation of insertRows() (used by insertRow()) should call beginInsertRows() and endInsertRows() around adding the new "row" the underlying data structures.

    If something is updating the data structure outside of the QAbstractItemModel interface then you must ensure it does the equivalent of the insertRows(), removeRows(), or dataChaged() implementations so that views stay correctly in synchronisation.

  4. The following user says thank you to ChrisW67 for this useful post:

    sami1592 (2nd February 2016)

  5. #4
    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: QTableView and custom QAbstractItemModel

    As Chris said, you first need to call beginInsertRows(), then add the product to the list and then call endInsertRows().

    The two functions will take care of emitting the correct signals that the view is expecting.

    Cheers,
    _

  6. #5
    Join Date
    Feb 2016
    Posts
    6
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Re: QTableView and custom QAbstractItemModel

    Quote Originally Posted by ChrisW67 View Post
    If something is updating the data structure outside of the QAbstractItemModel interface
    This is the case for me, my dataSouce/dataStructure for my QAbstractTableModel interface gets updated(added or removed item from dataStructure) outside of QAbstractTableModel. How can I implement insterRows() for this case?
    As far as I know in insertRows() implementation we should do something like this

    Qt Code:
    1. bool MyModelClass::insertRows(int position, int rows, const QModelIndex &parent)
    2. {
    3. beginInsertRows(QModelIndex(), position, position+rows-1);
    4.  
    5. // change the data structure.
    6.  
    7. endInsertRows();
    8.  
    9. return true;
    10. }
    To copy to clipboard, switch view to plain text mode 

    But my MyModelClass does not hold the data structure nor it modifies it , it happens outside MyModelClass. How should I handle this situation? And should we call insertRows() ? Yes, as usual I am new at this.

    Thanks in advance.

    PS: If unclear, please do tell
    Last edited by sami1592; 2nd February 2016 at 15:02.

  7. #6
    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: QTableView and custom QAbstractItemModel

    See comment #4

    Cheers,
    _

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView and custom QAbstractItemModel

    MyModelClass does not hold the data structure nor it modifies it
    Your model class must have at least a pointer or reference to the actual data structure, otherwise where would it get the information it needs to pass to views where it is displayed? It doesn't matter if MyModelClass modifies the data or not, it still needs read-only access to display it.

    In cases like yours where the QAbstractItemModel is just a shallow wrapper around another data structure, you need to add something so you can notify MyModelClass that the underlying data structure has been changed. If you don't have the ability to change the data structure class, then you can add some interface "glue" where you first update the data structure, then you update the model:

    Qt Code:
    1. // Pseudocode, obviously
    2. void MyGlueClass::insertRow( int row, MyRowData * data )
    3. {
    4. myDataStructure->insertRow( row, data );
    5. myModelClass->insertRow( QModelIndex(), row );
    6. }
    To copy to clipboard, switch view to plain text mode 

    and all MyModelClass needs is this:

    Qt Code:
    1. bool MyModelClass::insertRow( int row, const QModelIndex & parent )
    2. {
    3. beginInsertRows( parent, row, row );
    4. endInsertRows();
    5. }
    To copy to clipboard, switch view to plain text mode 

    If you have more ability to change the data structure class, then you could add methods that allow it to directly communicate with MyModelClass or with the class that owns both the data structure and the MyModelClass instance (like your QMainWindow class, for example - which in this case could act as the "glue").

    In other words, MyModelClass doesn't have to do anything in insertRow() / insertRows() except notify its views that the model has changed. This is what the begin / end methods do - they tell views that the model content is about to change (so they can freeze updating themselves) and that the changes have been completed so they can refresh their appearance.
    Last edited by d_stranz; 2nd February 2016 at 15:26. Reason: Typo

  9. The following user says thank you to d_stranz for this useful post:

    sami1592 (2nd February 2016)

  10. #8
    Join Date
    Feb 2016
    Posts
    6
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Smile Re: QTableView and custom QAbstractItemModel

    First of all, thanks! Did not expect such quick and detailed answer.

    Yes, my MyModelClass do have a pointer/reference of the dataStructure(in my case a list of user defined object). So when my dataStructure changes outside of my MyModelClass I just have to call the insertRows() function from there? And inside the insertRows() function I just have to call beginInsertRows() and endInsertRows() function? From the documentation I got the impression that after calling beginInsertRows() and before calling endInsertRows() I have to make changed to my internal data structure; in my case the data structure is not internal, and the change/modification of data structure does not happen in MyModelClass. Ques: What is the difference between insertRow() and insertRows(), can I manually call both of them?

    From the documentation:
    Used to add new rows and items of data to all types of model. Implementations must call beginInsertRows() before inserting new rows into any underlying data structures, and call endInsertRows() immediately afterwards.
    Link: http://doc.qt.io/qt-4.8/model-view-p...ad-only-access


    I know it's too much to ask, but do you have a working copy for this kind of scenario? Or point to some blog that has? I searched around but no luck so far except here

  11. #9
    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: QTableView and custom QAbstractItemModel

    Quote Originally Posted by sami1592 View Post
    Yes, my MyModelClass do have a pointer/reference of the dataStructure(in my case a list of user defined object). So when my dataStructure changes outside of my MyModelClass I just have to call the insertRows() function from there?
    You need to call a function that takes the new Product object and adds it to the dataBase_ list.
    What you call that function is irrelevant.

    Quote Originally Posted by sami1592 View Post
    And inside the insertRows() function I just have to call beginInsertRows() and endInsertRows() function?
    If you give us a number of how many time we need to repeat that then we can do that in a single posting and not waste part of every posting to repeat it.

    Quote Originally Posted by sami1592 View Post
    From the documentation I got the impression that after calling beginInsertRows() and before calling endInsertRows() I have to make changed to my internal data structure
    Exactly!

    Quote Originally Posted by sami1592 View Post
    in my case the data structure is not internal, and the change/modification of data structure does not happen in MyModelClass.
    DataModel::dataBase_ looks very internal to DataModel: it is private and neither a reference or pointer. Doesn't get more "internal" than that.


    Quote Originally Posted by sami1592 View Post
    Ques: What is the difference between insertRow() and insertRows(), can I manually call both of them?
    You can avoid that by using a different name for the function you are implementing, e.g. insertProduct()

    Cheers,
    _

  12. #10
    Join Date
    Feb 2016
    Posts
    6
    Thanks
    7
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTableView and custom QAbstractItemModel

    I am not the original poster. My case is a bit different that the OP. I think you mistook me for the OP

  13. #11
    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: QTableView and custom QAbstractItemModel

    Quote Originally Posted by sami1592 View Post
    I am not the original poster. My case is a bit different that the OP. I think you mistook me for the OP
    Oh boy, so instead of posting your actual problem you have resurrected an unrelated thread and wasted everybodies times?
    Way to go!

    Fortunately most of the stuff applies in any case, you're just not updating the data since it has already changed.

    Cheers,
    _

  14. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView and custom QAbstractItemModel

    Link: http://doc.qt.io/qt-4.8/model-view-p...ad-only-access

    I know it's too much to ask, but do you have a working copy for this kind of scenario? Or point to some blog that has? I searched around but no luck so far except here
    The scenario is exactly what you (and the OP) have been told: if you modify the data served up by the model (whether it is internal or external), your implementation of the model has to make the appropriate calls so that the signals to the views get generated.

    In your case, where all changes to the data are made externally to the model, then you are in effect doing a "switcheroo": the data is getting changed by whatever means it is getting changed, and so all you need to do in the model class is get the signals generated. You don't need to follow the beginInsertRows() -> make changes to the data -> endInsertRows() sequence. In your case, you are reversing the first two steps; you change the data first, then call begin / end back to back. In actual practice, you could make dozens of changes to your data, but update the model only when you want the new information displayed.

    The begin -> modify -> end sequence applies mostly when the model contains the data; if you are making calls to setData() and other model methods, then these could cause unwanted updates to the views while the model is being changed. In this case, you would call the begin method first, make the changes, then call the end method. This ensures that the views don't respond to unwanted signals until after all the changes are complete.

  15. The following user says thank you to d_stranz for this useful post:

    sami1592 (3rd February 2016)

Similar Threads

  1. Deleteting/Inserting rows in QAbstractItemModel
    By lightburst in forum Newbie
    Replies: 1
    Last Post: 9th April 2012, 01:22
  2. Problem with adding rows into QTableView
    By januszmk in forum Newbie
    Replies: 8
    Last Post: 14th July 2011, 09:20
  3. Replies: 1
    Last Post: 21st March 2011, 19:58
  4. Adding rows to Tableview
    By codeman in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2009, 10:27
  5. adding rows to tablewidget
    By reshma in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2009, 13:40

Tags for this Thread

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.