在 PyQt 中的 QTextEdit 上绘制垂直线 [英] Draw vertical lines on QTextEdit in PyQt

查看:108
本文介绍了在 PyQt 中的 QTextEdit 上绘制垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个包含 QTextEdit 小部件的 GUI.当 gui 加载时,它会从文件中提取数据,其中数据位于固定宽度的列中.

我希望用户能够单击 QTextEdit 小部件中的各个点,以标记新列开始的位置,并且我希望在这些位置的小部件上绘制垂直线,以显示列.

在我的 GUI init() 方法中,我有以下行来拦截来自文本小部件的paintEvent:

self.mytextviewer.paintEvent = self.handlePaintEvent

我有一个 handlePaintEvent() 方法:

def handlePaintEvent(self, event):画家 = QPainter(self.mytextviewer)pen = QPen(Qt.SolidLine)pen.setColor(Qt.black)pen.setWidth(1)Painter.setPen(钢笔)Painter.drawLine(20, 0, 20, 100)

但是,当我尝试运行代码时,我开始收到有关画家未处于活动状态的 QPainter 错误.

然后我尝试了一个不同的方向,继承 QTextEdit 并将与上面基本相同的代码添加到我的子类的paintEvent()方法中.但是我仍然收到错误.

然后我尝试将 painter.begin(self)painter.end() 添加到paintEvent() 方法中,但也没有任何乐趣.>

此外,由于我添加了自定义的paintEvent() 方法,因此不再显示最初显示在小部件中的文本.

我是在做一些非常愚蠢的事情,还是有更好/更简单的方法来解决这个问题?

谢谢.

解决方案

我找到了一个答案,希望它可以帮助其他人......

在paintEvent()中创建QPainter的实例时,您需要为QPainter提供widgets视口.

要让它显示文本,请包含父类的 super() 方法.

defpaintEvent(self, event):画家 = QPainter(self.viewport())pen = QPen(Qt.SolidLine)pen.setColor(Qt.black)pen.setWidth(1)Painter.setPen(钢笔)Painter.drawLine(20, 0, 20, 100)super(TextWidgetWithLines, self).paintEvent(event)

I am trying to develop a GUI that contains a QTextEdit widget. When the gui loads, it pulls in data from a file where the data is in columns of fixed widths.

I want the user to be able to click at various points in the QTextEdit widget, to mark the positions where new columns start, and I want vertical lines to be drawn on the widget at those positions, to show the columns.

In my GUI init() method I had the following line to intercept the paintEvent from the text widget:

self.mytextviewer.paintEvent = self.handlePaintEvent

and I had a handlePaintEvent() method:

def handlePaintEvent(self, event):
    painter = QPainter(self.mytextviewer)
    pen = QPen(Qt.SolidLine)
    pen.setColor(Qt.black)
    pen.setWidth(1)
    painter.setPen(pen)
    painter.drawLine(20, 0, 20, 100)

However when I tried to run the code I started to get QPainter errors about the painter not being active.

I then tried a different direction, subclassing QTextEdit and adding basically the same code as above to the paintEvent() method of my subclass. However I am still getting the errors.

I then tried adding painter.begin(self) and painter.end()to the paintEvent() method, but had no joy with that either.

Also, the text that was initially being displayed in the widget is no longer displayed since I added my custom paintEvent() method.

Am I doing something really stupid here, or is there a better/easier way to go about this?

Thanks.

解决方案

I found an answer, hopefully it might help someone else....

You need to supply QPainter with the widgets viewport when creating an instance of QPainter in the paintEvent().

To get it to display the text, include the super() method of the parent class.

def paintEvent(self, event):
    painter = QPainter(self.viewport())
    pen = QPen(Qt.SolidLine)
    pen.setColor(Qt.black)
    pen.setWidth(1)
    painter.setPen(pen)
    painter.drawLine(20, 0, 20, 100)
    super(TextWidgetWithLines, self).paintEvent(event)

这篇关于在 PyQt 中的 QTextEdit 上绘制垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆