我创建了自定义类(CustomObject),它从QGraphicsItem继承。我使用这些对象在场景(矩形,多边形和东西)上绘图,并将指针存储在QList中。现在我需要显示的tableView我所有的CustomOBjects,但有两个条件:Qt tablView带指向自定义类的指针的模型
- 当我选择,并在tableview中的对象进行交互 - 我必须能够与它(例如为代表的“真实”的CustomObject互动:我在tableview中选择了obj1,然后单击按钮“删除”或“编辑” - 我希望能够与acctaul对象交互(删除或编辑它)。
- 当我添加新的或更改它时 - 我希望看到更改在tableView。
我不知道如果我可以实现与jsut表视图和soem自定义mdoel - 或者我做我自己的QAbstractItemModel类,bu如果我做 - 我该怎么做?首先,我从QAbstractItemModel继承类并添加指向我的CustomObject的指针,或者只强制我的CustomObjects进入soem特定模型?我的代码
小位:
这里是我的CustomObject.h //我删除了一些代码,被stricly涉及与我的应用程序的特定功能相关的“个人”功能
class CustomObject : public QGraphicsItem
{
public:
CustomObject();
CustomObject(int _x, int _y, int _w, int _h);
virtual QRectF boundingRect() const;
void set_Name(QString name);
QString get_Name();
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
private:
QString Name;
我将它们存储在列表中,在我的“监督者”类:
class ObjOverseer
public:
void drawingCustomObject_Do(int x, int y); //This function creates new "CustomObject" and adds it to the list (below)
QList<CustomObject*> ObjectsList_CustomObjects;
在我的主窗口 - 我只需创建ObjOverseer并保持它的指针。
编辑1
我用这个例子: https://doc.qt.io/archives/4.6/itemviews-addressbook.html 和创建这个类:
CustomModelOfCustomObject::CustomModelOfCustomObject()
{
}
CustomModelOfCustomObject::CustomModelOfCustomObject(QObject *parent)
: QAbstractTableModel(parent)
{
}
CustomModelOfCustomObject::CustomModelOfCustomObject(QList<CustomObject*> objects, QObject *parent)
: QAbstractTableModel(parent)
{
ListOfObjects=objects;
}
int CustomModelOfCustomObject::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return ListOfObjects.size();
}
int CustomModelOfCustomObject::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;//TODO - ZMIENIC ILOSC KOLUMN
}
QVariant CustomModelOfCustomObject::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= ListOfObjects.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole) {
CustomObject* obj = ListOfObjects.at(index.row());
if (index.column() == 0)
return obj->get_Name();
else if (index.column() == 1)
return obj->get_Address();
}
return QVariant();
}
QVariant CustomModelOfCustomObject::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("Name");
case 1:
return tr("Address");
default:
return QVariant();
}
}
return QVariant();
}
bool CustomModelOfCustomObject::insertRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position+rows-1);
for (int row=0; row < rows; row++) {
CustomObject* obj;
ListOfObjects.insert(position, obj);
}
endInsertRows();
return true;
}
bool CustomModelOfCustomObject::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position+rows-1);
for (int row=0; row < rows; ++row) {
ListOfObjects.removeAt(position);
}
endRemoveRows();
return true;
}
bool CustomModelOfCustomObject::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
int row = index.row();
CustomObject* p = ListOfObjects.value(row);
if (index.column() == 0)
p->set_Name(value.toString());
else if (index.column() == 1)
p->set_Address(value.toString());
else
return false;
ListOfObjects.replace(row, p);
emit(dataChanged(index, index));
return true;
}
return false;
}
Qt::ItemFlags CustomModelOfCustomObject::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
QList<CustomObject*> CustomModelOfCustomObject::getList()
{
return ListOfObjects;
}
但尽管如此,当我在我的功能达到地步,我768,16使用这个模型 - 我不知道我应该添加它,或者即使我能够按预期使用它。
EDIT 2 当我chaned ListOfObject公众和尝试:
MyModel->ListOfObjects.append(newObj);
所有坠毁
您可以通过从qabstracttablemodel继承来创建自己的模型。请查看http://doc.qt.io/qt-4.8/model-view-programming.html – Apin
该问题仍未解决。 (@编辑1)我试图实现我自己的课程,但偶然发现了一些问题,我想告诉你更多的帮助。 – Arker
您不能直接将数据添加到数据源。如果你想添加调用MyModel-> insertRow(),插入后你手动编辑你的数据。 所以,如果你想添加,删除,移动行,它应该通过模型API调用。如果你想编辑,你可以直接编辑你的自定义班级数据,然后刷新模型 – Apin