C++ Qt 学习(九):模型视图代理

1. Qt 模型视图代理

  • Qt 模型视图代理,也可以称为 MVD 模式
    • 模型(model)、视图(view)、代理(delegate)
    • 主要用来显示编辑数据

在这里插入图片描述

1.1 模型

  • 模型 (Model) 是视图与原始数据之间的接口
    • 原始数据可以是:数据库的一个数据表、内存中的一个 StringList,磁盘文件结构
    • QAbstractItemModel 是所有模型的祖宗类,其它 model 类都派生于它

在这里插入图片描述

1.2 视图

  • 视图 (View) 是显示和编辑数据的界面组件
    • 主要的视图组件有 QListView、QTreeView 和 QTableView
    • QListWidget、QTreeWidget 和 QTableWidget 是视图类的简化版
      • 它们不使用数据模型,而是将数据直接存储在组件的每个项里
    • QAbstractItemView 是所有视图的祖宗类,其它 view 类都派生于它

在这里插入图片描述

1.3 代理

  • 代理 (Delegate) 为视图组件提供数据编辑器
    • 如在表格组件中,编辑一个单元格的数据时,缺省是使用一个 QLineEdit 编辑框
    • 代理负责从数据模型获取相应的数据,然后显示在编辑器里,修改数据后,又将其保存到数据模型中

2. QTableView 应用

在这里插入图片描述

  • tableView.pro
    QT       += core gui
    
    // 使用 QAxObject 需添加下行
    // The QAxObject class provides a QObject that wraps a COM object.
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets axcontainer
    

2.1 widget.ui

在这里插入图片描述

2.2 主窗口

2.2.1 widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include "cintspindelegate.h"
#include "cfloatspindelegate.h"
#include "ccomboboxdelegate.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget {
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_btnOpenExcel_clicked();
    void on_btnReshowData_clicked();
    void OnCurrentChanged(const QModelIndex &current, const QModelIndex &previous);
    void on_btnAppendLast_clicked();
    void on_btnAppend_clicked();
    void on_btnDeleteSelectedLine_clicked();

private:
    Ui::Widget *ui;

    QStandardItemModel  *m_pItemModel;        // 数据模型
    QItemSelectionModel *m_pSelectionModel;   // Item 选择模型

    CIntSpinDelegate    m_intSpinDelegate;    // 整型数 spinbox 代理
    CFloatSpinDelegate  m_floatSpinDelegate;  // 浮点数 spinbox 代理
    CComboBoxDelegate   m_comboBoxDelegate;   // combobox 代理
};
#endif // WIDGET_H
2.2.2 widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QAxObject>
#include <QFileDialog>
#include <QStandardPaths>

static const int COLUMN_COUNT = 7;

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
    ui->setupUi(this);
    showMaximized();

    m_pItemModel = new QStandardItemModel(1, COLUMN_COUNT, this);
    m_pSelectionModel = new QItemSelectionModel(m_pItemModel);  // Item 选择模型

    // 选择当前单元格变化时的信号与槽
    connect(m_pSelectionModel, &QItemSelectionModel::currentChanged, this, &Widget::OnCurrentChanged);

    ui->tableView->setModel(m_pItemModel);                // 设置数据模型
    ui->tableView->setSelectionModel(m_pSelectionModel);  // 设置选择模型
    ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);

    // 给第 3,4,5 列设置自定义代理组件
    ui->tableView->setItemDelegateForColumn(3, &m_floatSpinDelegate);
    ui->tableView->setItemDelegateForColumn(4, &m_intSpinDelegate);
    ui->tableView->setItemDelegateForColumn(5, &m_comboBoxDelegate);
}

Widget::~Widget() {
    delete ui;
}

// 打开 excel
void Widget::on_btnOpenExcel_clicked() {
    QAxObject *excel = new QAxObject(this);
    excel->setControl("Excel.Application");
    excel->setProperty("Visible", false);  // 显示窗体看效果,选择 ture 将会看到 excel 表格被打开
    excel->setProperty("DisplayAlerts", true);
    QAxObject *workbooks = excel->querySubObject("WorkBooks");  // 获取工作簿(excel文件)集合

    QString str = QFileDialog::getOpenFileName(this, u8"打开excel",
                                               "D:/MyQtCreatorProject/9_2_tableView",
                                               u8"Excel 文件(*.xls *.xlsx)");

    // 打开刚才选定的 excel
    workbooks->dynamicCall("Open(const QString&)", str);
    QAxObject *workbook = excel->querySubObject("ActiveWorkBook");
    QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)",1);
    QAxObject *usedRange = worksheet->querySubObject("UsedRange");   // 获取表格中的数据范围

    QVariant var = usedRange->dynamicCall("Value");  // 将所有的数据读取到 QVariant 容器中保存
    QList<QList<QVariant>> excel_list;               // 用于将 QVariant 转换为 Qlist 的二维数组
    QVariantList varRows = var.toList();
    if (varRows.isEmpty()) {
         return;
    }

    const int row_count = varRows.size();
    QVariantList rowData;

    for (int i = 0; i < row_count; ++i) {
        rowData = varRows[i].toList();
        excel_list.push_back(rowData);
    }

    // 将每一行的内容放到 contentList
    QList<QStringList> contentList;

    for (int i = 0; i < row_count; i++) {
        QList<QVariant> curList = excel_list.at(i);
        int curRowCount = curList.size();
        QStringList oneLineStrlist;

        for (int j = 0; j < curRowCount; j++) {
            QString content = curList.at(j).toString();
            oneLineStrlist << content;
        }

        contentList << oneLineStrlist;
    }

    workbook->dynamicCall("Close(Boolean)", false);
    excel->dynamicCall("Quit(void)");
    delete excel;

    // 解析 contentList,填充 tableView
    int rowCounts = contentList.size();
    QStandardItem *aItem;

    // 遍历行
    for (int i = 0; i < rowCounts; i++) {
        QStringList tmpList = contentList[i];

        if(i == 0) {
            // 设置表头
            m_pItemModel->setHorizontalHeaderLabels(tmpList);
        } else {
            int j;
            for (j = 0; j < COLUMN_COUNT - 1; j++) {
                // 不包含最后一列
                aItem = new QStandardItem(tmpList.at(j));
                m_pItemModel->setItem(i-1, j, aItem);       // 为模型的某个行列位置设置 Item
            }

            // 设置最后一列
            aItem = new QStandardItem(contentList[0].at(j));  // 获取最后一列的指针
            aItem->setCheckable(true);  // 设置可以使用 check 控件
            if (tmpList.at(j) == "0")
                aItem->setCheckState(Qt::Unchecked);  // 根据数据设置 check 状态
            else
                aItem->setCheckState(Qt::Checked);

            m_pItemModel->setItem(i-1 , j, aItem);    // 设置最后一列
        }
    }
}

// 选择单元格变化时的响应
void Widget::OnCurrentChanged(const QModelIndex &current, const QModelIndex &previous) {
   Q_UNUSED(previous);

    if (current.isValid()) {  // 当前模型索引有效
        ui->textEdit->clear();
        ui->textEdit->append(QString::asprintf(u8"当前单元格:%d行,%d列",
                             current.row(),current.column()));  // 显示模型索引的行和列号
        QStandardItem *aItem;
        aItem = m_pItemModel->itemFromIndex(current);           // 从模型索引获得 item
        ui->textEdit->append(u8"单元格内容:" + aItem->text());  // 显示 item 的文字内容
    }
}

// 在表格最后一行添加
void Widget::on_btnAppendLast_clicked() {
    QList<QStandardItem*> aItemList;
    QStandardItem *aItem;

    for (int i = 0; i < COLUMN_COUNT - 1; i++) {  // 不包含最后 1 列
        aItem = new QStandardItem(u8"自定义");
        aItemList << aItem;
    }

    // 获取最后一列的表头文字
    QString str = m_pItemModel->headerData(m_pItemModel->columnCount()-1, Qt::Horizontal, Qt::DisplayRole).toString();
    aItem = new QStandardItem(str);
    aItem->setCheckable(true);
    aItemList<<aItem;   // 添加到容器

    m_pItemModel->insertRow(m_pItemModel->rowCount(), aItemList);  // 插入一行,需要每个 Cell 的 Item
    QModelIndex curIndex = m_pItemModel->index(m_pItemModel->rowCount()-1, 0);  // 创建最后一行的 ModelIndex

    // 如果之前点击了表格,清空选择项
    m_pSelectionModel->clearSelection();

    // 设置刚插入的行为当前选择行
    m_pSelectionModel->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}

void Widget::on_btnAppend_clicked() {
    QList<QStandardItem*> aItemList;
    QStandardItem *aItem;
    for(int i = 0; i < COLUMN_COUNT-1; i++) {
        aItem = new QStandardItem(u8"自定义");
        aItemList << aItem;
    }

    // 获取表头文字
    QString str = m_pItemModel->headerData(m_pItemModel->columnCount()-1, Qt::Horizontal, Qt::DisplayRole).toString();
    aItem = new QStandardItem(str);
    aItem->setCheckable(true);
    aItemList<<aItem;

    QModelIndex curIndex = m_pSelectionModel->currentIndex();  // 获取当前选中项的模型索引
    m_pItemModel->insertRow(curIndex.row(), aItemList);  // 在当前行的前面插入一行
    m_pSelectionModel->clearSelection();                // 清除已有选择
    m_pSelectionModel->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}

// 删除选择的行
void Widget::on_btnDeleteSelectedLine_clicked() {
    QModelIndex curIndex = m_pSelectionModel->currentIndex();  // 获取当前选择单元格的模型索引

    if (curIndex.row() == m_pItemModel->rowCount() - 1) {  // 如果是最后一行
        m_pItemModel->removeRow(curIndex.row());           // 删除最后一行
    } else {
        m_pItemModel->removeRow(curIndex.row());           // 删除一行,并重新设置当前选择行
        m_pSelectionModel->setCurrentIndex(curIndex, QItemSelectionModel::Select);
    }
}

// 将 tableView 的数据显示在 textEdit
void Widget::on_btnReshowData_clicked() {
    ui->textEdit->clear();  // 清空
    QStandardItem *aItem;
    QString str;

    // 获取表头文字
    int i, j;
    for (i = 0; i < m_pItemModel->columnCount(); i++) {
        aItem = m_pItemModel->horizontalHeaderItem(i);  // 获取表头的一个项数据
        str = str + aItem->text() + "\t";  // 用 tab 间隔文字
    }

    ui->textEdit->append(str);  // 添加为文本框的一行

    //获取数据区的每行
    for (i = 0; i < m_pItemModel->rowCount(); i++) {
        str = "";
        for (j = 0; j<m_pItemModel->columnCount()-1; j++) {
            aItem = m_pItemModel->item(i,j);
            str = str + aItem->text() + QString::asprintf("\t");  //以 tab 分隔
        }

        aItem = m_pItemModel->item(i, j);  // 最后一行
        if (aItem->checkState() == Qt::Checked)
            str = str + "1";
        else
            str = str + "0";

         ui->textEdit->append(str);
    }
}

2.3 整型数 spinbox 代理

2.3.1 cintspindelegate.h
#ifndef CINTSPINDELEGATE_H
#define CINTSPINDELEGATE_H

#include <QStyledItemDelegate>

class CIntSpinDelegate : public QStyledItemDelegate {
    Q_OBJECT
public:
    CIntSpinDelegate(QObject *parent=0);

    // 自定义代理组件必须继承以下 4 个函数
    // 创建编辑组件
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const Q_DECL_OVERRIDE;

    // 从数据模型获取数据,显示到代理组件中
    void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;

    // 将代理组件的数据,保存到数据模型中
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const Q_DECL_OVERRIDE;

    // 更新代理编辑组件的大小
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const Q_DECL_OVERRIDE;
};

#endif // CINTSPINDELEGATE_H
2.3.2 cintspindelegate.cpp
#include "cintspindelegate.h"
#include <QSpinBox>

CIntSpinDelegate::CIntSpinDelegate(QObject *parent) : QStyledItemDelegate(parent) {}

QWidget *CIntSpinDelegate::createEditor(QWidget *parent,
   const QStyleOptionViewItem &option, const QModelIndex &index) const {
    // 创建代理编辑组件
    Q_UNUSED(option);
    Q_UNUSED(index);

    QSpinBox *editor = new QSpinBox(parent);  // 创建一个 QSpinBox
    editor->setFrame(false);  // 设置为无边框
    editor->setMinimum(0);
    editor->setMaximum(120);

    return editor;  // 返回此编辑器
}

void CIntSpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    // 从数据模型获取数据,显示到代理组件中
    // 获取数据模型的模型索引指向的单元的数据
    int value = index.model()->data(index, Qt::EditRole).toInt();

    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  // 强制类型转换
    spinBox->setValue(value);  // 设置编辑器的数值
}

void CIntSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    // 将代理组件的数据,保存到数据模型中
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  // 强制类型转换
    spinBox->interpretText();      // 解释数据,如果数据被修改后,就触发信号
    int value = spinBox->value();  // 获取 spinBox 的值

    model->setData(index, value, Qt::EditRole); //更新到数据模型
}

void CIntSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    // 设置组件大小
    Q_UNUSED(index);
    editor->setGeometry(option.rect);
}

2.4 浮点数 spinbox 代理

2.4.1 cfloatspindelegate.h
#ifndef CFLOATSPINDELEGATE_H
#define CFLOATSPINDELEGATE_H

#include <QObject>
#include <QWidget>
#include <QStyledItemDelegate>

class CFloatSpinDelegate : public QStyledItemDelegate {
    Q_OBJECT
public:
    CFloatSpinDelegate(QObject *parent=0);

    // 自定义代理组件必须继承以下4个函数
    // 创建编辑组件
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const Q_DECL_OVERRIDE;

    void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const Q_DECL_OVERRIDE;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const Q_DECL_OVERRIDE;
};

#endif // CFLOATSPINDELEGATE_H
2.4.2 cfloatspindelegate.cpp
#include "cfloatspindelegate.h"
#include <QDoubleSpinBox>

CFloatSpinDelegate::CFloatSpinDelegate(QObject *parent):QStyledItemDelegate(parent) {}

QWidget *CFloatSpinDelegate::createEditor(QWidget *parent,
        const QStyleOptionViewItem &option, const QModelIndex &index) const {
    Q_UNUSED(option);
    Q_UNUSED(index);

    QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
    editor->setFrame(false);
    editor->setMinimum(0);
    editor->setDecimals(2);
    editor->setMaximum(100);

    return editor;
}

void CFloatSpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    float value = index.model()->data(index, Qt::EditRole).toFloat();
    QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
    spinBox->setValue(value);
}

void CFloatSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
    spinBox->interpretText();
    float value = spinBox->value();
    QString str = QString::asprintf("%.2f", value);

    model->setData(index, str, Qt::EditRole);
}

void CFloatSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    editor->setGeometry(option.rect);
}

2.5 combobox 代理

2.5.1 ccomboboxdelegate.h
#ifndef CCOMBOBOXDELEGATE_H
#define CCOMBOBOXDELEGATE_H

#include <QItemDelegate>

class CComboBoxDelegate : public QItemDelegate {
    Q_OBJECT

public:
    CComboBoxDelegate(QObject *parent=0);

    // 自定义代理组件必须继承以下4个函数
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const Q_DECL_OVERRIDE;

    void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;

    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const Q_DECL_OVERRIDE;

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const Q_DECL_OVERRIDE;
};

#endif // CCOMBOBOXDELEGATE_H
2.5.2 ccomboboxdelegate.cpp
#include "ccomboboxdelegate.h"
#include <QComboBox>

CComboBoxDelegate::CComboBoxDelegate(QObject *parent) : QItemDelegate(parent) {}

QWidget *CComboBoxDelegate::createEditor(QWidget *parent,
       const QStyleOptionViewItem &option, const QModelIndex &index) const {
    QComboBox *editor = new QComboBox(parent);

    editor->addItem(u8"优");
    editor->addItem(u8"良");
    editor->addItem(u8"一般");

    return editor;
}

void CComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    QString str = index.model()->data(index, Qt::EditRole).toString();

    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    comboBox->setCurrentText(str);
}

void CComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    QString str = comboBox->currentText();
    model->setData(index, str, Qt::EditRole);
}

void CComboBoxDelegate::updateEditorGeometry(QWidget *editor,
                const QStyleOptionViewItem &option, const QModelIndex &index) const {
    editor->setGeometry(option.rect);
}

3. QListView 应用

在这里插入图片描述

3.1 widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QStringListModel>
#include <QMenu>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget {
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    void initMenu();

private slots:
    void on_btnAddItem_clicked();
    void on_btnDeleteItem_clicked();
    void on_btnInsert_clicked();
    void on_btnClearAllData_clicked();
    void on_btnReshow_clicked();
    void on_showRightMenu(const QPoint& pos);
    void OnActionDelete();

    // 链接 listview 的 clicked 信号
    void on_listView_clicked(const QModelIndex &index);

private:
    Ui::Widget *ui;

    QStringListModel* m_pStringListModel;
    QMenu *m_pMenu;
};
#endif // WIDGET_H

3.2 widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QMenu>

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) {
    ui->setupUi(this);
    this->setWindowTitle(u8"QListView使用教程");

    QStringList strList;
    strList << u8"北京" << u8"上海" << u8"深圳" << u8"广东"
            << u8"南京" << u8"苏州" << u8"西安";

    // 创建数据模型
    m_pStringListModel = new QStringListModel(this);

    // 为模型设置 StringList,会导入 StringList 的内容
    m_pStringListModel->setStringList(strList);

    // 为 listView 设置数据模型
    ui->listView->setModel(m_pStringListModel);

    // 设置 listview 编辑属性
    // 双击与选择
    //ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);

    initMenu();

    // listview 右键菜单
    ui->listView->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->listView, &QListView::customContextMenuRequested, this, &Widget::on_showRightMenu);
}

Widget::~Widget() {
    delete ui;
}

// 添加 item
void Widget::on_btnAddItem_clicked() {
    // 在尾部插入一空行, 不添加就把最后一行给替换了
    m_pStringListModel->insertRow(m_pStringListModel->rowCount());

    // 获取最后一行
    QModelIndex index = m_pStringListModel->index(m_pStringListModel->rowCount() - 1, 0);
    m_pStringListModel->setData(index,"new item", Qt::DisplayRole);  // 设置显示文字

    // 设置新添加的行选中
    ui->listView->setCurrentIndex(index);
}

// 删除选中的项
void Widget::on_btnDeleteItem_clicked() {
    // 获取当前选中的 modelIndex
    QModelIndex index = ui->listView->currentIndex();

    // 删除当前行
    m_pStringListModel->removeRow(index.row());
}

// 插入一项
void Widget::on_btnInsert_clicked() {
    // 获取选中 model Index
    QModelIndex index=ui->listView->currentIndex();

    // 在当前行的前面插入一行
    m_pStringListModel->insertRow(index.row());
    m_pStringListModel->setData(index, "inserted item", Qt::DisplayRole);
    ui->listView->setCurrentIndex(index);
}

// 回显 listview数据
void Widget::on_btnReshow_clicked() {
    // 获取数据模型的 StringList
    QStringList tmpList = m_pStringListModel->stringList();

    ui->textEdit->clear();  // 文本框清空

    for (int i = 0; i < tmpList.count(); i++) {
        // 显示数据模型的 StringList()返回的内容
        ui->textEdit->append(tmpList.at(i));
    }
}

// 清除所有数据
void Widget::on_btnClearAllData_clicked() {
    m_pStringListModel->removeRows(0, m_pStringListModel->rowCount());
}

void Widget::initMenu() {
    m_pMenu = new QMenu(ui->listView);
    QAction *pAc1 = new QAction(u8"删除", ui->listView);
    QAction *pAc2 = new QAction(u8"插入", ui->listView);
    QAction *pAc3 = new QAction(u8"置顶", ui->listView);
    QAction *pAc4 = new QAction(u8"排到最后", ui->listView);

    m_pMenu->addAction(pAc1);
    m_pMenu->addAction(pAc2);
    m_pMenu->addAction(pAc3);
    m_pMenu->addAction(pAc4);

    // 注意在 exec 前链接信号槽,因为 exec 会阻塞主线程,
    // 如果 connect 写在 exec 代码之后,信号槽将无法链接
    connect(pAc1, &QAction::triggered, this, &Widget::OnActionDelete);
}

void Widget::on_showRightMenu(const QPoint& pos) {
    if (!((ui->listView->selectionModel()->selectedIndexes()).empty())) {
        m_pMenu->exec(QCursor::pos());  // 在当前鼠标位置显示
    }
}

void Widget::OnActionDelete() {
    // 获取当前 modelIndex
    QModelIndex index = ui->listView->currentIndex();

    // 删除当前行
    m_pStringListModel->removeRow(index.row());
}

void Widget::on_listView_clicked(const QModelIndex &index) {
    ui->textEdit->clear();  // 文本框清空

    // 显示 QModelIndex 的行、列号
    ui->textEdit->append(QString::asprintf(u8"当前项:row=%d, column=%d",
                        index.row(), index.column()));
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/155920.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

什么是持续集成的自动化测试?

持续集成的自动化测试 如今互联网软件的开发、测试和发布&#xff0c;已经形成了一套非常标准的流程&#xff0c;最重要的组成部分就是持续集成&#xff08;Continuous integration&#xff0c;简称CI&#xff0c;目前主要的持续集成系统是Jenkins&#xff09;。 那么什么是持…

Python爬虫教程:从入门到实战

更多Python学习内容&#xff1a;ipengtao.com 大家好&#xff0c;我是涛哥&#xff0c;今天为大家分享 Python爬虫教程&#xff1a;从入门到实战&#xff0c;文章3800字&#xff0c;阅读大约15分钟&#xff0c;大家enjoy~~ 网络上的信息浩如烟海&#xff0c;而爬虫&#xff08;…

4G工业路由器智慧电梯联网应用方案

随着电梯老旧增多及日常管理上缺失&#xff0c;电梯安全运行上存在一定的问题&#xff0c;从全国电梯统计数据中可以发现&#xff0c;主要的电梯困人、故障事件发生在住宅小区&#xff0c;当前&#xff0c;住宅小区的电梯绝大多数是通过物业公司负责管理&#xff0c;物业公司安…

【尘缘赠书活动第四期】推荐几本架构师成长和软件架构技术相关的好书,助你度过这个不太景气的寒冬!

目录 1 《高并发架构实战&#xff1a;从需求分析到系统设计》2《架构师的自我修炼&#xff1a;技术、架构和未来》3《中台架构与实现&#xff1a;基于DDD和微服务》4《分布式系统架构&#xff1a;架构策略与难题求解》5《流程自动化实战&#xff1a;系统架构和软件开发视角 》6…

验证码识别之OCR识别

验证码识别&#xff1a; 背景&#xff1a; ​ 决定开一个专题给大家讲一下验证码识别&#xff0c;不要多想&#xff0c;我们不搞深度学习&#xff0c;知识用于攻破模拟登录的时候弹出的验证码&#xff0c;后续会给大家讲讲滑块等等&#xff0c;反爬虫策略如何应对。 好了&am…

大会技术“硬核”资讯来啦!点击预约 IoTDB 现场一对一调优咨询服务

承诺大家的&#xff0c;2023 IoTDB 用户大会现场“快讯”第一弹来啦&#xff01; 我们将在大会现场设置 IoTDB Workshop&#xff0c;IoTDB 原厂团队将免费提供一对一技术咨询、使用调优、疑问解答等服务。 现场提供的一对一咨询、调优时长为 30 分钟&#xff0c;在您预约的时间…

Linux_VMware 软件安装与虚拟机

从这里开始&#xff0c;我们就踏上了学习 Linux 的路程。长路漫漫亦灿灿&#xff0c;祝我也祝所有看到这篇文章的朋友&#xff01;&#xff01;&#xff01; 参考书目&#xff1a;【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.81 / ARM 裸机与嵌入式 Linux 驱动…

Stable Diffusion1.5网络结构-超详细原创

目录 1 Unet 1.1详细整体结构 1.2 缩小版整体结构 1.3 时间步编码 1.4 CrossAttnDownBlock2D 1.4.1 ResnetBlock2D 1.4.2 Transformer2DModel 1.4.2.1 BasicTransformerBlock 1.4.2.1.1 SelfAttention 1.4.2.1.2 CrossAttention 2 VAE 3 CLIP 绘制软件&#xff1a;…

Linux操作系统基础 – 正则表达式快速入门

Linux操作系统基础 – 正则表达式快速入门 Linux Operating System Essentials - Introduction to Regular Expressions 通常在计算机科学领域&#xff0c;正则表达式被解释为对字符串操作的一种逻辑公式&#xff0c;即用事先定义好的特定字符及其组合组成所谓的“规则字符串”…

深度学习之基于YoloV5血红细胞检测识别系统

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 深度学习已经在许多领域中得到了广泛的应用&#xff0c;包括医疗健康领域。其中&#xff0c;YOLO&#xff08;You O…

UE基础篇四:地形基础概念

导语: 视频文档在文末 一、从引擎中导入高度图 高度图是灰度图,并且每个像素有16位的深度,支持这种格式的是.png 和.raw格式 高度图可以自己绘制,或者通过许多网站下载那些复制现实世界地点的图片,许多网站也有免费资源下载 二、草地和石头混合图层 创建一个混合材质,…

龙迅LT8912B 单通道MIPIDSI桥接LVDS+HDMI(1.4)同显点屏LVDS,加环出一路HDMI

龙迅LT8912B 描述: Lontium LT8912B MIPIDSI到LVDS和HDMI桥采用单通道MIPID-PHY接收器前端配置&#xff0c;每个通道有4个数据通道&#xff0c;每个数据通道运行1.5Gbps&#xff0c;最大输入带宽可达6Gbps。对于屏幕应用&#xff0c;该桥解码MIPIDSI 18bpp RGB666和24bpp RGB…

系列四、JVM的内存结构【本地接口(Native Interface)】

一、组成 本地接口由本地方法栈&#xff08;Native Method Stack&#xff09;、本地方法接口&#xff08;Native Interface&#xff09;、本地方法库组成。 二、本地接口的作用 本地接口的作用是融合不同的编程语言为Java所用&#xff0c;它的初衷是融合C/C程序&#xff0c;Jav…

【ARM Trace32(劳特巴赫) 使用介绍 5 -- Trace32 scan dump 详细介绍】

文章目录 1.1 JTAG 测试逻辑架构1.2 D型扫描触发器1.2.1 全扫描介绍1.3 IR 寄存器1.4 TDR(Test data registers)1.4.1 TDR 的实现1.4.1.1 Bypass Register1.4.1.2 Boundary-scan register1.5 Scan Dump1.5.1 soft fusion1.1 JTAG 测试逻辑架构 图 1-1 片上测试逻辑概念图 如前面…

YOLOv8-Seg改进:检测头全新创新篇 | S_improve_Segment结构创新

🚀🚀🚀本文改进:S_improve_Segment全新的分割结构头创新,适配各个YOLO 🚀🚀🚀 S_improve_Segment在各个场景都能够有效涨点 🚀🚀🚀YOLOv8-seg创新专栏:http://t.csdnimg.cn/KLSdv 学姐带你学习YOLOv8,从入门到创新,轻轻松松搞定科研; 1)手把手教你…

数据结构 链表

单链表&#xff1a;单链表用来写邻接表&#xff0c;邻接表用来存储图和树 双链表&#xff1a;用来优化某些问题 单链表 链式存储 #include<stdio.h> #include<stdlib.h> int cont 0; //结构体 typedef struct List { int data; //数据域 struct List* next; //…

Freeswitch中CHANNEL_HANGUP外呼挂断事件

1.CHANNEL_HANGUP外呼挂断事件 事件详细 ################## 外呼挂断&#xff1a;############################# [EslMessage{contentTypecommand/reply, headers3, body0 lines}] 2023-11-16T03:41:33.5140800 INFO c.e.c.v.s.c.i.FsServerEventHandler - eventReceived:…

vue2 mixin的方式 大屏适配(缩放居中的方式)

使用要求&#xff1a;指定容器的 id 为 bigScreenContainer 一、效果图 不管你的屏幕多大都会根据设计稿 1920*1080 进行缩放 图一&#xff1a;缩小的效果 图二&#xff1a;放大的效果 二、使用方式 <template><div id"bigScreenContainer" ref"big…

【Proteus仿真】【Arduino单片机】HC05蓝牙通信

文章目录 一、功能简介二、软件设计三、实验现象联系作者 一、功能简介 本项目使用Proteus8仿真Arduino单片机控制器&#xff0c;使用PCF8574、LCD1602液晶、HC05蓝牙传感器等。 主要功能&#xff1a; 系统运行后&#xff0c;LCD1602显示蓝牙接收数据。 二、软件设计 /* 作者…

axios升级依赖版本后报错SyntaxError: Cannot use import statement outside a module

Axios构建为ES模块&#xff0c;而不是在Node中运行时的CommonJs。Jest的问题是它在Node中运行代码。这就是为什么告诉Jest转换Axios有效的原因。 Axios SDK附带了一个用于Node env的内置CommonJs文件。因此&#xff0c;我们可以通过将以下内容添加到您的package.json来修复它&a…
最新文章