开发者

Qt Painter outside paintEvent - impossible - workaround?

So it appe开发者_StackOverflow社区ars that Qt4 doesn't let you draw on windows outside of a paint event. I have a lot of code that expects to be able to in order to draw rubber band lines (generic drawing code for a particular, proprietary interface that I then implement in the given UI). I've read about the pixmap method, it would be a lot of work and I don't think it's really what I want.

Is there a workaround that allows me to do what I want anyway? I just need to draw XOR bands on the screen.

Tried the WA_PaintOutsidePaintEvent flag. Then I saw the bit that says it doesn't work on Windows.


In modern compositing desktops window painting needs to be synchronized by the window manager so that the alpha blending and other effects can be applied, in order, to the correct back buffers - the result of which is then flipped onto the screen to allow tear-free window animations.

Invoking painting operations out-of-band of this process - while supported for legacy reasons on the underlying platforms - would subvert this process and cause a number of very non optimal code paths to be executed.

Basically, when you have painting to do on a window: Call the invalidate function to schedule the painting soon, and paint during the paint event.


Just paint to a QPixmap, and copy it to the real widget in the paintEvent. This is the only standard way. You shouldn't try to workaround it.


Seems like if you could get access to the Hwnd of the window in question, you could paint on that surface. Otherwise, I'm not sure. If by pixmap method you mean something like this, I don't think it's a bad solution:

m_composed_image = QImage(size, QImage::Format_ARGB32);
m_composed_image.setDotsPerMeterX(dpm);
m_composed_image.setDotsPerMeterY(dpm);
m_composed_image.fill(Qt::transparent);

//paint all image data onto new image
QPainter painter(&m_composed_image);
painter.drawImage(QPoint(0, 0), m_alignment_image); 


As it's mentioned in one of the answers, The best way to do it will be to make a pixmap buffer. The painting works will be done in the buffer and when it's done, repaint() will be scheduled. And the paintEvent() function just paints the widget by copying the pixel buffer

I was trying to draw a circle on a widget area after user inputs values and pushes a button. This was my solution. connecting the drawCircle() slot to the clicked() signal.

class PaintHelper : public QWidget
{
    Q_OBJECT
private:
    QPixmap *buffer;

public:
    explicit PaintHelper(QWidget *parent = 0) : QWidget(parent)
    {
        buffer=new QPixmap(350,250);// this is the fixe width of this widget so 
        buffer->fill(Qt::cyan);
    }

signals:
public slots:
    void drawCircle(int cx, int cy, int r){


        QPainter painter(buffer);
        painter.setBrush(QBrush(QColor(0,0,255)));

        // A part of mid-point algorithm to draw 1/8 pacrt of circle   
        int x1=0,y1=r;
        int p=1-r;
        for(int i=0;y1>=x1;i++){
            painter.drawPoint(x1+cx,y1+cy);
            x1++;
            if(p>0){
                p+=3+x1;
            }
            else{
                y1--;
                p+=2*x1-2*y1;
                p++;
            }
        }
        this->repaint();
    }



    // QWidget interface
protected:
    void paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.drawPixmap(0,0,*buffer);
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜