Question

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??

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top