我創建了自定義類(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