c++ - Qt:QWidget::paintEngine:不应再调用

标签 c++ qt qpainter

我正在尝试制作一个应用程序,您可以在其中用手指在 Canvas 上绘图。
为此,我继承了 QWidget作为MFCanvas , 在 QML 中注册类
qmlRegisterType<>() , 实现虚拟 paintEvent();功能,和
QPainter 在上面绘图在paintEvent();里面功能。

问题:
施工后,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);
}

主要.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;
}

创建者.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);
            }
        }
    //...
    }
}

如果您需要任何其他信息,请告诉我。
先感谢您! =)

最佳答案

这里有很好的解释:

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

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

关于c++ - Qt:QWidget::paintEngine:不应再调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25781353/

相关文章:

c++ - 如何在过载时强制发出 "statement has no effect"警告==

c++ - 使用 Qt Creator C++ 11,nullptr 是关键字?

c++ - 有没有一种紧凑的方法可以使 std::optional<T>::value_or 处理 T 的成员变量

android - Qt 5 在Android 上解析HTML

c++ - 如何使用 SourceOver 在 drawPixmap 上应用 alpha channel

python - 有没有什么办法可以让油漆变得更光滑,像素不可见?

c++ - 从 vector 中删除项目的有效方法

python - 在 Python 中将 Qt 图像转换为 Open CV 中的图像

c++ - 如何从打开的窗口列表中检索 QWidget

c++ - 派生 QPainterPath,QPainter 性能快速下降