model
QStringListModel(->QStandardItemModel->QAbstractItemModel)
QStringListModel是qt自带的比较好用的listview的model
QStandardItemModel(->QAbstractItemModel)
可以用作treeview和tableview的model 通过appendrow添加QStandardItem类型的项 通过model->appendrow添加头节点。通过QStandardItem再appendrow子节点
- appendRow: appendRow 是 QStandardItem 类中的函数,用于在树形视图中的项(QStandardItem)的末尾添加一个新的子项。 例如,如果您有一个 QStandardItem 对象 parentItem,您可以调用 parentItem->appendRow(new QStandardItem(“Child”)) 来向 parentItem 添加一个名为 “Child” 的子项。
- setChild: setChild 也是 QStandardItem 类中的函数,用于设置某个项的指定位置的子项。 例如,如果您有一个 QStandardItem 对象 parentItem,您可以调用 parentItem->setChild(row, column, new QStandardItem(“Child”)) 来将一个名为 “Child” 的子项设置到 parentItem 的指定行和列位置。
- setIndexWidget 在 Qt 中,setIndexWidget 函数通常用于 QTableWidget 或者 QListWidget 等部件,用于在特定单元格或项上设置自定义的部件。 也可以通过使用 QTreeView 的委托(delegate)来实现类似的功能。委托是 Qt 中用于自定义部件的一种机制,它可以让您为特定的单元格或项提供自定义的绘制和编辑行为。
- setitem model->setItem(0,0,item1);
view中添加QCombobox
一般使用createEditor()/setEditorData()/setModelData()三种方法 createEditor()的方法是创建控件 setEditorData()的方法是设置控件数据,从model中读取 setModelData()的方法是更新model数据,从控件中读取
#include <QApplication>
#include <QStandardItemModel>
#include <QTreeView>
#include <QItemDelegate>
#include <QComboBox>
class ComboBoxDelegate : public QItemDelegate
{
public:
ComboBoxDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
if (index.column() == 1) // 仅在第二列上放置QComboBox
{
QComboBox *comboBox = new QComboBox(parent);
comboBox->addItem("Option 1");
comboBox->addItem("Option 2");
comboBox->addItem("Option 3");
return comboBox;
}
else
{
return QItemDelegate::createEditor(parent, option, index);
}
}
void setEditorData(QWidget *editor, const QModelIndex &index) const override
{
if (QComboBox *comboBox = qobject_cast<QComboBox *>(editor))
{
QString value = index.model()->data(index, Qt::EditRole).toString();
int i = comboBox->findText(value);
if (i != -1)
comboBox->setCurrentIndex(i);
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
{
if (QComboBox *comboBox = qobject_cast<QComboBox *>(editor))
{
model->setData(index, comboBox->currentText(), Qt::EditRole);
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStandardItemModel model(4, 2);
QTreeView treeView;
treeView.setModel(&model);
treeView.setItemDelegateForColumn(1, new ComboBoxDelegate(&treeView)); // 将QComboBoxDelegate应用于第二列
treeView.show();
return app.exec();
}
在这个示例中,我们创建了一个自定义的QItemDelegate类ComboBoxDelegate,它用于在QTreeView的第二列上放置一个QComboBox。在createEditor()方法中,我们检查索引的列号,如果是第二列,则创建一个QComboBox并返回。在setEditorData()方法中,我们将QComboBox的当前值设置为项目的值。在setModelData()方法中,我们将QComboBox的当前文本设置为项目的数据。
通过使用QItemDelegate,您可以轻松地在QTreeView中的项目上放置QComboBox或者其他自定义的编辑器。
QStandardItemModel和QAbstractItemModel的区别
QAbstractItemModel包含一些纯虚函数,需要实现具体的定义,而QStandardItemModel,不需要逐一实现各纯虚函数,设置数据直接model->setData(index,