C++ 我如何用 QPainter 绘画?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/707492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:51:27  来源:igfitidea点击:

How do I paint with QPainter?

c++qt

提问by chester89

I have started learning Qt recently.
I did not get quite clear how can I paint using QPainterclass. Let`s say I want just to place a few points in the window:

我最近开始学习Qt。
我不太清楚如何使用QPainter类进行绘画。假设我只想在窗口中放置几个​​点:


class PointDrawer: public QWidget {
    Q_OBJECT
private:
    QPainter p;
public:
    PointDrawer(QWidget* obj=0): QWidget(obj), p(this) {}
    virtual void paintEvent(QPaintEvent*) {
        p.setPen(QPen(Qt::black, 3));
        int n = 8;
        while(...) {
            qreal fAngle = 2 * 3.14 * i / n;
            qreal x = 50 + cos(fAngle) * 40;
            qreal y = 50 + sin(fAngle) * 40;
            p.drawPoint(QPointF(x, y));
                        i++;
        }
    }
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    PointDrawer drawer;

    drawer.resize(200, 200);
    drawer.show();

    return app.exec();
}

And after that, I got nothing!
Can you please tell me where I am wrong?

在那之后,我什么都没有!
你能告诉我我错在哪里吗?

采纳答案by Caotic

I think the problem is your QPainterinitialization.

我认为问题在于您的QPainter初始化。

You could just create the QPainterlike in hydroes' answer, it would look like this then:

您可以QPainter在 Hydroes 的回答中创建类似的内容,然后看起来像这样:

class PointDrawer: public QWidget {
    Q_OBJECT
public:
    PointDrawer(QWidget* obj=0): QWidget(obj) {}
    virtual void paintEvent(QPaintEvent*) {
        QPainter p(this)
        p.setPen(QPen(Qt::black, 3));
        int n = 8;
        while(...) {
                qreal fAngle = 2 * 3.14 * i / n;
                qreal x = 50 + cos(fAngle) * 40;
                qreal y = 50 + sin(fAngle) * 40;
                p.drawPoint(QPointF(x, y));
                        i++;
        }
    }
}

It could also use something like this, but I don't really recommend it (I just prefer the other solution):

它也可以使用这样的东西,但我并不真正推荐它(我只是更喜欢其他解决方案):

class PointDrawer: public QWidget {
    Q_OBJECT
private:
    QPainter p;
public:
    PointDrawer(QWidget* obj=0): QWidget(obj) {}
    virtual void paintEvent(QPaintEvent*) {
        p.begin(this);
        p.setPen(QPen(Qt::black, 3));
        int n = 8;
        while(...) {
                qreal fAngle = 2 * 3.14 * i / n;
                qreal x = 50 + cos(fAngle) * 40;
                qreal y = 50 + sin(fAngle) * 40;
                p.drawPoint(QPointF(x, y));
                        i++;
        }
        p.end();
    }
}

The QPainter::begin(this)and QPainter::end()calls are essential in the second example. In the first example, you can think of QPainter::begin(this)being called in the constructor and QPainter::end()in the destructor

QPainter::begin(this)QPainter::end()电话是在第二个例子中是必不可少的。在第一个例子中,你可以想到QPainter::begin(this)在构造函数和QPainter::end()析构函数中被调用

For the reason, I'm guessing: As QPaintDevices are usually double buffered in QT4, QPainter::end()might be where the image is transferred to the graphic memory.

出于这个原因,我猜测:由于QPaintDevices 通常在 QT4 中进行双缓冲,因此QPainter::end()可能是图像传输到图形内存的地方。

回答by Ronny Brendel

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

http://doc.qt.digia.com/4.4/qpainter.html

http://doc.qt.digi.com/4.4/qpainter.html

回答by shoosh

You need to initialize the painter with the widget you want to paint on.
Usually this is done using the constructor which takes a QPaintDevicebut you can also do it by calling begin().

您需要使用要在其上绘画的小部件来初始化画家。
通常这是使用带有 a 的构造函数完成的,QPaintDevice但您也可以通过调用begin().