c++ - 带有模型的 Qt TableView 包含指向自定义类的指针

标签 c++ qt tableview qgraphicsitem

我创建了我的自定义类 (CustomObject),它继承自 QGraphicsItem。我使用这些对象在场景(矩形、多边形等)上绘制,并将指向它们的指针存储在 QList 中。现在我需要在 tableView 中显示我所有的 CustomOBjects,但是有两个条件:

  1. 当我在 tableview 中选择对象并与之交互时 - 我必须能够与它表示的“真实”CustomObject 交互(例如:我在 tableview 中选择 obj1 并单击“删除”或“编辑”按钮 - 我想能够与 acctaul 对象交互(删除或编辑它)。
  2. 当我添加新的或更改它时 - 我希望看到 tableView 的变化。

我不确定我是否可以使用 jsut TableView 和 soem 自定义 mdoel 实现这一点 - 或者我应该创建自己的 QAbstractItemModel 类,但如果我这样做 - 我该怎么做?我应该让类继承自 QAbstractItemModel 并添加指向我的 CustomObject 的指针,还是只强制我的 CustomObjects 进入 soem 特定模型?

我的一些代码:

这是我的 CustomObject.h //我删除了一些与我的应用的特定功能相关的“个人”功能严格相关的代码

    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;

我将它们存储在列表中,在我的“Overseer”类中:

    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;
    }

但是,当我在我的函数中到达我应该使用这个模型的位置时——我不知道我应该添加它,或者即使我能够按预期使用它。

编辑 2 当我将 ListOfObject 更改为 public 并尝试时:

   MyModel->ListOfObjects.append(newObj);

全部崩溃

最佳答案

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

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

您需要向您的 CustomModel 添加函数来处理添加新的自定义对象:

 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 添加新对象。

关于c++ - 带有模型的 Qt TableView 包含指向自定义类的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35797997/

相关文章:

c++ - 为什么类成员包含 "std::shared_ptr<std::thread>"时会发生崩溃?

c++ - bind1st 和 bind2nd

Qt 4.7.4 : Is there a way to find out the status of CAPS LOCK?

c++ - QXml 和 QDom 有什么区别?

qt - 使用着色器时在QGLWidget中通过OpenGL使用QPainter

ios - 滚动 uitableviewcell 后 UIView.animateWithDuration 停止

java - 创建搜索 TextField 字段以在 javafx tableview 中搜索

ios - 适用于 iOS 的 UITableView

c++ - 构造函数中对默认构造函数的 undefined reference

c++ - 使用 fstream 读取