欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

此處我們分別重寫三個代理接口,其中兩個ComBox組件用于選擇婚否,SpinBox組件用于調(diào)節(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); //設(shè)置為無邊框
    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);  //強(qiáng)制類型轉(zhuǎn)換
    spinBox->setValue(value); //設(shè)置編輯器的數(shù)值
}

void QWIntSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
//將代理組件的數(shù)據(jù),保存到數(shù)據(jù)模型中
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor); //強(qiáng)制類型轉(zhuǎn)換
    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
{
//設(shè)置組件大小
    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);
}

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

#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);    // 關(guān)聯(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);
        }
    }

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

}

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

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

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

相關(guān)文章

  • C++實(shí)現(xiàn)簡易選課系統(tǒng)代碼分享

    C++實(shí)現(xiàn)簡易選課系統(tǒng)代碼分享

    這篇文章主要介紹了C++實(shí)現(xiàn)簡易選課系統(tǒng)及實(shí)現(xiàn)代碼的分享,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助
    2022-01-01
  • C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二)

    C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(137.單獨(dú)的數(shù)字之二),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 用C語言實(shí)現(xiàn)簡單五子棋小游戲

    用C語言實(shí)現(xiàn)簡單五子棋小游戲

    這篇文章主要為大家詳細(xì)介紹了用C語言實(shí)現(xiàn)簡單五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C++二叉搜索樹模擬實(shí)現(xiàn)示例

    C++二叉搜索樹模擬實(shí)現(xiàn)示例

    本文主要介紹了C++二叉搜索樹模擬實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • C++實(shí)現(xiàn)車票管理系統(tǒng)

    C++實(shí)現(xiàn)車票管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)車票管理系統(tǒng),連接數(shù)據(jù)庫MySQL,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++實(shí)現(xiàn) 單例模式實(shí)例詳解

    C++實(shí)現(xiàn) 單例模式實(shí)例詳解

    這篇文章主要介紹了C++實(shí)現(xiàn) 單例模式實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • C/C++位操作實(shí)例總結(jié)

    C/C++位操作實(shí)例總結(jié)

    這篇文章主要介紹了C/C++位操作實(shí)例總結(jié),是C/C++程序設(shè)計中很重要的概念,需要的朋友可以參考下
    2014-08-08
  • C++ deque與vector對比的優(yōu)缺點(diǎn)

    C++ deque與vector對比的優(yōu)缺點(diǎn)

    這篇文章主要介紹了C++中deque與vector相比的優(yōu)勢與劣勢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-01-01
  • C++算法計時器的實(shí)現(xiàn)示例

    C++算法計時器的實(shí)現(xiàn)示例

    本文主要介紹了C++算法計時器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • C語言數(shù)據(jù)結(jié)構(gòu)之堆排序的優(yōu)化算法

    C語言數(shù)據(jù)結(jié)構(gòu)之堆排序的優(yōu)化算法

    堆排序Heap?Sort就是利用堆進(jìn)行排序的方法,下面這篇文章主要給大家介紹了關(guān)于C語言數(shù)據(jù)結(jié)構(gòu)之堆排序的優(yōu)化算法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04

最新評論