Reputation: 15
I'm trying to create a simple Mandlebrot viewer in Qt, I have a Mainwindow with a QGraphicsScene in it, I generate the QImage with the picture and then I have some buttons that I'd like to use to navigate the image (Move, zoom, etc.)
I can get the initial image to appear, however I'm not sure how to tell it to rerender after I've changed any of the coordinates. For the life of me I can't work out how to refresh the QMainWindow, or alternatively remove the QGraphicsScene from the MainWindow and make a call to render it.
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: Solved! Thank you so much goug, that helped a lot. I'm new to Qt, so couldn't work out where the loop was that would update things. I added the code you suggested and it worked perfectly. Thanks :)
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()));
}
Upvotes: 0
Views: 973
Reputation: 2444
You don't show any code that changes any coordinates, or anything for that matter. For the built-in graphics items such as QGraphicsPixmapItem
, which is what you create by call addPixmap
, you generally don't have to force anything. Those objects repaint themselves as needed when you change something via their member functions.
I suspect that where you're going wrong is that you may be believing that there's a connection between the pixmap and the QGraphicsPixmapItem
that you created in the constructor. There isn't; so if it's the pixmap that you're changing, then you need to reapply that pixmap to the pixmap item. You'll need a new member in your class to track:
QGraphicsPixmapItem *pixmap_item_;
And change your constructor code to:
pixmap_item_ = graphic->addPixmap(QPixmap::fromImage(renderImage()));
Then whenever you've updated your pixmap, reapply that pixmap to the graphics item you've created in the constructor:
pixmap_item_->setPixmap (QPixmap::fromImage(renderImage()));
The setPixmap
call will trigger the pixmap item to repaint itself; you don't have to call update()
separately. If this isn't the issue, then we need to see more of your code.
Upvotes: 1