QGraphicsView in a QDialog?
I am trying to create and display a QPixmap using a QGraphicsView in a QDialog as follows:
Code:
{
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?
Re: QGraphicsView in a QDialog?
This is so very very wrong :-)
Code:
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.
Re: QGraphicsView in a QDialog?
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?
Code:
ViewerDialog
::ViewerDialog(QWidget *parent
) : ui(new Ui::ViewerDialog)
{
ui->setupUi(this);
...
pixmap = new QPixmap(ui
->view
->size
());
}
...
void ViewerDialog::updateUi()
{
...
scene->setSceneRect(ui->view->rect());
ui->view->setScene(scene);
scene->addPixmap(*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);
}
}
}
Re: QGraphicsView in a QDialog?
Yep, you're getting closer, but you still don't understand the paint event.
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.
Re: QGraphicsView in a QDialog?
Thanks--that was it! What was confusing me was the Qt help text:
Quote:
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.
Re: QGraphicsView in a QDialog?
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 ?
Re: QGraphicsView in a QDialog?
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.
Quote:
Originally Posted by
aamer4yu
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 ?
Re: QGraphicsView in a QDialog?
What would prevent
Code:
...
...
pixmap = new QPixmap(ui
->view
->size
());
...
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.
Re: QGraphicsView in a QDialog?
scaled returns the scaled pixmap, it doesn't perform the scaling on the pixmap ;-)
Re: QGraphicsView in a QDialog?
Sorry. I've been looking at this too long. Got a nice image now... Thanks a million.
Quote:
Originally Posted by
tbscope
scaled returns the scaled pixmap, it doesn't perform the scaling on the pixmap ;-)