Ai
1 Star 0 Fork 0

戴的天/xbmypt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tablemodel.cpp 3.49 KB
一键复制 编辑 原始数据 按行查看 历史
戴的天 提交于 2020-04-30 13:00 +08:00 . 第一次提交
#include "tablemodel.h"
#include <QDebug>
//! [0]
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
TableModel::TableModel(const QVector<Contact> &contacts, QObject *parent)
: QAbstractTableModel(parent),
contacts(contacts)
{
}
//! [0]
//! [1]
int TableModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : contacts.size();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : 4;
}
//! [1]
//! [2]
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= contacts.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole) {
const auto &contact = contacts.at(index.row());
switch (index.column()) {
case 0:
return contact.domain;
case 1:
return contact.username;
case 2:
return contact.password;
case 3:
return contact.note;
default:
break;
}
}
return QVariant();
}
//! [2]
//! [3]
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return tr("Domain");
case 1:
return tr("Username");
case 2:
return tr("Password");
case 3:
return tr("Note");
default:
break;
}
}
return QVariant();
}
//! [3]
//! [4]
bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
contacts.insert(position, { QString(), QString(), QString(), QString() });
endInsertRows();
return true;
}
//! [4]
//! [5]
bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row)
contacts.removeAt(position);
endRemoveRows();
return true;
}
//! [5]
//! [6]
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
const int row = index.row();
auto contact = contacts.value(row);
switch (index.column()) {
case 0:
contact.domain = value.toString();
break;
case 1:
contact.username = value.toString();
break;
case 2:
contact.password = value.toString();
break;
case 3:
contact.note = value.toString();
break;
default:
return false;
}
contacts.replace(row, contact);
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
return true;
}
return false;
}
//! [6]
//! [7]
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
//! [7]
//! [8]
const QVector<Contact> &TableModel::getContacts() const
{
return contacts;
}
//! [8]
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/ddt/xbmypt.git
git@gitee.com:ddt/xbmypt.git
ddt
xbmypt
xbmypt
master

搜索帮助