Qt QTableView / QAbstractTableModel / QTextTable

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 “QScreen *screen = QGuiApplication::primaryScreen()” ,i get nullpt

Merge cells

QTreeView merge some cells
How to merge two cells of a qtable row?

Column/Row resize

Columns auto-resize to size of QTableView

How to update QTableView on QAbstractTableModel Change
ModelView with QTableView and QItemDelegate
Using QTableView with a model
Creating QTextTable And Inserting Data
QtextTable draw your own boarders
How to change row height in QTextTable
How to disable selection highlighting in a QTableWidget
How to set row height of QTableView?
Disable selecting row or column by clicking the header in QTableWidget

Focus Rectangle / Border Selection

Hide the border of the selected cell in qtablewidget in pyqt? (WORKS!)
How to disable selection highlighting in a QTableWidget
how to change style on a selected row
QTableWidget. Disable selection completely.

Row Height / Column Width

How to set row height of QTableView?
QTableView column width

Hide Vertical Header

How to remove the vertical header in QStandardItemModel?

Hide Column

Qt hide column in QTableView

Add QWidget or custom Painting in TableView

Adding button to QTableview
Star Delegate Example
Designing Delegates

Alignment

Centering the text of a vertical header in a QTableView?

Header

How to change the header background color of a QTableView
How to change QTableWidget header color and selection color ?
QTableView/QTableWidget grid stylesheet – grid line width

Update Model

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.

How to modify the data in the model and update the view when new data is received from external?
How to update a QStringListModel
New in Qt 5.11: improvements to the model/view APIs (part 1)
How to set non-selectable default text on QComboBox?
QComboBox – Select no entry
ComboBox model change does not update graphical appearance every time
The ModelView Architecture

Source

#0  PatientModel::setData () at ../DatabaseSelect/src/business/PatientModel.cpp:188
#1  QAbstractItemModel::setItemData () at itemmodels/qabstractitemmodel.cpp:1912
#2  QComboBox::insertItem () at widgets/qcombobox.cpp:2315
#3  QComboBox::insertItem () at ../../../Qt/5.12.5/gcc_64/include/QtWidgets/qcombobox.h:279
#4  QComboBox::addItem () at ../../../Qt/5.12.5/gcc_64/include/QtWidgets/qcombobox.h:272
#5  PatientListenerComboBox::patientAdded () at ../DatabaseSelect/src/presentation/PatientListenerComboBox.cpp:18
#6  Root::addPatient () at ../DatabaseSelect/src/business/Root.cpp:15
#7  MainWindow::onPatientButtonAddClicked () at ../DatabaseSelect/src/presentation/MainWindow.cpp:164
#8  MainWindow::qt_static_metacall ()::InvokeMetaMethod, _id=3, _a=0x7fffffffd590) at moc_MainWindow.cpp:106
#9  QMetaObject::activate ()
#10 QMetaObject::activate ()::staticMetaObject>,
#11 QAbstractButton::clicked () at .moc/moc_qabstractbutton.cpp:312
#12 QAbstractButtonPrivate::emitClicked () at widgets/qabstractbutton.cpp:414
#13 QAbstractButtonPrivate::click () at widgets/qabstractbutton.cpp:407
#14 QAbstractButton::mouseReleaseEvent () at widgets/qabstractbutton.cpp:1011
#15 QToolButton::mouseReleaseEvent () at widgets/qtoolbutton.cpp:622
#16 QWidget::event () at kernel/qwidget.cpp:9327
#17 QAbstractButton::event () at widgets/qabstractbutton.cpp:968
#18 QToolButton::event () at widgets/qtoolbutton.cpp:1001
#19 QApplicationPrivate::notify_helper () at kernel/qapplication.cpp:3700
#21 QCoreApplication::notifyInternal2 () at kernel/qcoreapplication.cpp:1088
#22 QCoreApplication::sendSpontaneousEvent () at kernel/qcoreapplication.cpp:1488
#23 QApplicationPrivate::sendMouseEvent ()
#24 QWidgetWindow::handleMouseEvent () at kernel/qwidgetwindow.cpp:663
#25 QWidgetWindow::event () at kernel/qwidgetwindow.cpp:281
#26 QApplicationPrivate::notify_helper () at kernel/qapplication.cpp:3700
#27 QApplication::notify () at kernel/qapplication.cpp:3446
#28 QCoreApplication::notifyInternal2 () at kernel/qcoreapplication.cpp:1088
#29 QCoreApplication::sendSpontaneousEvent () at kernel/qcoreapplication.cpp:1488
#30 QGuiApplicationPrivate::processMouseEvent () at kernel/qguiapplication.cpp:2107
#31 QGuiApplicationPrivate::processWindowSystemEvent () at kernel/qguiapplication.cpp:1842.
#32 QWindowSystemInterface::sendWindowSystemEvents () at kernel/qwindowsysteminterface.cpp:1151
#33 xcbSourceDispatch () at qxcbeventdispatcher.cpp:105
#34 g_main_context_dispatch () from /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
#35 ?? () from /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
#36 g_main_context_iteration () from /usr/lib/x86_64-linux-gnu/libglib-2.0.so.0
#37 QEventDispatcherGlib::processEvents () at kernel/qeventdispatcher_glib.cpp:422
#38 QEventLoop::exec () at kernel/qeventloop.cpp:225
#39 QCoreApplication::exec () at kernel/qcoreapplication.cpp:1389
#40 main () at ../DatabaseSelect/src/main.cpp:55

Model

Qt/5.12.3/Src/qtbase/src/corelib/itemmodels/qabstractitemmodel.h

Qt/5.12.3/Src/qtbase/src/gui/itemmodels/qstandarditemmodel.h

class QStandardItemPrivate
{
    [...]

    QStandardItemModel *model;
    QStandardItem *parent;
    QVector<QStandardItemData> values;
    QVector<QStandardItem*> children;
    int rows;
    int columns;

    QStandardItem *q_ptr;

    [...]
};

void
QStandardItemModel::setItem(int row,
                            int column,
                            QStandardItem *item)
{
    Q_D(QStandardItemModel);
    d->root->d_func()->setChild(row, column, item, true);
}

void
QStandardItemPrivate::setChild(int row,
                               int column,
                               QStandardItem *item,
                               bool emitChanged)
{
    [...]
}

QVariant
QStandardItemModel::data(const QModelIndex &index,
                         int role) const
{
    Q_D(const QStandardItemModel);
    QStandardItem *item = d->itemFromIndex(index);
    return item ? item->data(role) : QVariant();
}

QVariant
QStandardItem::data(int role) const
{
    Q_D(const QStandardItem);
    role = (role == Qt::EditRole) ? Qt::DisplayRole : role;
    QVector<QStandardItemData>::const_iterator it;
    for (it = d->values.begin(); it != d->values.end(); ++it) {
        if ((*it).role == role)
            return (*it).value;
    }
    return QVariant();
}

void
QStandardItem::setRowCount(int rows)
{
    int rc = rowCount();
    if (rc == rows)
        return;
    if (rc < rows)
        insertRows(qMax(rc, 0), rows - rc);
    else
        removeRows(qMax(rows, 0), rc - rows);
}

void
QStandardItem::insertRows(int row,
                          const QList<QStandardItem*> &items)
{
    Q_D(QStandardItem);
    if (row < 0)
        return;
    d->insertRows(row, items);
}

bool
QStandardItemPrivate::insertRows(int row,
                                 int count,
                                 const QList<QStandardItem*> &items)
{
    [...]

    if (model)
        model->d_func()->rowsAboutToBeInserted(q, row, row + count - 1);

    [...]

    if (model)
        model->d_func()->rowsInserted(q, row, count);

    return true;
}

View

Qt/5.12.3/Src/qtbase/src/widgets/widgets/qcombobox.h

Qt/5.12.5/Src/qtbase/src/widgets/itemviews/qabstractitemview.h

/*!
    This slot is called when items with the given \a roles are changed in the
    model. The changed items are those from \a topLeft to \a bottomRight
    inclusive. If just one item is changed \a topLeft == \a bottomRight.

    The \a roles which have been changed can either be an empty container (meaning everything
    has changed), or a non-empty container with the subset of roles which have changed.
*/
void QAbstractItemView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
    [...]
}

Qt/5.12.5/Src/qtbase/src/widgets/itemviews/qtreeview.h

/*!
  \reimp
*/
void QTreeView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
    [...]
    QAbstractItemView::dataChanged(topLeft, bottomRight, roles);
}

Leave a Reply

Your email address will not be published. Required fields are marked *