{"id":9450,"date":"2019-04-13T13:24:11","date_gmt":"2019-04-13T13:24:11","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=9450"},"modified":"2020-07-16T18:48:30","modified_gmt":"2020-07-16T18:48:30","slug":"qt-qtableview-qabstracttablemodel-qtexttable","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=9450","title":{"rendered":"Qt QTableView \/ QAbstractTableModel \/ QTextTable"},"content":{"rendered":"<p><a href=\"https:\/\/doc.qt.io\/qt-5\/qtwidgets-index.html\">Qt Widgets<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/modelview.html\">Model\/View Tutorial<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/model-view-programming.html\">Model\/View Programming<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/sql-presenting.html\">Presenting SQL Data in a Table View<\/a><\/p>\n<h3>Example<\/h3>\n<p><a href=\"https:\/\/doc.qt.io\/qt-5\/qtwidgets-itemviews-stardelegate-example.html\">Star Delegate Example<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/qtwidgets-itemviews-spinboxdelegate-example.html\">Spin Box Delegate Example<\/a><\/p>\n<h3>Documentation \/ Class<\/h3>\n<p><a href=\"https:\/\/doc.qt.io\/qt-5\/qtabwidget.html\">QTabWidget Class<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/qtexttable.html\">QTextTable Class<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/qheaderview.html\">QHeaderView Class<\/a> (<br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/qabstracttablemodel.html\">QAbstractTableModel Class<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/qabstractitemmodel.html\">QAbstractItemModel Class<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/qt.html#ItemDataRole-enum\">enum Qt::ItemDataRole<\/a><\/p>\n<p><a href=\"https:\/\/doc.qt.io\/archives\/4.6\/model-view-selection.html\">Handling Selections in Item Views<\/a><\/p>\n<p><a href=\"https:\/\/forum.qt.io\/topic\/92458\/when-i-use-qscreen-screen-qguiapplication-primaryscreen-i-get-nullptr\/5\">when i use &#8220;QScreen *screen = QGuiApplication::primaryScreen()&#8221; ,i get nullpt<\/a><\/p>\n<h4>Merge cells<\/h4>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/27149733\/qtreeview-merge-some-cells\">QTreeView merge some cells<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/6504782\/how-to-merge-two-cells-of-a-qtable-row\">How to merge two cells of a qtable row?<\/a><\/p>\n<h4>Column\/Row resize<\/h4>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/18293403\/columns-auto-resize-to-size-of-qtableview\/34190094\">Columns auto-resize to size of QTableView<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/45359569\/how-to-update-qtableview-on-qabstracttablemodel-change\">How to update QTableView on QAbstractTableModel Change<\/a><br \/>\n<a href=\"https:\/\/www.bogotobogo.com\/Qt\/Qt5_QTableView_QItemDelegate_ModelView_MVC.php\">ModelView with QTableView and QItemDelegate<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/18964377\/using-qtableview-with-a-model\">Using QTableView with a model<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/29013381\/creating-qtexttable-and-inserting-data\">Creating QTextTable And Inserting Data<\/a><br \/>\n<a href=\"https:\/\/www.qtcentre.org\/threads\/52315-QtextTable-draw-your-own-boarders\">QtextTable draw your own boarders<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/15518211\/how-to-change-row-height-in-qtexttable\">How to change row height in QTextTable<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/24973378\/how-to-disable-selection-highlighting-in-a-qtablewidget\">How to disable selection highlighting in a QTableWidget<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/19304653\/how-to-set-row-height-of-qtableview\">How to set row height of QTableView?<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/5421947\/disable-selecting-row-or-column-by-clicking-the-header-in-qtablewidget\">Disable selecting row or column by clicking the header in QTableWidget<\/a><\/p>\n<h3>Focus Rectangle \/ Border Selection<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/2055705\/hide-the-border-of-the-selected-cell-in-qtablewidget-in-pyqt\">Hide the border of the selected cell in qtablewidget in pyqt?<\/a> (WORKS!)<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/24973378\/how-to-disable-selection-highlighting-in-a-qtablewidget\">How to disable selection highlighting in a QTableWidget<\/a><br \/>\n<a href=\"https:\/\/forum.qt.io\/topic\/73247\/how-to-change-style-on-a-selected-row\/4\">how to change style on a selected row<\/a><br \/>\n<a href=\"https:\/\/forum.qt.io\/topic\/41419\/qtablewidget-disable-selection-completely\">QTableWidget. Disable selection completely.<\/a><\/p>\n<h3>Row Height \/ Column Width<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/19304653\/how-to-set-row-height-of-qtableview\">How to set row height of QTableView?<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/26681578\/qtableview-column-width\">QTableView column width<\/a><\/p>\n<h3>Hide Vertical Header<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/18630236\/how-to-remove-the-vertical-header-in-qstandarditemmodel\">How to remove the vertical header in QStandardItemModel?<\/a><\/p>\n<h3>Hide Column<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/19001933\/qt-hide-column-in-qtableview\">Qt hide column in QTableView<\/a><\/p>\n<h3>Add QWidget or custom Painting in TableView<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/11777637\/adding-button-to-qtableview\">Adding button to QTableview<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/qt-5\/qtwidgets-itemviews-stardelegate-example.html\">Star Delegate Example<\/a><br \/>\n<a href=\"https:\/\/doc.qt.io\/archives\/qq\/qq24-delegates.html\">Designing Delegates<\/a><\/p>\n<h3>Alignment<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/37232302\/centering-the-text-of-a-vertical-header-in-a-qtableview\">Centering the text of a vertical header in a QTableView?<\/a><\/p>\n<h3>Header<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/11454694\/how-to-change-the-header-background-color-of-a-qtableview\">How to change the header background color of a QTableView<\/a><br \/>\n<a href=\"https:\/\/forum.qt.io\/topic\/87371\/how-to-change-qtablewidget-header-color-and-selection-color\/3\">How to change QTableWidget header color and selection color ?<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/26162387\/qtableview-qtablewidget-grid-stylesheet-grid-line-width\">QTableView\/QTableWidget grid stylesheet &#8211; grid line width<\/a><\/p>\n<h1>Update Model<\/h1>\n<p>The model emits signals to indicate changes. For example, dataChanged() is emitted whenever items of data made available by the model are changed. Changes to the headers supplied by the model cause headerDataChanged() to be emitted. If the structure of the underlying data changes, the model can emit layoutChanged() to indicate to any attached views that they should redisplay any items shown, taking the new structure into account.<\/p>\n<p><a href=\"https:\/\/forum.qt.io\/topic\/94170\/how-to-modify-the-data-in-the-model-and-update-the-view-when-new-data-is-received-from-external\/2\">How to modify the data in the model and update the view when new data is received from external?<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/37412712\/how-to-update-a-qstringlistmodel\">How to update a QStringListModel<\/a><br \/>\n<a href=\"https:\/\/www.kdab.com\/new-in-qt-5-11-improvements-to-the-model-view-apis-part-1\/\">New in Qt 5.11: improvements to the model\/view APIs (part 1)<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/7632645\/how-to-set-non-selectable-default-text-on-qcombobox\/7633081#7633081\">How to set non-selectable default text on QComboBox?<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/35113892\/qcombobox-select-no-entry\">QComboBox &#8211; Select no entry<\/a><br \/>\n<a href=\"https:\/\/forum.qt.io\/topic\/88045\/combobox-model-change-does-not-update-graphical-appearance-every-time\">ComboBox model change does not update graphical appearance every time<\/a><br \/>\n<a href=\"https:\/\/www.learnpyqt.com\/courses\/model-views\/modelview-architecture\/\">The ModelView Architecture<\/a><\/p>\n<p><!-- SOURCE -----------------------------------------------------------------------><\/p>\n<h1>Source<\/h1>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#0  PatientModel::setData () at ..\/DatabaseSelect\/src\/business\/PatientModel.cpp:188\r\n#1  QAbstractItemModel::setItemData () at itemmodels\/qabstractitemmodel.cpp:1912\r\n#2  QComboBox::insertItem () at widgets\/qcombobox.cpp:2315\r\n#3  QComboBox::insertItem () at ..\/..\/..\/Qt\/5.12.5\/gcc_64\/include\/QtWidgets\/qcombobox.h:279\r\n#4  QComboBox::addItem () at ..\/..\/..\/Qt\/5.12.5\/gcc_64\/include\/QtWidgets\/qcombobox.h:272\r\n#5  PatientListenerComboBox::patientAdded () at ..\/DatabaseSelect\/src\/presentation\/PatientListenerComboBox.cpp:18\r\n#6  Root::addPatient () at ..\/DatabaseSelect\/src\/business\/Root.cpp:15\r\n#7  MainWindow::onPatientButtonAddClicked () at ..\/DatabaseSelect\/src\/presentation\/MainWindow.cpp:164\r\n#8  MainWindow::qt_static_metacall ()::InvokeMetaMethod, _id=3, _a=0x7fffffffd590) at moc_MainWindow.cpp:106\r\n#9  QMetaObject::activate ()\r\n#10 QMetaObject::activate ()::staticMetaObject&gt;,\r\n#11 QAbstractButton::clicked () at .moc\/moc_qabstractbutton.cpp:312\r\n#12 QAbstractButtonPrivate::emitClicked () at widgets\/qabstractbutton.cpp:414\r\n#13 QAbstractButtonPrivate::click () at widgets\/qabstractbutton.cpp:407\r\n#14 QAbstractButton::mouseReleaseEvent () at widgets\/qabstractbutton.cpp:1011\r\n#15 QToolButton::mouseReleaseEvent () at widgets\/qtoolbutton.cpp:622\r\n#16 QWidget::event () at kernel\/qwidget.cpp:9327\r\n#17 QAbstractButton::event () at widgets\/qabstractbutton.cpp:968\r\n#18 QToolButton::event () at widgets\/qtoolbutton.cpp:1001\r\n#19 QApplicationPrivate::notify_helper () at kernel\/qapplication.cpp:3700\r\n#21 QCoreApplication::notifyInternal2 () at kernel\/qcoreapplication.cpp:1088\r\n#22 QCoreApplication::sendSpontaneousEvent () at kernel\/qcoreapplication.cpp:1488\r\n#23 QApplicationPrivate::sendMouseEvent ()\r\n#24 QWidgetWindow::handleMouseEvent () at kernel\/qwidgetwindow.cpp:663\r\n#25 QWidgetWindow::event () at kernel\/qwidgetwindow.cpp:281\r\n#26 QApplicationPrivate::notify_helper () at kernel\/qapplication.cpp:3700\r\n#27 QApplication::notify () at kernel\/qapplication.cpp:3446\r\n#28 QCoreApplication::notifyInternal2 () at kernel\/qcoreapplication.cpp:1088\r\n#29 QCoreApplication::sendSpontaneousEvent () at kernel\/qcoreapplication.cpp:1488\r\n#30 QGuiApplicationPrivate::processMouseEvent () at kernel\/qguiapplication.cpp:2107\r\n#31 QGuiApplicationPrivate::processWindowSystemEvent () at kernel\/qguiapplication.cpp:1842.\r\n#32 QWindowSystemInterface::sendWindowSystemEvents () at kernel\/qwindowsysteminterface.cpp:1151\r\n#33 xcbSourceDispatch () at qxcbeventdispatcher.cpp:105\r\n#34 g_main_context_dispatch () from \/usr\/lib\/x86_64-linux-gnu\/libglib-2.0.so.0\r\n#35 ?? () from \/usr\/lib\/x86_64-linux-gnu\/libglib-2.0.so.0\r\n#36 g_main_context_iteration () from \/usr\/lib\/x86_64-linux-gnu\/libglib-2.0.so.0\r\n#37 QEventDispatcherGlib::processEvents () at kernel\/qeventdispatcher_glib.cpp:422\r\n#38 QEventLoop::exec () at kernel\/qeventloop.cpp:225\r\n#39 QCoreApplication::exec () at kernel\/qcoreapplication.cpp:1389\r\n#40 main () at ..\/DatabaseSelect\/src\/main.cpp:55\r\n<\/pre>\n<h2>Model<\/h2>\n<h4><code>Qt\/5.12.3\/Src\/qtbase\/src\/corelib\/itemmodels\/qabstractitemmodel.h<\/code><\/h4>\n<h4><code>Qt\/5.12.3\/Src\/qtbase\/src\/gui\/itemmodels\/qstandarditemmodel.h<\/code><\/h4>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nclass QStandardItemPrivate\r\n{\r\n    &#x5B;...]\r\n\r\n    QStandardItemModel *model;\r\n    QStandardItem *parent;\r\n    QVector&lt;QStandardItemData&gt; values;\r\n    QVector&lt;QStandardItem*&gt; children;\r\n    int rows;\r\n    int columns;\r\n\r\n    QStandardItem *q_ptr;\r\n\r\n    &#x5B;...]\r\n};\r\n\r\nvoid\r\nQStandardItemModel::setItem(int row,\r\n                            int column,\r\n                            QStandardItem *item)\r\n{\r\n    Q_D(QStandardItemModel);\r\n    d-&gt;root-&gt;d_func()-&gt;setChild(row, column, item, true);\r\n}\r\n\r\nvoid\r\nQStandardItemPrivate::setChild(int row,\r\n                               int column,\r\n                               QStandardItem *item,\r\n                               bool emitChanged)\r\n{\r\n    &#x5B;...]\r\n}\r\n\r\nQVariant\r\nQStandardItemModel::data(const QModelIndex &amp;index,\r\n                         int role) const\r\n{\r\n    Q_D(const QStandardItemModel);\r\n    QStandardItem *item = d-&gt;itemFromIndex(index);\r\n    return item ? item-&gt;data(role) : QVariant();\r\n}\r\n\r\nQVariant\r\nQStandardItem::data(int role) const\r\n{\r\n    Q_D(const QStandardItem);\r\n    role = (role == Qt::EditRole) ? Qt::DisplayRole : role;\r\n    QVector&lt;QStandardItemData&gt;::const_iterator it;\r\n    for (it = d-&gt;values.begin(); it != d-&gt;values.end(); ++it) {\r\n        if ((*it).role == role)\r\n            return (*it).value;\r\n    }\r\n    return QVariant();\r\n}\r\n\r\nvoid\r\nQStandardItem::setRowCount(int rows)\r\n{\r\n    int rc = rowCount();\r\n    if (rc == rows)\r\n        return;\r\n    if (rc &lt; rows)\r\n        insertRows(qMax(rc, 0), rows - rc);\r\n    else\r\n        removeRows(qMax(rows, 0), rc - rows);\r\n}\r\n\r\nvoid\r\nQStandardItem::insertRows(int row,\r\n                          const QList&lt;QStandardItem*&gt; &amp;items)\r\n{\r\n    Q_D(QStandardItem);\r\n    if (row &lt; 0)\r\n        return;\r\n    d-&gt;insertRows(row, items);\r\n}\r\n\r\nbool\r\nQStandardItemPrivate::insertRows(int row,\r\n                                 int count,\r\n                                 const QList&lt;QStandardItem*&gt; &amp;items)\r\n{\r\n    &#x5B;...]\r\n\r\n    if (model)\r\n        model-&gt;d_func()-&gt;rowsAboutToBeInserted(q, row, row + count - 1);\r\n\r\n    &#x5B;...]\r\n\r\n    if (model)\r\n        model-&gt;d_func()-&gt;rowsInserted(q, row, count);\r\n\r\n    return true;\r\n}\r\n<\/pre>\n<h2>View<\/h2>\n<h4><code>Qt\/5.12.3\/Src\/qtbase\/src\/widgets\/widgets\/qcombobox.h<\/code><\/h4>\n<h4><code>Qt\/5.12.5\/Src\/qtbase\/src\/widgets\/itemviews\/qabstractitemview.h<\/code><\/h4>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/*!\r\n    This slot is called when items with the given \\a roles are changed in the\r\n    model. The changed items are those from \\a topLeft to \\a bottomRight\r\n    inclusive. If just one item is changed \\a topLeft == \\a bottomRight.\r\n\r\n    The \\a roles which have been changed can either be an empty container (meaning everything\r\n    has changed), or a non-empty container with the subset of roles which have changed.\r\n*\/\r\nvoid QAbstractItemView::dataChanged(const QModelIndex &amp;topLeft, const QModelIndex &amp;bottomRight, const QVector&lt;int&gt; &amp;roles)\r\n{\r\n    &#x5B;...]\r\n}\r\n<\/pre>\n<h4><code>Qt\/5.12.5\/Src\/qtbase\/src\/widgets\/itemviews\/qtreeview.h<\/code><\/h4>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/*!\r\n  \\reimp\r\n*\/\r\nvoid QTreeView::dataChanged(const QModelIndex &amp;topLeft, const QModelIndex &amp;bottomRight, const QVector&lt;int&gt; &amp;roles)\r\n{\r\n    &#x5B;...]\r\n    QAbstractItemView::dataChanged(topLeft, bottomRight, roles);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Qt Widgets Model\/View Tutorial Model\/View Programming Presenting SQL Data in a Table View Example Star Delegate Example Spin Box Delegate Example Documentation \/ Class QTabWidget Class QTextTable Class QHeaderView Class ( QAbstractTableModel Class QAbstractItemModel Class enum Qt::ItemDataRole Handling Selections in Item Views when i use &#8220;QScreen *screen = QGuiApplication::primaryScreen()&#8221; ,i get nullpt Merge cells QTreeView [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9450","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/9450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9450"}],"version-history":[{"count":27,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/9450\/revisions"}],"predecessor-version":[{"id":11262,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/9450\/revisions\/11262"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}