您的位置:首页 > 编程语言 > Qt开发

Qt中QAbstractTableModel、QItemDelegate的联合使用

2015-04-16 22:21 603 查看
1、继承QAbstractTableModel实现CurrencyModel,需要重写的函数有

introwCount(constQModelIndex&parent)const;
//返回表的行数

intcolumnCount(constQModelIndex&parent)const;//返回表的列数

QVariantheaderData(intsection,Qt::Orientationorientation,introle)const;//用来显示表头信息

QVariantdata(constQModelIndex&index,introle)const;//用来显示表中的数据

Qt::ItemFlagsflags(constQModelIndex&index)const;//设置表项是否可编辑

boolsetData(constQModelIndex&index,constQVariant&value,introle
=Qt::EditRole);//用来设置表的数据(指的是后台数据)

CurrencyModel.h头文件代码

#ifndef CURRENCYMODEL_H
#define CURRENCYMODEL_H

#include <QAbstractTableModel>
#include <QMap>

class CurrencyModel : public QAbstractTableModel
{
public:
CurrencyModel(QObject *parent = 0);
~CurrencyModel();
void setCurrencyMap(const QMap<QString, double> &map);
bool insertRows(int row, int count, const QModelIndex &parent);
bool removeRows(int row, int count, const QModelIndex &parent);
int rowCount(const QModelIndex &parent) const;

protected:
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
Qt::ItemFlags flags(const QModelIndex &index) const;

public:
QMap<QString, double> currencyMap;
private:
QString currencyAt(int offset) const;
};

#endif // CURRENCYMODEL_H
CurrencyModel.cpp代码
#include "currencymodel.h"
#include <QtDebug>

CurrencyModel::CurrencyModel(QObject *parent):
QAbstractTableModel(parent)
{

}

CurrencyModel::~CurrencyModel()
{

}

void CurrencyModel::setCurrencyMap(const QMap<QString, double> &map)
{
beginResetModel();
currencyMap = map;
endResetModel();
}

int CurrencyModel::rowCount(const QModelIndex &parent) const
{
return currencyMap.count();
}

int CurrencyModel::columnCount(const QModelIndex &parent) const
{
return currencyMap.count();
}

QVariant CurrencyModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();

if (role == Qt::TextAlignmentRole) {
return int(Qt::AlignRight | Qt::AlignVCenter);
} else if (role == Qt::DisplayRole) {
QString rowCurrency = currencyAt(index.row());
QString columnCurrency = currencyAt(index.column());

if (index.column() == 1) return (currencyMap.begin() + index.row()).value(); //第二列表用来显示QComboBox选择的内容
if (currencyMap.value(rowCurrency) == 0.0) return "####";

double amount = currencyMap.value(columnCurrency) / currencyMap.value(rowCurrency);

return QString("%1").arg(amount, 0, 'f', 4);
}

return QVariant();
}

QVariant CurrencyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) return QVariant();

return currencyAt(section);
}

QString CurrencyModel::currencyAt(int offset) const
{
return (currencyMap.begin() + offset).key();
}

bool CurrencyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
int row = index.row();
int  col = index.column();

currencyMap[(currencyMap.begin() + row).key()] = value.toDouble();
return true;
}

return false;
}

Qt::ItemFlags CurrencyModel::flags(const QModelIndex &index) const
{
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}

bool CurrencyModel::insertRows(int row, int count, const QModelIndex &parent)
{
beginInsertRows(QModelIndex(), row, row + count - 1);

endInsertRows();

return true;
}

bool CurrencyModel::removeRows(int row, int count, const QModelIndex &parent)
{
beginRemoveRows(QModelIndex(), row, row + count - 1);

endRemoveRows();

return true;
}
2、继承QItemDelegate实现itemDelegate类,要重载三个函数


<span style="color: rgb(128, 0, 128);">QWidget</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0); font-style: italic;">createEditor</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 128);">QWidget</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">*</span>parent<span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QStyleOptionViewItem</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">&</span>option<span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QModelIndex</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">&</span>index<span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(0, 0, 0);">;//创建编辑器</span>
<span style="color: rgb(0, 0, 0);"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0); font-style: italic;">setEditorData</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 128);">QWidget</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">*</span>editor<span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QModelIndex</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">&</span>index<span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(0, 0, 0);">;//设置编辑器数据</span></span>
<span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 0);"><span style="color: rgb(128, 128, 0);">void</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0); font-style: italic;">setModelData</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 128);">QWidget</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">*</span>editor<span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QAbstractItemModel</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">*</span>model<span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 0, 128);">QModelIndex</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(0, 0, 0);">&</span>index<span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(192, 192, 192);"> </span><span style="color: rgb(128, 128, 0);">const</span><span style="color: rgb(0, 0, 0);">;//设置model数据</span></span></span>
<span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 0);">itemDelegate头文件代码</span></span></span>
#ifndef ITEMDELEGATE_H
#define ITEMDELEGATE_H

#include <QItemDelegate>

class itemDelegate : public QItemDelegate
{
public:
itemDelegate(QObject *parent = 0);
~itemDelegate();

protected:
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;

};

#endif // ITEMDELEGATE_H

itemDelegate.cpp代码

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

itemDelegate::itemDelegate(QObject *parent ):QItemDelegate(parent)
{

}

itemDelegate::~itemDelegate()
{

}

QWidget* itemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *comboBox = new QComboBox(parent);
for (int i = 0; i < 10; i++) {
comboBox->addItem(QString("%1").arg('0' + i));
}

return comboBox;
}

void itemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString text = index.model()->data(index, Qt::EditRole).toString();
QComboBox * comboBox = static_cast<QComboBox*>(editor);
int tindex = comboBox->findText(text);
comboBox->setCurrentIndex(tindex);
}

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


3、将tableview的model设置为CurrencyModel,将第二列代理设置为ItemDelegate

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "currencymodel.h"
#include "itemdelegate.h"
#include <QtDebug>

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

QMap<QString, double> currencyMap;
currencyMap.insert("AUD", 1.3259);
currencyMap.insert("CHF", 1.2970);
currencyMap.insert("CZK", 24.510);
currencyMap.insert("DKK", 6.2168);
currencyMap.insert("EUR", 0.8333);
currencyMap.insert("GBP", 0.5661);
currencyMap.insert("HKD", 7.7562);
currencyMap.insert("JPY", 112.92);
currencyMap.insert("NOK", 6.5200);
currencyMap.insert("NZD", 1.4697);
currencyMap.insert("SEK", 7.8180);
currencyMap.insert("SGD", 1.6901);
currencyMap.insert("USD", 1.0000);

itemDelegate *pdelegate = new itemDelegate();
ui->tableView->setItemDelegateForColumn(1, pdelegate);

CurrencyModel *pcurrencyModel = new CurrencyModel();
pcurrencyModel->setCurrencyMap(currencyMap);
ui->tableView->setModel(pcurrencyModel);

}

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

void MainWindow::on_pushButton_clicked()
{
ui->tableView->model()->insertRows(ui->tableView->model()->rowCount(), 1);
CurrencyModel *model = dynamic_cast<CurrencyModel*>(ui->tableView->model());
model->currencyMap.insert("a", 100);
qDebug() << ui->tableView->model()->rowCount();
}

void MainWindow::on_pushButton_2_clicked()
{
int row = ui->tableView->model()->rowCount();
ui->tableView->model()->removeRows(row, 1);
CurrencyModel *model = dynamic_cast<CurrencyModel*>(ui->tableView->model());
model->currencyMap.erase(model->currencyMap.begin() + row - 1);

qDebug() << ui->tableView->model()->rowCount();

}


</pre><p><pre class="cpp" name="code" snippet_file_name="blog_20150416_12_2330568" code_snippet_id="645952">



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: