PDA

View Full Version : QGraphicsView in a QDialog?



jnadelman
3rd June 2010, 16:05
I am trying to create and display a QPixmap using a QGraphicsView in a QDialog as follows:

void ViewerDialog::paintEvent(QPaintEvent *e)
{
QPixmap pixmap(ui->view->size());
QPainter painter(&pixmap);
QGraphicsScene scene(ui->view);
scene.setSceneRect(ui->view->rect());
vector<SyDataT>::iterator it;
for (it = sy->syVector.begin(); it < sy->syVector.end(); it++)
{
if ((pl == it->pl) && (fr == it->fr))
{
int rsi = it->rsi;
if ((rsi > -10) || (rsi < -130))
{
rsi = -130;
}
int colorMax = 255;
int red = 0;
if (rsiMax > rsiMin)
{
red = colorMax*(rsi-rsiMin)/(rsiMax-rsiMin);
}
int green = colorMax - red;
int blue = 0;
int alpha = colorMax;
QColor color(red, green, blue, alpha);
painter.setPen(color);
painter.drawPoint(it->az, it->el);
}
scene.addPixmap(pixmap);
}
if (rsiMax == rsiMin)
{
scene.addText("No rsi");
}
ui->view->setScene(&scene);
ui->view->show();
}
I can see an expected image on the QDialog with "QPainter painter(this);" but I'd like to get the image into the QGraphicsView. What am I missing?

Also: I know there are a number of things that can trigger a paintEvent, but it is being called continuously when nothing is happening. Any idea why?

tbscope
3rd June 2010, 16:18
This is so very very wrong :-)


ui->view->setScene(&scene);
ui->view->show();

Among other lines, this will at least put your paint event in an infinite loop.

And from what it looks like, you don't even need to use the paint event of your dialog.
Why use a graphics view and start painting yourself?

The only thing and nothing else you should be doing in the paint event is painting.

jnadelman
3rd June 2010, 17:02
Thanks! That explains the loop from hell: setScene() and show() were causing paintEvents in the paintEvent.
But I still do not understand how to get the QPixmap into the QGraphicsView.
I moved QGraphicsView and QGraphicsScene out of the paintEvent so that they only get called once.
I moved QPixmap so it is available to the class.
I got rid of the view->show() since I do not think it is needed.
But I still do not see the image in the QGraphicsView.
Am I getting closer at least?


ViewerDialog::ViewerDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ViewerDialog)
{
ui->setupUi(this);
...
pixmap = new QPixmap(ui->view->size());
scene = new QGraphicsScene(ui->view);
}
...
void ViewerDialog::updateUi()
{
...
scene->setSceneRect(ui->view->rect());
ui->view->setScene(scene);
scene->addPixmap(*pixmap);
}
...
void ViewerDialog::paintEvent(QPaintEvent *e)
{
QPainter painter(pixmap);
vector<SyDataT>::iterator it;
for (it = sy->syVector.begin(); it < sy->syVector.end(); it++)
{
if ((pl == it->pl) && (fr == it->fr))
{
...
QColor color(red, green, blue, alpha);
painter.setPen(color);
painter.drawPoint(it->az, it->el);
}
}
}

tbscope
3rd June 2010, 17:28
Yep, you're getting closer, but you still don't understand the paint event.


void ViewerDialog::paintEvent(QPaintEvent *e)
This is the paint event of your dialog.

In this paint event, all you do now is paint on a pixmap, nothing else.
Hence, when you display the dialog, nothing will be shown.

Do not use the paint event.
Create another function that creates the pixmap. Don't do this in the paint event.

jnadelman
3rd June 2010, 18:05
Thanks--that was it! What was confusing me was the Qt help text:
Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent()...Thanks again.

aamer4yu
3rd June 2010, 19:26
You can go for QGraphicsPixnapItem... you need to add the item in a scene.
Set the scene to a view, and show the view . Thats it.

But what special case u have that you need to show pixmap in a graphics view only ?

jnadelman
3rd June 2010, 19:36
Don't think it is a special case. I was working with Qt Designer and needed a QDialog for mapping some of the data from a csv file to an image based on user selections. It was easy enough to add the QGraphicsView to the QDialog in Qt Designer. It just wasn't clear how to do it properly. Now I need to look at ways to modify the QPixmap in the QGraphicsView such as zoom, interpolate, etc.


You can go for QGraphicsPixnapItem... you need to add the item in a scene.
Set the scene to a view, and show the view . Thats it.

But what special case u have that you need to show pixmap in a graphics view only ?

jnadelman
3rd June 2010, 20:25
What would prevent

...
view = new QGraphicsView(layoutWidget);
...
pixmap = new QPixmap(ui->view->size());
scene = new QGraphicsScene(ui->view);
...
pixmap->scaled(ui->view->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
ui->view->setScene(scene);
scene->addPixmap(*pixmap);
from scaling pixmap to ui->view->size()?
I don't see anything in QPixmap like setScaledEnabled.

tbscope
3rd June 2010, 20:32
scaled returns the scaled pixmap, it doesn't perform the scaling on the pixmap ;-)

jnadelman
3rd June 2010, 20:42
Sorry. I've been looking at this too long. Got a nice image now... Thanks a million.
scaled returns the scaled pixmap, it doesn't perform the scaling on the pixmap ;-)