Qt tablView 模型包含指向自定义类的指针 [英] Qt tablView with model containg pointer to custom class

查看:43
本文介绍了Qt tablView 模型包含指向自定义类的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created my custom class (CustomObject), that inherit from QGraphicsItem. I use those object to draw on scene (rectangles, polygons and stuff) and I store pointers to them in QList. Now I need to display all my CustomOBjects in tableView, but there are 2 conditions:

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

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

I'm not sure if i can achieve that with jsut table view and soem custom mdoel - or shoud I make my own QAbstractItemModel class, but if i do - how do i do it? Shoud i make class inherit from QAbstractItemModel and add pointer to my CustomObject, or just force my CustomObjects into soem specific model?

我的一些代码:

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

Here is my CustomObject.h //I removed some code that was stricly related to "personal" functions related with specific features of my app

    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;

我将它们存储在列表中,在我的监督者"类中:

I store them in list, in my "Overseer" class:

    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 并保留它的指针.

In my mainwindow - I simply create that ObjOverseer and keep its pointer.

编辑 1

我使用了这个例子:https://doc.qt.io/archives/4.6/itemviews-addressbook.html并创建了这个类:

I used this example: https://doc.qt.io/archives/4.6/itemviews-addressbook.html and created this class:

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

但是,当我到达我应该使用这个模型的函数点时 - 我不知道我应该添加它,或者即使我能够按预期使用它.

But still, when i reach point in my function where i shoud use this model- i dont know hwo i shoud add it or even if i will be able to use it as intended.

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

EDIT 2 When i chaned ListOfObject to public and tried:

   MyModel->ListOfObjects.append(newObj);

全部崩溃

推荐答案

解决了.现在我只将对象的表示"添加到模型中,每当我需要更新任何对象(例如更改名称或颜色)时,我只是通过我的监督者来完成,传递 soem 类型的 guid/标识符/id/任何可以让我区分对象.

Solved it. Now i only add "representation" of object to model and whenever i need to update any object (ew. change name or color) i just do it via my overseer, passing soem kind of guid/identifier/id/anything that woudl let me tell objects apart form eachother.

这篇关于Qt tablView 模型包含指向自定义类的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆