Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QGraphicsRectItem和QGraphicsCene在场景更改时的问题_C++_Qt_User Interface_Graphics - Fatal编程技术网

C++ QGraphicsRectItem和QGraphicsCene在场景更改时的问题

C++ QGraphicsRectItem和QGraphicsCene在场景更改时的问题,c++,qt,user-interface,graphics,C++,Qt,User Interface,Graphics,我想做的是: 我有一个带有QGraphicsView的小GUI。在此图形视图中,我加载了一张图片: // m_picture is QPixmap // image is QImage // m_graphic is QGraphicsScene // graphicsView is QGraphicsView m_picture.convertFromImage(image); m_graphic->addPixmap(m_picture); ui->graphicsView-&

我想做的是:

我有一个带有QGraphicsView的小GUI。在此图形视图中,我加载了一张图片:

// m_picture is QPixmap
// image is QImage
// m_graphic is QGraphicsScene
// graphicsView is QGraphicsView

m_picture.convertFromImage(image);
m_graphic->addPixmap(m_picture);
ui->graphicsView->setScene(m_graphic);
这不会导致任何问题,而且我总是可以毫无问题地加载新图像。 现在,除了显示图片外,我还想让用户能够在上面画一个矩形(将“焦点”放在特定区域)。实际上,用户只需在GUI上的四个文本框中输入坐标(x、y、宽度、高度)。提供坐标后,用户按下按钮,应显示以下坐标处的矩形。 我通过以下代码完成了这一点:

void tesseract_gui::show_preview_rect()
{
    int x,y,h,w;
    x = ui->numBox_x->value();
    y = ui->numBox_y->value();
    h = ui->numBox_h->value();
    w = ui->numBox_w->value();

    if( rect_initialized )
    {
        m_graphic->removeItem(m_rect);
    }
    else
    {
        rect_initialized = true;
    }
    m_rect->setPen(QPen(Qt::red));
    m_rect->setRect(x,y,h,w);
    m_graphic->addItem(m_rect);
    return;
}
remove调用是因为我总是希望只显示一个矩形。 现在,正如我所提到的,这很好。但是如果用户现在加载另一张图片(调用在我的帖子顶部),当我试图绘制一个新的矩形时,程序就会崩溃。我 在调用时获取分段错误

m_rect->setPen(QPen(Qt::red));
如果我打电话

m_graphic->removeItem(m_rect);
加载新图片后,我得到

QGraphicscene::removeItem:项0x8c04080的场景(0x0)与此场景(0x8c0a8b0)不同

然后它在setPen发生了同样的错误

我不明白的是,我不会改变场景。我只需添加另一张图片(或覆盖它)。 好吧,有什么建议可以让我把这件事做好吗

致意

//编辑:

我试着每次都用一个新的矩形来做:

void tesseract_gui::show_preview_rect()
{
    int x,y,h,w;
    x = ui->numBox_x->value();
    y = ui->numBox_y->value();
    h = ui->numBox_h->value();
    w = ui->numBox_w->value();

    m_graphic->clear();

    m_graphic->addRect(x,y,h,w);
    return;
}
if( m_rect->scene() != 0 )
{
    m_graphic->removeItem(m_rect);
}

m_rect->setPen(QPen(Qt::red));
m_rect->setRect(x,y,h,w);
m_graphic->addItem(m_rect);
这里的问题是,使用clear()调用,它也会从我的GraphicsView中清除图片本身。。。因此没有解决办法

//编辑:

正如我建议的那样,我摆脱了这样的警告:

void tesseract_gui::show_preview_rect()
{
    int x,y,h,w;
    x = ui->numBox_x->value();
    y = ui->numBox_y->value();
    h = ui->numBox_h->value();
    w = ui->numBox_w->value();

    m_graphic->clear();

    m_graphic->addRect(x,y,h,w);
    return;
}
if( m_rect->scene() != 0 )
{
    m_graphic->removeItem(m_rect);
}

m_rect->setPen(QPen(Qt::red));
m_rect->setRect(x,y,h,w);
m_graphic->addItem(m_rect);
我知道这不是最好的方法,但我也尝试过这种方法(对我来说不起作用):

我在构造函数中添加了该项:

m_graphic->addItem(m_rect);
然后

m_rect->setPen(QPen(Qt::red));
m_rect->setRect(x,y,h,w);
m_graphic->update();
我得到了与往常一样的“相同”错误(程序在m_rect->setPen()时崩溃)
因此,当我已经将矩形添加到图形中,然后更改m_graphic的图像,然后使用m_rect执行任何操作时,似乎总是会出现问题。(实际上,我猜m_graphic拥有m_rect的所有权,因此这会导致分割错误…?)

消息
qgraphicscene::removietem:item 0x8c0480的场景(0x0)与此场景(0x8c0a8b0)不同。
告诉您
m_rect
在您调用它时不在任何场景中。它可能在代码中的其他地方被删除,或者在类层次结构中有两个同名的变量

此外,您不需要将其从场景中移除即可进行更改。只要在场景中改变它就行了。它将在下一个绘制事件中使用新颜色和几何体重新绘制


即使您真的想在更改它之前删除它,也只需通过调用
QGraphicsItem::scene()
检查它是否在场景中。不需要init check变量。

好的,现在我去掉了removietem-但是现在如果我调用addItem,它会说“项目已经添加到此场景中”-好的,它会更改其位置,但我对这个警告感到不舒服。有没有办法“更新”以便它只在新坐标处绘制矩形?不幸的是,当我改变场景的图像,然后调用m_rectook的另一个函数时,我的程序就崩溃了,这对解决最初的问题没有帮助。它现在可以工作了——使用QGraphicsItem::scene(),我可以随时检查该项。我的代码中有一个疯狂的“m_graphic->clear()”,我想这是导致崩溃的原因。