gpt4 book ai didi

c++ - 在 QAbstractTableModel 上的 setModel 之后插入数据

转载 作者:行者123 更新时间:2023-12-02 10:22:42 24 4
gpt4 key购买 nike

我的模型是 QAbstractTableModel 的子类我已经覆盖了一些方法来完成这项工作。

class UserModel: public QAbstractTableModel
{
Q_OBJECT
public:
UserModel(QList<User> users, QObject *parent = Q_NULLPTR);

int rowCount(const QModelIndex &) const override
{

return users.size();
}
int columnCount(const QModelIndex &) const override
{
return 3;
}

QVariant data(const QModelIndex &index, int role) const override
{
if(role == Qt::DisplayRole)
{
User user = users.at(index.row());
QVariant data[] = { user.getFirstname(), user.getLastname() , user.getAge() };
return data[index.column()];
}

return {};
}


void sort(int column, Qt::SortOrder order) override
{
auto fnSort = [](const User & u1, const User & u2){ return u1.getFirstname() < u2.getFirstname(); };
auto lnSort = [](const User & u1, const User & u2){ return u1.getLastname() < u2.getLastname(); };
auto agSort = [](const User & u1, const User & u2){ return u1.getAge() < u2.getAge(); };

std::function<bool (const User &, const User &)> sortFn[] = {fnSort, lnSort, agSort};
std::sort(users.begin(), users.end(), sortFn[column]);

if(order == Qt::DescendingOrder)
{
std::reverse(users.begin(), users.end());
}
}


bool insertRows(int position, int rows, const QModelIndex &index) override
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);

for (int row = 0; row < rows; ++row)
users.insert(position, {User()});

endInsertRows();
return true;
}


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

User u = users.value(row);

if (index.column() == 0)
u.setFirstname(value.toString());
else if (index.column() == 1)
u.setLastname(value.toString());
else
return false;

users.replace(row, u);
emit(dataChanged(index, index));

return true;
}

return false;
}


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

QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
switch (section) {
case 0:
return QString("firstname");
case 1:
return QString("lastname");
case 2:
return QString("age");
}
}
return QVariant();
}

signals:
void editCompleted(const QString &);

private:

QList<User> users;



};

#endif // USERMODEL_H

有了这个,我看到了我的 table :
this->model = new UserModel(users, this);
this->m_proxy->setSourceModel(this->model);
this->ui->tableView->setModel(this->m_proxy);

我尝试使我的模型与以下内容一起工作:
this->model->addSomethingToMyModel(newUser);

似乎 LOOOOOOT 更复杂。你能帮帮我吗?我的代码基于 AdressBook 示例

最佳答案

您可以将这样的函数添加到模型类中:

void append(const User & user)
{
int row = _users.size();
insertRows(row, 1, QModelIndex());
_users[row] = user;
emit(dataChanged(index(row, 0), index(row, 2)));
}

然后向下转换表模型以使用它:
UserModel * model = dynamic_cast<UserModel*>(myTableView->model());
model->append(newUser);

关于c++ - 在 QAbstractTableModel 上的 setModel 之后插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com