views:

83

answers:

2

QT4, QTCreator

I am trying to draw inside Widget:

void Widget::on_pushButton_clicked()
{
    QPainter painter;

    painter.begin(ui->label);

    QRectF rectangle(10.0, 20.0, 80.0, 60.0);
     int startAngle = 30 * 16;
     int spanAngle = 120 * 16;
     painter.drawArc(rectangle, startAngle, spanAngle);

    painter.end();
}

But when i press button nothing happens.

How to do this right way ?

+3  A: 

You need to override paintEvent() and do your painting there. You don't really need the begin() and end(). Declare the painter with

QPainter painter(this);

The constructor will handle begin(), and end() will be called when the painter object goes out of scope and is destroyed.

You also won't need the click event to trigger the painting. paintEvent() will be called whenever the widget needs to draw itself. You could use the the button click to toggle a boolean that the paintEvent() checks to determine whether or not it should display the rectangle and arc. Just make sure you call update() after you toggle the variable.

void Widget::on_pushButton_clicked()
{
    drawShapes = !drawShapes;
    update();
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    if(drawShapes)
    {
        QRectF rectangle(10.0, 20.0, 80.0, 60.0);
        int startAngle = 30 * 16;
        int spanAngle = 120 * 16;
        painter.drawArc(rectangle, startAngle, spanAngle);
    }
}

UPDATE:

To avoid having to override the paintEvent() of a widget, you could use a QLabel and assign a pixmap to it and paint to that. Note: As far as I can tell, you will need to set the pixmap each time you modify it.

void MainForm::slot_buttonClick()
{
    QPixmap pixmap(100,100);
    pixmap.fill(QColor("transparent"));

    QPainter painter(&pixmap);
    painter.setBrush(QBrush(Qt::black));
    painter.drawRect(10, 10, 100, 100);

    label.setPixmap(pixmap);
}
Arnold Spence
Is there any way to paint without overriding? Or is there any component on which i can draw without any event, like TImage or TForm or any other component containing TCanvas in VCL(Delphi)? (In Delphi could just write Form1.Canvas.Circle(...) whenever i need it, not only inside onpaint event)
Astronavigator
I've never actually tried drawing on a widget with code external to the widget but QWidget does inherit PaintDevice so it should be possible.
Arnold Spence
Scratch that, from the docs for QPainter.. "Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting."
Arnold Spence
You could use a QLabel and QPixmap, draw on the pixmap and assign it to the label. I added an update with code above.
Arnold Spence
A: 

If you overwrite the paint-method as described by Arnold Spence, you should call the parent's paintEvent or you end up with a widget that only shows your rectangle on a white background.

Sebastian N.