c++代码
时间: 2023-07-09 11:14:46 浏览: 138
C++代码
好的,以下是相应的C++代码示例:
```cpp
#include <QAbstractTableModel>
#include <QItemDelegate>
#include <QLineEdit>
#include <QIntValidator>
#include <QVariant>
#include <QModelIndex>
#include <QApplication>
#include <QTableView>
class CustomData {
public:
CustomData(QString name, int age) : _name(name), _age(age) {}
QString name() const {
return _name;
}
int age() const {
return _age;
}
void setAge(int age) {
_age = age;
}
private:
QString _name;
int _age;
};
class CustomTableModel : public QAbstractTableModel {
public:
CustomTableModel(QList<CustomData*> data, QObject* parent = nullptr)
: QAbstractTableModel(parent), _data(data) {}
int rowCount(const QModelIndex& parent = QModelIndex()) const override {
Q_UNUSED(parent);
return _data.size();
}
int columnCount(const QModelIndex& parent = QModelIndex()) const override {
Q_UNUSED(parent);
return 2;
}
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override {
if (role == Qt::DisplayRole) {
if (index.column() == 0) {
return _data.at(index.row())->name();
}
else if (index.column() == 1) {
return _data.at(index.row())->age();
}
}
else if (role == Qt::EditRole) {
return QVariant::fromValue(_data.at(index.row()));
}
return QVariant();
}
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override {
if (role == Qt::EditRole) {
CustomData* data = value.value<CustomData*>();
_data.replace(index.row(), data);
emit dataChanged(index, index);
return true;
}
return false;
}
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override {
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
if (section == 0) {
return "Name";
}
else if (section == 1) {
return "Age";
}
}
return QVariant();
}
private:
QList<CustomData*> _data;
};
class CustomDelegate : public QItemDelegate {
public:
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
Q_UNUSED(option);
Q_UNUSED(index);
QLineEdit* editor = new QLineEdit(parent);
QIntValidator* validator = new QIntValidator(0, 100, editor);
editor->setValidator(validator);
return editor;
}
void setEditorData(QWidget* editor, const QModelIndex& index) const override {
QVariant value = index.model()->data(index, Qt::EditRole);
CustomData* data = value.value<CustomData*>();
QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(QString::number(data->age()));
}
void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override {
CustomData* data = model->data(index, Qt::EditRole).value<CustomData*>();
QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
data->setAge(lineEdit->text().toInt());
model->setData(index, QVariant::fromValue(data), Qt::EditRole);
}
void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override {
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
};
int main(int argc, char* argv[]) {
QApplication a(argc, argv);
QList<CustomData*> data = { new CustomData("Tom", 20),
new CustomData("Jerry", 30),
new CustomData("Lucy", 25) };
CustomTableModel model(data);
QTableView view;
view.setModel(&model);
CustomDelegate delegate;
view.setItemDelegate(&delegate);
view.show();
return a.exec();
}
```
这个示例中,我们使用了QList和指针来存储CustomData对象。我们还使用了QVariant::fromValue()和QVariant::value()方法来在模型中存储和检索CustomData对象。
在自定义Delegate中,我们重写了createEditor()、setEditorData()、setModelData()和updateEditorGeometry()方法来实现我们的自定义Delegate。在setEditorData()方法中,我们从模型中检索CustomData对象,并将其转换为QLineEdit中的文本。在setModelData()方法中,我们将文本转换回CustomData对象,并将其存储回模型。
阅读全文