PDA

View Full Version : QGraphicsView and QGraphicsScene



Molier
25th November 2010, 06:18
hello,
I wrote an anpplication where i use QGraphicsView widget. This widget is synchornized with a scene object (QGraphicsScene class). Because i want to show graphs on scene it is easy to put raster lines like scene.addLine(..).But how to easily draw pixels? I want to draw them similar way to WinApi, like:

SetPixel(memdc, j,((tab[j][i]-ymin)/skala)*(-1)+chartH, RGB(120,70,60));

Is there any simple way? Of course the best way is sth like a buffer which i can show at the end of drawing. Am i supposed to use QImage and then show it?
Or maybe there is possibilty to draw it on scene. I tried with QPolygon but it is only possible to show QPolygon object in one place, right? Please, give me some ideas.

By the way: i dont want to use any Qwt library.

Molier
27th November 2010, 03:13
No ideas?

I just wanna set pixels on QGraphicsScene scene. My idea is to create QImage, set pixexs on it, and finally show. Is there any better ability?

franz
27th November 2010, 10:34
Thats probably how you should do it. QGraphicsScene is about managing graphical objects, not pixels.

wysota
27th November 2010, 15:51
Create a graphics view item class for representing points or use QGraphicsEllipseItem with a small radius.

Molier
27th November 2010, 16:39
Drawing point as an elipse with minimal radius is not very elegant.
I did sth like that:
(my QGraphicsView has 370 x 270 dimensions)



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

scene=new QGraphicsScene();
scene->setSceneRect(0,0,350,250);

timer=new QTimer();

ui->graphicsView->setScene(scene);


ui->graphicsView->adjustSize();

image=QPixmap(350,250);
image.fromImage(nice);

image.fill(Qt::white);
scene->addPixmap(image); scene->addPixmap(image);
text1=scene->addText("hello",QFont());//text1->setPos(100,110);






nice.setPixel(20,20,qRgb(255, 255, 39));

scene->update();
ui->graphicsView->update();
}


And nothing is happening. No pixel with different color. Why?

wysota
27th November 2010, 17:26
Drawing point as an elipse with minimal radius is not very elegant.
What you are trying to do is even less elegant. I'd say that if I want to say "I want a red dot here" then inserting an object representing a dot is exactly that. Thinking like you if I wanted to have a line drawn I would draw a line on an image and insert that image into the scene. Doesn't make much sense if you ask me...


And nothing is happening. No pixel with different color. Why?
Why would there be a pixel with different colour if you are placing a blank white image into the scene?

Molier
27th November 2010, 20:54
Maybe. I will more explain. I have WinApi code. Using WinApi, u draw on buffer. Then u swap it to graphical output. I wanna do it easily and elegant.
With QPolygons i will have to make an array of polygons or sth. I don't want to.
This is just white, because i did not want that as a black (default is black). But note that AFTER THAT i put SetPixel. So why it does not work properly?? (even i erase image.fill(Qt::white); i still see nothing has changed).

wysota
27th November 2010, 21:57
Maybe. I will more explain. I have WinApi code. Using WinApi, u draw on buffer. Then u swap it to graphical output. I wanna do it easily and elegant.
Maybe you shouldn't be using Graphics View then?


This is just white, because i did not want that as a black (default is black). But note that AFTER THAT i put SetPixel. So why it does not work properly?? (even i erase image.fill(Qt::white); i still see nothing has changed).
You modify object A and expect object B to change. Or actually you create object B from a copy of object A then create object C from a copy of object B and when you modify object A you expect object C to change.

Molier
28th November 2010, 00:04
I know what u mean. I modified QPixmap instead of QImage. But when i did sth like that:


ui->setupUi(this);

scene=new QGraphicsScene();
scene->setSceneRect(0,0,350,250);

//timer=new QTimer();

ui->graphicsView->setScene(scene);


ui->graphicsView->adjustSize();

image=QPixmap(350,250);

nice=QImage(350,250,QImage::Format_ARGB32);
//image.fill(Qt::white);
nice.fill(qRgb(0, 0, 39));
image.fromImage(nice);
scene->addPixmap(image); scene->addPixmap(image);
text1=scene->addText("hello",QFont());//text1->setPos(100,110);






nice.setPixel(20,20,qRgb(255, 255, 100));


scene->update();
ui->graphicsView->update();


I have black scene.
In case with QGraphicsView: it is absolutely usefull down here. Why? Beucase i wanna show them in one place.Just as on th picture:
http://www.easy-upload.net/fichiers/lecran.20101127231714.jpeg

Added after 42 minutes:

QPixmap QPixmap::fromImage( const QImage & image, Qt::ImageConversionFlags flags = Qt::AutoColor ).

So it looks like it is taken as reference variable. But still no changes to see. Any ideas? I really dont want to use addEllipse, or addLine(20,20,1,1,(...)). It will draw a point maybe, but it will take more time. It is not elegant. Even it is default mechanism.
So how to SetPixel on image, show that on scene, and then once again change sth on image and show too? Any ideas?

Added after 1 4 minutes:

No response? I start thinking about
addEllipse(..) :D

wysota
28th November 2010, 00:27
I have black scene.
Somehow I'm not suprised since you are adding a blank pixmap to the scene.


So it looks like it is taken as reference variable. But still no changes to see. Any ideas?
It returns a copy.


I really dont want to use addEllipse, or addLine(20,20,1,1,(...)). It will draw a point maybe, but it will take more time.
More time than blitting a large pixmap? Doubtful :)


It is not elegant.
Then create yourself a GraphicsPointItem that will use QPainter::drawPoint() to draw the dot.


So how to SetPixel on image, show that on scene, and then once again change sth on image and show too? Any ideas?
I would start by paying attention when reading documentation of the API. If you want to shoot yourself in the foot, go ahead. But I bet my implementation using QGraphicsEllipseItem would be more efficient than yours using an image. And it would look nicer too.

Molier
28th November 2010, 01:18
Yes, i think so. Thanx a million.


by the way: some time ago i got a specific problem with setting background on QPushButton.

ui->pushButton->setPalette(QPalette(QColor(166,240,25)));

The result is as on the picture above. But on Gnome and Windows it does just not work. Buttons are without colors. Why?

Added after 27 minutes:

Shit.. That could be caused by missing color=new QColorDialog(); before. But i will see. Hopefully it will fix the problem.
Else i will write about it:)

wysota
28th November 2010, 01:23
ui->pushButton->setPalette(QPalette(QColor(166,240,25)));

The result is as on the picture above. But on Gnome and Windows it does just not work. Buttons are without colors. Why?
Windows doesn't know how to change the background of buttons. The only way to do it is with stylesheets.