2017-11-05 4 views
0

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を呼び出すだけですよね?以下は完全なコードです。私は変更したすべてのセクションにコメントしました。私はまた、色の線をもっとはっきりと(実際に描画されたときに)見るために、テキストの背景色を変更するコードをコメントアウトしました。背景色のコードは正常に動作します。PySideを使用してQWidgetサブクラスにパラメータを追加する

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_()) 
+0

は、私の解決策の仕事をしましたか? – eyllanesc

+0

はい。ありがとうございました。 – davideps

答えて

2

方法が上書きされていない場合、あなたはそれが仕事をしたい場合は、それらのパラメータを追加する必要がありますので、それはこれらが親に何度も依存するための簡単な方法があり、親の実装方法と同じになります*args**kwargsを使用して、新しいパラメータを最初のパラメータとして渡します。さらに、色はコンストラクタ内にのみ存在するため、colorの代わりにself.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()) 

出力:

enter image description here

関連する問題