【问题标题】:Golang with Qt issue with painting in paintEventGolang与Qt在paintEvent中的绘画问题
【发布时间】:2020-01-19 06:49:26
【问题描述】:

我正在使用这个 Golang Qt 绑定 here 创建一个简单的代码编辑器。我正在将paintEvent 回调处理程序连接到实际的编辑器,我正在尝试在其中进行绘画。正如我在各种论坛中发现的那样,这是应该完成绘画的唯一点。 但是,当调用painter := gui.NewQPainter2(ce.editor) 时,我会收到一些警告输出

QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1

当调用 setPen 函数时,我会收到消息

QPainter::setPen: Painter 未激活

这是该问题的一个工作示例

package main


import (
    "os"

    "github.com/therecipe/qt/widgets"
    "github.com/therecipe/qt/gui"
    "github.com/therecipe/qt/core"
)

type CodeEditor struct {
    editor *widgets.QPlainTextEdit
}

func NewCodeEditor(parent *widgets.QWidget) *CodeEditor {
    codeEditor := &CodeEditor{editor: widgets.NewQPlainTextEdit(parent)}
    codeEditor.setupSignals()
    return codeEditor
}

func (ce *CodeEditor) setupSignals() {
    ce.editor.ConnectPaintEvent(ce.paintEvent)
}

func (ce *CodeEditor) paintEvent(event *gui.QPaintEvent) {
    painter := gui.NewQPainter2(ce.editor)
    color := gui.NewQColor6("red")
    painter.SetPen2(color)
    painter.DestroyQPainter()
}

func main() {
    core.QCoreApplication_SetAttribute(core.Qt__AA_ShareOpenGLContexts, true)
    widgets.NewQApplication(len(os.Args), os.Args)

    mainWindow := widgets.NewQMainWindow(nil, 0)
    codeEditor := NewCodeEditor(nil)

    mainWindow.SetCentralWidget(codeEditor.editor)
    mainWindow.ShowMaximized()

    widgets.QApplication_Exec()
}

【问题讨论】:

    标签: qt go


    【解决方案1】:

    您尚未激活 QPainter 实例。 检查https://doc.qt.io/qt-5/qpainter.html#begin

    【讨论】:

    • NewQPainter2 会自动执行此操作,因此不需要明确的begin(...);但只是为了仔细检查我已经尝试过包含它,但我仍然遇到同样的问题
    猜你喜欢
    • 1970-01-01
    • 2019-01-16
    • 2013-03-20
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    相关资源
    最近更新 更多