1

我通过这个在 QGraphicsScene 和 QGraphicsView 的新选项卡中打开了每个图像

    void MainWindow::on_actionOpen_triggered()
    {
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
        if (!fileName.isEmpty()) {
           QImage image(fileName);
            if (image.isNull()) {
               QMessageBox::information(this, tr("Master Measure"),
                                tr("Cannot load %1.").arg(fileName));
               return;
            }

            scene = new QGraphicsScene;
            view = new QGraphicsView;

           view->setScene(scene);
           tabWidget->addTab(view,"someTab");

           scene->addPixmap(QPixmap::fromImage(image));
           scene->setBackgroundBrush(QBrush(Qt::lightGray, Qt::SolidPattern));

           QFileInfo fileInfo = fileName;
           tabWidget->setTabText(ui->tabWidget->count()-1, fileInfo.baseName());
           tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
        }
    }

我想通过单击在每个图像上绘制一些东西。

所以我通过点击新闻事件做到了这一点

void MainWindow::mousePressEvent(QMouseEvent *event)
{
     QPen pen(Qt::black);
     QBrush brush(Qt::red);
     pen.setWidth(6);
     scene->addEllipse(0,0,1000,500,pen,brush);
}

它只是在最后打开的图像(选项卡)上绘制椭圆。

我不知道如何解决这个问题。

我很感激任何想法。谢谢你。

4

1 回答 1

0

显然scene变量指向最后创建的场景。当您创建新场景时,旧指针会丢失,因为您没有将其保存在任何地方。因此,您需要在某处保留所有场景和视图指针并使用当前可见的对象。

我建议您创建一个负责每个选项卡内容QGraphicsView的子类(我们称之为)。MyView将文件名传递给该对象的构造函数。在构造函数中创建一个场景并将其存储在一个成员变量中。重新实现MyView::mousePressEvent以执行绘图。

然后你可以像这样添加新标签:

MyView* view = new MyView(filename);
view ->addTab(view,"someTab");

当用户单击视图时,将调用MyView::mousePressEvent方法或相应的对象。MyView每个视图将只看到自己的scene变量,并且将编辑相应的场景。

于 2013-07-27T20:16:40.050 回答