【问题标题】:Adding additional parameters to QWidget subclass using PySide使用 PySide 向 QWidget 子类添加附加参数
【发布时间】:2017-11-05 07:44:58
【问题描述】:

我正在向 QWidget 的LineBand 子类添加一个颜色参数。我找到了几个如何在 Python 3 中向子类添加附加参数的示例,并且相信我已经遵循了这些建议。然而,当我使用box = LineBand(self.widget2, color) 调用新版本的类时,我收到错误File "C:/Users/...", line 63, in showBoxes ... box = LineBand(viewport, color) ... TypeError: __init__() takes 2 positional arguments but 3 were given。但是,我只是用 2 个参数调用 LineBand,对吗?下面是完整的代码。我已经评论了我更改的所有部分。我还注释掉了更改文本背景颜色的代码,以便更清楚地看到彩色线条(当它们实际绘制时)。背景颜色代码工作正常。

import sys
from PySide.QtCore import *
from PySide.QtGui import *

db = ((5,8,'A',Qt.darkMagenta),(20,35,'B',Qt.darkYellow),(45,60,'C',Qt.darkCyan)) # added color to db

class TextEditor(QTextEdit):
    def __init__(self, parent=None):
        super().__init__(parent)
        text="This is example text that is several lines\nlong and also\nstrangely broken up and can be\nwrapped."
        self.setText(text)
        cursor = self.textCursor()
        for n in range(0,len(db)):
            row = db[n]
            startChar = row[0]
            endChar = row[1]
            id = row[2]
            color = row[3]  # assign color from db to variable
            cursor.setPosition(startChar)
            cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, endChar-startChar)
            #charfmt = cursor.charFormat()
            #charfmt.setBackground(QColor(color)) # assign color to highlight (background)
            #cursor.setCharFormat(charfmt)
        cursor.clearSelection()
        self.setTextCursor(cursor)

    def getBoundingRect(self, start, end):
        cursor = self.textCursor()
        cursor.setPosition(end)
        last_rect = end_rect = self.cursorRect(cursor)
        cursor.setPosition(start)
        first_rect = start_rect = self.cursorRect(cursor)
        if start_rect.y() != end_rect.y():
            cursor.movePosition(QTextCursor.StartOfLine)
            first_rect = last_rect = self.cursorRect(cursor)
            while True:
                cursor.movePosition(QTextCursor.EndOfLine)
                rect = self.cursorRect(cursor)
                if rect.y() < end_rect.y() and rect.x() > last_rect.x():
                    last_rect = rect
                moved = cursor.movePosition(QTextCursor.NextCharacter)
                if not moved or rect.y() > end_rect.y():
                    break
            last_rect = last_rect.united(end_rect)
        return first_rect.united(last_rect)



class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.edit = TextEditor(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.edit)
        self.boxes = []

    def showBoxes(self):
        while self.boxes:
            self.boxes.pop().deleteLater()
        viewport = self.edit.viewport()
        for start, end, id, color in db:  # get color too
            rect = self.edit.getBoundingRect(start, end)
            box = LineBand(viewport, color) # call LineBand with color as argument
            box.setGeometry(rect)
            box.show()
            self.boxes.append(box)

    def resizeEvent(self, event):
        self.showBoxes()
        super().resizeEvent(event)

class LineBand(QWidget):
    def __init__(self, color):  # define color within __init__
        super().__init__(self)
        self.color = color


    def  paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(QPen(color, 1.8)) # call setPen with color
        painter.drawLine(self.rect().topLeft(), self.rect().bottomRight())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    window.showBoxes()
    app.exec_()
    sys.exit(app.exec_())

【问题讨论】:

  • 我的解决方案有效吗?
  • 是的。谢谢。

标签: python pyside qpainter qtextedit


【解决方案1】:

当一个方法未被覆盖时,它将与父方法的实现方法相同,因此如果您希望它工作,您必须添加这些参数,因为这些参数多次依赖于父方法,一个简单的方法是使用@987654323 @ 和 **kwargs 并将新参数作为第一个参数传递。另外你必须使用 self.color 而不是 color 因为 color 只存在于构造函数中。

class Window(QWidget):
    [...]
    def showBoxes(self):
        while self.boxes:
            self.boxes.pop().deleteLater()
        viewport = self.edit.viewport()
        for start, end, id, color in db:  # get color too
            rect = self.edit.getBoundingRect(start, end)
            box = LineBand(color, viewport) # call LineBand with color as argument
            box.setGeometry(rect)
            box.show()
            self.boxes.append(box)
    [...]

class LineBand(QWidget):
    def __init__(self, color, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.color = color


    def  paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(QPen(self.color, 1.8)) # call setPen with color
        painter.drawLine(self.rect().topLeft(), self.rect().bottomRight())

输出:

【讨论】:

    猜你喜欢
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多