“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

Item Views

The project view group is shown below

Project view group each control name

  • List View: A List View
  • Tree View: indicates the Tree View
  • Table View: indicates the Table View
  • Column View: Column View
  • Undo View: Deletes the list

Let’s start by explaining the difference between a Table View and a Table Widget

The differences are shown in the table below

The difference between QTableView QTableWidget
Inheritance relationships QTableWidget inherits from QTableView
Use the data model setModel You can use setModel to set the data model SetModel is a private function and cannot be used to set the data model
Show check boxes There is no function implementation check box QTableWidgetItem class setCheckState (Qt::Checked); Check boxes can be set
The QSqlTableModel binding QTableView can bind to QSqlTableModel QTableWidget cannot be bound to QSqlTableModel
  • Models: All models are based on the QAbstractItemModel class, which is an abstract base class.
  • Views: All views inherit from QAbstractItemView.

The InterView framework provides some common model classes and view classes, QStandardItemModel, QDirModel, QStringListModel, QProxyModel and QColumnView, QHeaderView, QListView, QTableView, QTreeView. QDirModel can display all subdirectories and related information under a directory in tree form. QProxyModel is used to transition the old Model type to the new type; QStandardItemModel displays the Model in the simplest way possible with Grid. And we can also inherit models that meet our requirements from QAbstractItemModel, QAbstractProxyModel, and QAbstractTableModel. Take your time and we’ll see later.

Qt also provides more convenient classes for working with common data models, rather than using existing models and views. They combine the model and view into one, making it easy to handle some general data structures. Using these types, while simple and convenient, also loses the flexibility of the model/view structure, so you need to play it by heart.

QTableWidget inherits from QTableView. QSqlTableModel can bind to QTableView, but not to QTableWidget. Take this code for example

        QSqlTableModel *model = new QSqlTableModel;
        model->setTable("employee");
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
        model->select(a); model->removeColumn(0);  // Do not display ID
        model->setHeaderData(0,Qt::Horizontal,tr("name"));
        model->setHeaderData(1,Qt::Horizontal,tr("Salary"));
        QTableView *view = new QTableView;
        view->setModel(model);
        view->show(a);Copy the code

It is important to note that when the view is bound to the model, the model must be created using new, otherwise the view cannot change as the model changes

Typical mistakes:

    QStandardItemModel model(4.2);
    model.setHeaderData(0,Qt::Horizontal,tr(Label));
    model.setHeaderData(1,Qt::Horizontal,tr(Quantity));
    ui->tableView->setModel(&model);
    for(int row = 0; row < 4; row++)
    {
        for(int column = 0; column < 2; column++)
        {
            QModelIndex index = model.index(row,column,QModelIndex());
            model.setData(index,QVariant((row+1) * (column+1))); }}Copy the code

The correct way to write this is:

    QStandardItemModel *model;
    model = new QStandardItemModel(4.2);
    model->setHeaderData(0,Qt::Horizontal,tr(Label));
    model->setHeaderData(1,Qt::Horizontal,tr(Quantity));
    ui->tableView->setModel(model);
    for(int row = 0; row < 4; row++)
    {
        for(int column = 0; column < 2; column++)
        {
            QModelIndex index = model->index(row,column,QModelIndex());
            model->setData(index,QVariant((row+1) * (column+1))); }}Copy the code

Item Widgets

The project control group is shown as follows:

Each control name explanation:

  • List Widget: A List control
  • Tree Widget: a Tree control
  • Table Widget: A Table control

Example:

Create a tree control with check boxes.

In Qt, tree controls are called QTreeTable, and tree nodes in controls are called QTreeWidgetItem. This control can be very useful, for example, when a group email requires a tree-shaped checkbox control.

Requirements:

Select the middle-top tree node, all the child nodes are selected, cancel the same as above, when the child node is selected, the parent node shows the partially selected state.

QTreeWidgetItem *item,int column). Use Qt designer to design the interface, then link the slot with the signal, change the signal (itemChanged(QTreeWidgetItem *item,int column)), implement this signal.

Implementation:

Create a Widget with a BUILT-IN UI, but I won’t go into that.

Drag the QTree Widget control into the Widget in the designer.

Add the following header file and code to widget.h:

#include<QTreeWidgetItem>
public: 
    void init(a); Void updateParentItem(QTreeWidgetItem *item);
public slots: 
    void onTreeItemChanged(QTreeWidgetItem *item,int column);
Copy the code

Next implement the above function in widget. CPP and call it in the constructor:

The constructor adds the following code:

init(a);connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this.SLOT(onTreeItemChanged(QTreeWidgetItem*, int)));
Copy the code

The init () implementation

void myTreeWidget::init(a)
{
    ui->treeWidget->clear(a); QTreeWidgetItem *group1 =new QTreeWidgetItem(ui->treeWidget);
    group1->setText(0.tr(2)); group1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    group1->setCheckState(0,Qt::Unchecked);
    QTreeWidgetItem *subItem11 = new QTreeWidgetItem(group1);
    subItem11->setCheckState(0,Qt::Unchecked);
    subItem11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem11->setText(0,subItem11);


    QTreeWidgetItem *subItem12 = new QTreeWidgetItem(group1);
    subItem12->setCheckState(0,Qt::Unchecked);
    subItem12->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem12->setText(0,subItem12);


    QTreeWidgetItem *subItem13 = new QTreeWidgetItem(group1);
    subItem13->setCheckState(0,Qt::Unchecked);
    subItem13->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem13->setText(0,subItem13);


    QTreeWidgetItem *subItem14 = new QTreeWidgetItem(group1);
    subItem14->setCheckState(0,Qt::Unchecked);
    subItem14->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem14->setText(0,subItem14);


    QTreeWidgetItem *group2 = new QTreeWidgetItem(ui->treeWidget);
    group2->setText(0.tr(three times)); group2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    group2->setCheckState(0,Qt::Unchecked);
    QTreeWidgetItem *subItem21 = new QTreeWidgetItem(group2);
    subItem21->setCheckState(0,Qt::Unchecked);
    subItem21->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem21->setText(0,subItem21);


    QTreeWidgetItem *subItem22 = new QTreeWidgetItem(group2);
    subItem22->setCheckState(0,Qt::Unchecked);
    subItem22->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem22->setText(0,subItem22);


    QTreeWidgetItem *subItem23 = new QTreeWidgetItem(group2);
    subItem23->setCheckState(0,Qt::Unchecked);
    subItem23->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem23->setText(0,subItem23);


    QTreeWidgetItem *subItem24 = new QTreeWidgetItem(group2);
    subItem24->setCheckState(0,Qt::Unchecked);
    subItem24->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem24->setText(0,subItem24);
}
Copy the code

OnTreeItemChanged function implementation

void myTreeWidget::onTreeItemChanged(QTreeWidgetItem *item, int column)
{
    QString itemText = item->text(0);
    int count = item->childCount(a);if(Qt::Checked == item->checkState(0))
    {
        if(count>0) {for(int i = 0; i < count; i++)
            {
                item->child(i)->setCheckState(0,Qt::Checked); }}else
        {
            // is a child node
            updateParentItem(item); }}else if(Qt::Unchecked == item->checkState(0))
    {
        if(count>0) {for(int i = 0; i < count; i++)
            {
                item->child(i)->setCheckState(0,Qt::Unchecked); }}else
        {
            // is a child node
            updateParentItem(item); }}}Copy the code

UpdateParentItem function implementation

void myTreeWidget::updateParentItem(QTreeWidgetItem *item)
{
    QTreeWidgetItem *parent = item->parent(a);if(parent == NULL)
    {
        return;
    }
    int selectedCount = 0;
    int childCount = parent->childCount(a);for (int i = 0; i < childCount; i++)
    {
        QTreeWidgetItem *childItem = parent->child(i);
        if(childItem->checkState(0) == Qt::Checked) { selectedCount++; }}if(selectedCount <= 0)
    {
        // State is not selected
        parent->setCheckState(0,Qt::Unchecked);
    }
    else if(selectedCount > 0 && selectedCount < childCount)
    {
        parent->setCheckState(0,Qt::PartiallyChecked);
    }
    else if(selectedCount == childCount)
    {
        parent->setCheckState(0,Qt::Checked); }}Copy the code

We open the designer right click the TreeWidget control and select DoubleChicked to set it to double-click to change the state


void myTreeWidget::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
    QTreeWidgetItem *parent = item->parent(a);if(parent == NULL)
    {
        return;
    }
    if(Qt::Unchecked == item->checkState(0))
    {
        item->setCheckState(0,Qt::Checked);
    }
    else if(Qt::Checked == item->checkState(0))
    {
        item->setCheckState(0,Qt::Unchecked); }}Copy the code

The effect is as follows:

Attached is a collection of sample controls from the top, ready to start studying layout in detail.

Gitee.com/gfbb/Qt_rea…