Qt将许多QGraphicsPixmapItem添加到QGraphicsScene

问题描述 投票:0回答:1

我正在尝试创建一个像大多数照片编辑器程序(Photoshop)一样的图层系统,基本上是使用QGraphicsPixmapItem :: setPixmap(QPixmap * image)绘制一个QGraphicsPixmapItem;在QGraphicsScene上。我该怎么做,但是我可以添加许多QPixmap并随意删除它们。我尝试创建一个QPixmaps列表和一个QGraphicsPixmapItems列表,但是如果我删除或重新排列QPixmaps的顺序会很混乱,是否有更好的方法呢?

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(draw())); //calls the function below to redraw sc
timer->start(30);

这每30ms更新一次GraphicsScene,所以我在像素图上所做的任何绘制*图像都被绘制,但是现在我想获取一个QPixmap列表,并在每次调用draw()时将它们添加到场景中,但是问题是我需要一个列表QGraphicsPixmapItems,如果我删除图层或移动它们的顺序,我希望关联的QGraphicsPixmapItem也要删除/移动。我想我可以做到,但是看起来很复杂,所以有什么建议吗?

void PaintArea::draw()
{
    m_item->setPixmap(*image); //Do this but call layerManager.getListOfLayers() and add each to the scene
}
c++ qt qgraphicsscene qgraphicsitem qpixmap
1个回答
0
投票

以下小示例应用程序显示了如何继续。基本上,有两个模型和两个视图。这些模型是QGraphicsScene和标准QStandardItemModel,而视图是QListView和QGraphicsView。

主要任务是通过使用信号和插槽使两个模型保持同步。

可以使用添加按钮和上下文菜单修改模型。对于这个小应用程序,只能添加,删除和更改像素图的图片。添加其他操作(例如通过拖放移动项目,并使用可检查的操作和其他自定义用户角色隐藏/可见)非常简单。

#include <QApplication>
#include <QFileDialog>
#include <QGraphicsPixmapItem>
#include <QGraphicsView>
#include <QHBoxLayout>
#include <QListView>
#include <QMenu>
#include <QPushButton>
#include <QStandardItemModel>

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);
    auto frame = new QFrame;
    frame->setLayout(new QHBoxLayout);
    auto listView = new QListView;
    frame->layout()->addWidget(listView);
    auto graphicsView = new QGraphicsView;
    frame->layout()->addWidget(graphicsView);
    auto graphicsScene = new QGraphicsScene;
    graphicsView->setScene(graphicsScene);
    auto myModel = new QStandardItemModel;
    auto btnAdd = new QPushButton("Add");
    frame->layout()->addWidget(btnAdd);
    QObject::connect(btnAdd, &QPushButton::clicked, [&]() {
        auto item = new QStandardItem("Pixmap");
        item->setData(QString("./data/test.png"), Qt::ItemDataRole::UserRole + 1);
        myModel->appendRow(item);
    });
    listView->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);

    QObject::connect(listView, &QListView::customContextMenuRequested, [&](const QPoint& pos) {
        auto index = listView->indexAt(pos);
        QMenu menu;

        auto remove = menu.addAction("Remove", [&]() {
            myModel->removeRow(index.row(), index.parent());
            });
        if (!index.isValid()) remove->setEnabled(false);

        auto changeImage = menu.addAction("Change...", [&]() {
            auto file=QFileDialog::getOpenFileName(frame, "Select PNG file", "./data/", "(*.png)");
            if (file.isEmpty()) return;
            myModel->setData(index,  file, Qt::ItemDataRole::UserRole + 1);
        });
        if (!index.isValid()) changeImage->setEnabled(false);

        menu.exec(listView->mapToGlobal(pos));
    });

    QObject::connect(myModel, &QStandardItemModel::dataChanged, [&](const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles = QVector<int>()) {
        if (auto item = myModel->itemFromIndex(topLeft)) {
            if (auto pixItem = dynamic_cast<QGraphicsPixmapItem*>(graphicsScene->items()[topLeft.row()])) {
                pixItem->setPixmap(QPixmap(item->data(Qt::ItemDataRole::UserRole + 1).toString()));
            }
        }
    });
    QObject::connect(myModel, &QStandardItemModel::rowsInserted, [&](const QModelIndex& parent, int first, int last) {
        for (auto iter = first; iter <= last; iter++) {
            auto index=myModel->index(iter, 0, parent);
            auto pixmap=myModel->data(index, Qt::ItemDataRole::UserRole + 1).toString();;
            auto item=graphicsScene->addPixmap(QPixmap(pixmap));
        }
    });

    QObject::connect(myModel, &QStandardItemModel::rowsRemoved, [&](const QModelIndex& parent, int first, int last) {
        auto items = graphicsScene->items();
        for (auto iter = first; iter <= last; iter++) {
            graphicsScene->removeItem(items[iter]);
        }
    });

    listView->setModel(myModel);
    frame->show();
    return app.exec();
}
© www.soinside.com 2019 - 2024. All rights reserved.