2016-03-04 40 views
1

我创建了自定义类(CustomObject),它从QGraphicsItem继承。我使用这些对象在场景(矩形,多边形和东西)上绘图,并将指针存储在QList中。现在我需要显示的tableView我所有的CustomOBjects,但有两个条件:Qt tablView带指向自定义类的指针的模型

  1. 当我选择,并在tableview中的对象进行交互 - 我必须能够与它(例如为代表的“真实”的CustomObject互动:我在tableview中选择了obj1,然后单击按钮“删除”或“编辑” - 我希望能够与acctaul对象交互(删除或编辑它)。
  2. 当我添加新的或更改它时 - 我希望看到更改在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); 

所有坠毁

+0

您可以通过从qabstracttablemodel继承来创建自己的模型。请查看http://doc.qt.io/qt-4.8/model-view-programming.html – Apin

+0

该问题仍未解决。 (@编辑1)我试图实现我自己的课程,但偶然发现了一些问题,我想告诉你更多的帮助。 – Arker

+0

您不能直接将数据添加到数据源。如果你想添加调用MyModel-> insertRow(),插入后你手动编辑你的数据。 所以,如果你想添加,删除,移动行,它应该通过模型API调用。如果你想编辑,你可以直接编辑你的自定义班级数据,然后刷新模型 – Apin

回答

0

解决了它。现在我只添加对象的“表示”到模型,并且每当我需要更新任何对象时(比如更改名称或颜色),我只需通过我的管理员,传递soem类型的guid/identifier/id /任何可以让我将对象分开告诉彼此。

+0

即使我被困在同样的问题。我需要将自定义对象数据添加到tableview并相应地填充它。我将在自定义对象中拥有唯一的ID字段。每当我收到删除id对象的请求时,我都需要从表中删除它。你能帮我提供这个源代码吗?我非常困惑,我无法从过去的4天中找出答案。 –

0

首先要注意的是,你在ObjOverseer和CustomModelOfCustomObject没有连接列表中,您需要保存ObjOverseer列表的指针,而不是将其复制到CustomModelOfCustomObject。这一个:

CustomModelOfCustomObject::CustomModelOfCustomObject(QList<CustomObject*> objects, QObject *parent) 
    : QAbstractTableModel(parent) 
{ 
    ListOfObjects=objects; 
} 

您需要添加功能,以您的CustomModel将处理增加新的customobject:

bool CustomModelOfCustomObject::insertNewData(CustomObject *obj, int rowposition = -1) 
{ 
    int row = rowposition < 0 ? ListOfObjects.size : row; 
    beginInserRows(QModelIndex(), row, row); 
    ListOfObjects.insert(row, obj); 
    endInsertRow(); 
} 

而当你要添加新的对象,只需调用这些功能。如果你在模型上的列表连接到ObjOverseer(指针类型),你不需要添加新的对象到你的ObjOverseer。