Qt:QWidget :: paintEngine:不应再被调用了

时间:2014-09-11 07:15:52

标签: c++ qt qpainter

我试图制作一个可以用手指画在画布上的应用程序 为了实现这一点,我将QWidget子类化为MFCanvas,在QML中注册了类 qmlRegisterType<>(),实现虚拟paintEvent();功能,和 在paintEvent()中使用QPainter绘制它;功能。

问题:
在施工时,QPainter会发出警告:

QWidget::paintEngine: Should no longer be called

然后,抛出其他几个相关的警告:

QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active

难怪:QPainter没有画任何东西......
另外,我应该调用paintEvent();我自己呢? 或者是否应该通过QWidget调用每一帧,我以某种方式搞砸了它?
我搜索了网络,但我发现的所有帖子都没有回复,或者他们在哪里 使用QWidget之外的其他东西
我的代码:

mfcanvas.cpp:

#include "mfcanvas.h"
#include <QDebug>
#include <QPainter>
#include <QVector2D>
#include <QList>

MFCanvas::MFCanvas(QWidget *parent) : QWidget(parent)
{
    paths = new QList<QList<QVector2D>*>();
    current = NULL;
    QWidget::resize(100, 100);
}

MFCanvas::~MFCanvas()
{
    delete paths;
}

void MFCanvas::paintEvent(QPaintEvent *)
{
    if(current!=NULL){
        if(current->length() > 1){
            QPainter painter(this);
            painter.setPen(Qt::black);
            for(int i = 1; i < current->length(); i++){
                painter.drawLine(current->at(i-1).x(), current->at(i-1).y(), current->at(i).x(), current->at(i).y());
            }
        }
    }
}

void MFCanvas::pressed(float x, float y)
{
    if(current==NULL){
        qDebug() << "null:"<<current;
        current = new QList<QVector2D>();
        current->append(QVector2D(x, y));
    }else{
        qDebug() << "current:"<<current;
    }
    paintEvent(NULL);
}

void MFCanvas::update(float x, float y)
{
    current->append(QVector2D(x, y));
}

void MFCanvas::resize(int w, int h)
{
    QWidget::resize(w, h);
}

main.cpp中:

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QSurfaceFormat>
#include "creator.h"
#include "mfcanvas.h"

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

    qmlRegisterType<MFCanvas>("com.cpp.mfcanvas", 1, 0, "MFCanvas");

    QQmlApplicationEngine engine;

    QQmlComponent *component = new QQmlComponent(&engine);
    QObject::connect(&engine, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));

    Creator creator(component);
    QObject::connect(component, SIGNAL(statusChanged(QQmlComponent::Status)), &creator, SLOT(create(QQmlComponent::Status)));

    component->loadUrl(QUrl("qrc:///main.qml"));

    int rv;

    rv = app.exec();
    delete component;
    return rv;
}

creator.cpp:

#include "creator.h"
#include <QQuickWindow>
#include <QDebug>

Creator::Creator(QQmlComponent *component)
{
    this->component = component;
}

void Creator::create(QQmlComponent::Status status)
{
    if(status == QQmlComponent::Ready){
        QObject *topLevel = component->create();
        QQuickWindow::setDefaultAlphaBuffer(true);
        QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);

        QSurfaceFormat surfaceFormat = window->requestedFormat();
        window->setFormat(surfaceFormat);
        window->show();
    }
}

main.qml :(重要部分)

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.2
import QtQuick.Layouts 1.1
import QtQuick.Window 2.0
import com.cpp.mfcanvas 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("MFCanvas")

    onSceneGraphInitialized: {
        drawMenu.visible = true;
        lineWidth.visible = true;
        colorMenu.visible = true;
        drawMenu.visible = false;
        lineWidth.visible = false;
        colorMenu.visible = false;
    }

    Rectangle {
        id: main
        anchors.fill: parent

        property real toolsH: 15
        property real iconW: 25

        property real menuH: 8
        property real menuW: 16

        property real dpi: (Screen.logicalPixelDensity == undefined ? 6 : Screen.logicalPixelDensity) * 1.5

        property color choosenColor: Qt.hsla(hue.value, saturation.value, luminance.value, 1)

        Text {
            anchors.centerIn: parent
            font.pointSize: 60
            text: "MFCanvas"
        }

        MFCanvas {
            id: canvas
            Component.onCompleted: {
                canvas.resize(main.width, main.height);
            }
        }
    //...
    }
}

告诉我您是否需要任何其他信息 先感谢您! =)

3 个答案:

答案 0 :(得分:4)

这里有很好的解释:

https://forum.qt.io/topic/64693

简而言之:不要试图直接从输入事件处理程序绘制, 但是重载窗口小部件中的paintEvent方法并创建 QPain在那里。仅使用输入事件来修改内部 数据模型并使用paintEvent中的QPainter在输出路径上显示它。

答案 1 :(得分:1)

在你的mfcanvas.cpp,void MFCanvas::pressed(float x, float y)函数中,行

paintEvent(NULL);

似乎令人不安。用类似的代码试了一下 - 我得到了同样的错误。

建议的解决方案:使用this->repaint()this->update()代替paintEvent(NULL)来重新绘制小部件似乎更合适。

可能的解释:看起来像 paintEvent()不应该直接调用(比如 paintEvent(),当< em> paint()函数被调用)。据我在QPainter文档中理解,QPainter与QPaintDevice和QPaintEngine一起工作,这三者构成了绘画的基础。错误QWidget::paintEngine: Should no longer be called使其非常直接。行

QPainter::begin: Paint device returned engine == 0, type: 1 
QPainter::setPen: Painter not active

可能表明这位画家的QPaintDevice(如QPaintDevice::paintEngine)没有提供QPaintEngine。可以假设绘制设备本身生成或以其他方式调用此QPaintEngine,例如,在窗口小部件上调用 paint()函数时。

答案 2 :(得分:1)

我自己找到了一个简单的解决方案:

而不是从QWidget派生,而是从QQuickPaintedItem派生。 QQuickPaintedItem是一个完全符合我需要的类:使用QPainter在QML元素上绘制。这是守则(缩小到基本部分):
mfcanvas.h:

class MFCanvas : public QQuickPaintedItem
{
    Q_OBJECT
public:
    explicit MFCanvas(QQuickItem *parent = 0);
    ~MFCanvas();

protected:
    void paint(QPainter *painter);

mfcanvas.cpp:

void MFCanvas::paint(QPainter *painter)
{
    painter->translate(-translation.x(), -translation.y());
    //...
}

正如您所看到的,提供了一个简单的paint()函数,它将指针移交给QPainter,随时可以使用。 =)