2016-03-04 91 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。