views:

51

answers:

1

Hell-o all, Im new to qt and I am having trouble drawing one single point.

I have a big qMainWindow that eventually calls a QgraphicsScene and inside of it I need to draw a single point, one little pixel, that is all I want, I want to use a QPainter but Im having trouble instantiating one. Any ideas??

+2  A: 

Sample code of what you're trying to do would help alot here.

QPainter use: Construct your QPainter object pointing at a canvas object, and then call painter_obj.drawPoint(x,y);. Note that the QPainter needs to be created on the stack, not the heap, so that the destructor of the object can kick off the actual drawing.

The example from the docs:

void SimpleExampleWidget::paintEvent(QPaintEvent *)
 {
     QPainter painter(this);
     painter.setPen(Qt::blue);
     painter.setFont(QFont("Arial", 30));
     painter.drawText(rect(), Qt::AlignCenter, "Qt");
 }

QGraphicsScene use: Usually, you use a QGraphicsScene to manage a large number of objects floating around a view at the same time. This is overkill for a simple drawing widget. QGraphicsScene is not, iirc, a valid canvas for a QPainter to paint on.

Instead, you create a QGraphicsItem (or subclass) of the appropriate type, and override the paint method. When your QGrpahicsItem is added to the Scene, the library will pass you a QPainter object to use to handle your drawing when appropriate.

jkerian
You don't "need" to create the `QPainter` on the stack. The way you have it is certainly the way I would do it too but using `new`/`delete` would have worked fine as well.
Troubadour
Hey, very usefull u guys, I am trying to use this for a computer graphics asignment and although this wasnt exactly the solution it gave me ideas, thanks
Zloy Smiertniy