最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C/C++?Qt?TableDelegate?自定義代理組件使用詳解

 更新時間:2021年12月02日 08:32:22   作者:LyShark  
TableDelegate自定義代理組件的主要作用是對原有表格進行調整,本文主要介紹了QT中TableDelegate?自定義代理組件的使用教程,感興趣的朋友可以了解一下

TableDelegate 自定義代理組件的主要作用是對原有表格進行調整,例如默認情況下Table中的缺省代理就是一個編輯框,我們只能夠在編輯框內輸入數(shù)據(jù),而有時我們想選擇數(shù)據(jù)而不是輸入,此時就需要重寫編輯框實現(xiàn)選擇的效果,代理組件常用于個性化定制Table表格中的字段類型。

在自定義代理中QAbstractItemDelegate是所有代理類的抽象基類,我們繼承任何組件時都必須要包括如下4個函數(shù):

  • CreateEditor() 用于創(chuàng)建編輯模型數(shù)據(jù)的組件,例如(QSpinBox組件)
  • SetEditorData() 從數(shù)據(jù)模型獲取數(shù)據(jù),以供Widget組件進行編輯
  • SetModelData() 將Widget組件上的數(shù)據(jù)更新到數(shù)據(jù)模型
  • UpdateEditorGeometry() 給Widget組件設置一個合適的大小

此處我們分別重寫三個代理接口,其中兩個ComBox組件用于選擇婚否,SpinBox組件用于調節(jié)數(shù)值范圍,先來定義三個重寫部件。

重寫接口spindelegate.cpp代碼如下.

#include "spindelegate.h"
#include <QSpinBox>

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

// https://www.cnblogs.com/lyshark
QWidget *QWIntSpinDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &option, const QModelIndex &index) const
{
//創(chuàng)建代理編輯組件
    Q_UNUSED(option);
    Q_UNUSED(index);

    QSpinBox *editor = new QSpinBox(parent); //創(chuàng)建一個QSpinBox
    editor->setFrame(false); //設置為無邊框
    editor->setMinimum(0);
    editor->setMaximum(10000);
    return editor;  //返回此編輯器
}

void QWIntSpinDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
//從數(shù)據(jù)模型獲取數(shù)據(jù),顯示到代理組件中
//獲取數(shù)據(jù)模型的模型索引指向的單元的數(shù)據(jù)
    int value = index.model()->data(index, Qt::EditRole).toInt();

    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  //強制類型轉換
    spinBox->setValue(value); //設置編輯器的數(shù)值
}

void QWIntSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
//將代理組件的數(shù)據(jù),保存到數(shù)據(jù)模型中
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor); //強制類型轉換
    spinBox->interpretText(); //解釋數(shù)據(jù),如果數(shù)據(jù)被修改后,就觸發(fā)信號
    int value = spinBox->value(); //獲取spinBox的值

    model->setData(index, value, Qt::EditRole); //更新到數(shù)據(jù)模型
}

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

重寫接口floatspindelegate.cpp代碼如下.

#include "floatspindelegate.h"
#include <QDoubleSpinBox>

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

QWidget *QWFloatSpinDelegate::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(10000);

    return editor;
}

void QWFloatSpinDelegate::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);
}

// https://www.cnblogs.com/lyshark
void QWFloatSpinDelegate::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 QWFloatSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

重寫接口comboxdelegate.cpp代碼如下.

#include "comboxdelegate.h"
#include <QComboBox>

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

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

    editor->addItem("已婚");
    editor->addItem("未婚");
    editor->addItem("單身");

    return editor;
}

// https://www.cnblogs.com/lyshark
void QWComboBoxDelegate::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 QWComboBoxDelegate::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 QWComboBoxDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

將部件導入到mainwindow.cpp中,并將其通過ui->tableView->setItemDelegateForColumn(0,&intSpinDelegate);關聯(lián)部件到指定的table下標索引上面。

#include "mainwindow.h"
#include "ui_mainwindow.h"

// https://www.cnblogs.com/lyshark
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 初始化模型數(shù)據(jù)
    model = new QStandardItemModel(4,6,this);      // 初始化4行,每行六列
    selection = new QItemSelectionModel(model);    // 關聯(lián)模型

    ui->tableView->setModel(model);
    ui->tableView->setSelectionModel(selection);

    // 添加表頭
    QStringList HeaderList;
    HeaderList << "序號" << "姓名" << "年齡" << "性別" << "婚否" << "薪資";
    model->setHorizontalHeaderLabels(HeaderList);

    // 批量添加數(shù)據(jù)
    QStringList DataList[3];
    QStandardItem *Item;

    DataList[0] << "1001" << "admin" << "24" << "男" << "已婚" << "4235.43";
    DataList[1] << "1002" << "lyshark" << "23" << "男" << "未婚" << "10000.21";
    DataList[2] << "1003" << "lucy" << "37" << "女" << "單身" << "8900.23";

    int Array_Length = DataList->length();                          // 獲取每個數(shù)組中元素數(shù)
    int Array_Count = sizeof(DataList) / sizeof(DataList[0]);       // 獲取數(shù)組個數(shù)

    for(int x=0; x<Array_Count; x++)
    {
        for(int y=0; y<Array_Length; y++)
        {
            // std::cout << DataList[x][y].toStdString().data() << std::endl;
            Item = new QStandardItem(DataList[x][y]);
            model->setItem(x,y,Item);
        }
    }

    // 為各列設置自定義代理組件
    // 0,4,5 代表第幾列 后面的函數(shù)則是使用哪個代理類的意思
    ui->tableView->setItemDelegateForColumn(0,&intSpinDelegate);
    ui->tableView->setItemDelegateForColumn(4,&comboBoxDelegate);
    ui->tableView->setItemDelegateForColumn(5,&floatSpinDelegate);

}

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

代理部件關聯(lián)后,再次運行程序,會發(fā)現(xiàn)原來的TableWidget組件中的編輯框已經替換為了選擇框等組件:

到此這篇關于C/C++ Qt TableDelegate 自定義代理組件使用詳解的文章就介紹到這了,更多相關C++ Qt TableDelegate 自定義代理組件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C語言中的rand()和rand_r()詳解

    C語言中的rand()和rand_r()詳解

    這篇文章主要為大家介紹了C語言中的rand()和rand_r(),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • STL常用算法之排序算法詳解

    STL常用算法之排序算法詳解

    這篇文章主要介紹了STL常用算法之排序算法詳解,STL提供了六大組件,彼此之間可以組合套用,這六大組件分別是:容器、算法、迭代器、仿函數(shù)、適配器、空間配置器,本文主要講算法中的排序算法,需要的朋友可以參考下
    2024-01-01
  • C++ 情懷游戲掃雷的實現(xiàn)流程詳解

    C++ 情懷游戲掃雷的實現(xiàn)流程詳解

    掃雷是電腦上很經典很經典的傳統(tǒng)老游戲,從小編第一次摸到計算機開始就玩過掃雷,雖然當時并不理解玩法原理,但終是第一次玩電腦游戲,下面來從掃雷的前世今生講起
    2021-11-11
  • 簡單對比C語言中的fputs()函數(shù)和fputc()函數(shù)

    簡單對比C語言中的fputs()函數(shù)和fputc()函數(shù)

    這篇文章主要介紹了簡單對比C語言中的fputs()函數(shù)和fputc()函數(shù),注意其之間的區(qū)別,需要的朋友可以參考下
    2015-08-08
  • C語言中輸入輸出流與緩沖區(qū)的深入講解

    C語言中輸入輸出流與緩沖區(qū)的深入講解

    一般情況下,由鍵盤輸入的字符并沒有直接送入程序,而是被存儲在一個緩沖區(qū)當中。下面這篇文章主要給大家介紹了關于C語言中輸入輸出流與緩沖區(qū)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-09-09
  • C++ accumulate函數(shù)詳細介紹和具體案例

    C++ accumulate函數(shù)詳細介紹和具體案例

    這篇文章主要介紹了C++ accumulate函數(shù)詳細介紹和具體案例,accumulate是numeric庫中的一個函數(shù),主要用來對指定范圍內元素求和,但也自行指定一些其他操作,如范圍內所有元素相乘、相除等
    2022-08-08
  • 深入詳解C編寫Windows服務程序的五個步驟

    深入詳解C編寫Windows服務程序的五個步驟

    本篇文章介紹了,使用C編寫Windows服務程序的五個步驟的詳細概述。需要的朋友參考下
    2013-05-05
  • 統(tǒng)計C語言二叉樹中葉子結點個數(shù)

    統(tǒng)計C語言二叉樹中葉子結點個數(shù)

    這篇文章主要介紹的是統(tǒng)計C語言二叉樹中葉子結點個數(shù),文章以C語言二叉樹中葉子結點為基礎分享一個簡單小栗子講解,具有一定的知識參考價值,需要的小伙伴可以參考一下
    2022-02-02
  • 用C語言實現(xiàn)計算器功能

    用C語言實現(xiàn)計算器功能

    這篇文章主要為大家詳細介紹了用C語言實現(xiàn)計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • C++詳解如何實現(xiàn)動態(tài)數(shù)組

    C++詳解如何實現(xiàn)動態(tài)數(shù)組

    這篇文章主要為大家詳細介紹了C++實現(xiàn)動態(tài)數(shù)組的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06

最新評論

临安市| 治多县| 龙门县| 沾化县| 深水埗区| 龙山县| 满城县| 翁牛特旗| 冷水江市| 共和县| 麟游县| 东乡族自治县| 二连浩特市| 梁山县| 洛隆县| 惠水县| 库尔勒市| 河东区| 宁武县| 博野县| 尚义县| 道真| 巧家县| 类乌齐县| 永川市| 永顺县| 开化县| 靖江市| 甘南县| 湟源县| 安陆市| 蚌埠市| 大庆市| 安康市| 淮北市| 城步| 民丰县| 洛阳市| 明星| 新邵县| 中西区|