2017-03-09 30 views
0

我想在Qt中创建一个简单的Mandlebrot查看器,我有一个QGraphicsScene的主窗口,我生成带图片的QImage,然后我有一些按钮,我会喜欢用于导航图像(移动,缩放等)如何更新QMainWindow中的QGraphicsScene

我可以得到最初的图像出现,但我不知道如何告诉它改变任何坐标后重新渲染。 对于我的生活,我无法弄清楚如何刷新QMainWindow,或者从MainWindow中删除QGraphicsScene并进行调用来渲染它。

QImage renderImage(//loads in global variables) 
{ 
    //calculates the image and returns a QImage 
} 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 

    QGraphicsScene *graphic = new QGraphicsScene(this); 
    graphic->addPixmap(QPixmap::fromImage(renderImage())); 
    ui->graphicsView->setScene(graphic); 

} 

void MainWindow::on_Left_clicked() 
{ 
    // Changes global variables and try to rerender the scene. 

    update(); //does nothing 
} 

UPDATE:解决了! 非常感谢你的帮助,这非常有帮助。我是Qt的新手,所以无法找出循环所在的位置,以便更新内容。我添加了您建议的代码,并且完美运行。谢谢:)

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 

    QGraphicsScene *graphic = new QGraphicsScene(this); 
    pixmap_item = graphic->addPixmap(QPixmap::fromImage(renderImage())); 
    ui->graphicsView->setScene(graphic); 

} 

void MainWindow::on_Left_clicked() 
{ 
    // Changes global variables and try to rerender the scene. 
    centerR -= 0.1; 
    pixmap_item->setPixmap(QPixmap::fromImage(renderImage())); 
} 
+0

他有一个QGraphicsView。它在UI定义中,并在构造函数中正确设置。 – goug

回答

1

你不显示任何代码,改变任何坐标,或任何事情。对于您通过addPixmap创建的内置图形项目,例如QGraphicsPixmapItem,您通常不需要强制执行任何操作。当你通过成员函数改变某些东西时,这些对象会根据需要重新绘制自己。

我怀疑你出错的地方在于你可能会相信像素图和你在构造函数中创建的QGraphicsPixmapItem之间有联系。没有;所以如果它是你正在改变的像素图,那么你需要将该像素图重新应用到像素图项目。你需要一个新成员,在你的类跟踪:

QGraphicsPixmapItem *pixmap_item_; 

,改变你的构造函数代码:

pixmap_item_ = graphic->addPixmap(QPixmap::fromImage(renderImage())); 

然后,每当你更新你的像素图,重新应用到像素图图形项您在构造函数中创建:

pixmap_item_->setPixmap (QPixmap::fromImage(renderImage())); 

setPixmap通话将触发图片项目重绘自己;您不必分别拨打update()。如果这不是问题,那么我们需要看到更多的代码。

相关问题