如何在多窗口中绘画?

时间:2018-07-10 14:18:15

标签: python python-3.x pyqt pyqt5

我想问一下如何在多窗口中绘制线条。更确切地说,我想打开多窗口并在其上绘制一些东西。

在下面的代码中,我想分别在4个窗口中绘制4条线。在一个窗口中一行。但是它只显示最后的绘画结果,并且只显示一个窗口。

我一个人尝试了很多,但都失败了。有人可以帮我吗?首先十分感谢!

代码:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

lines = [[1, 1, 100, 100], [10, 10, 400, 400], [20, 20, 600, 600], [60, 60, 10, 10]]  #I want to paint 4 lines.

class Plot(QWidget):
    def __init__(self, parent=None):
        super(Plot, self).__init__(parent)

    @pyqtSlot(list)
    def getvalue(self, list_plotline):
        print("\n", list_plotline)
        self.line_start_x = list_plotline[0]
        self.line_start_y = list_plotline[1]
        self.line_end_x = list_plotline[2]
        self.line_end_y = list_plotline[3]
        print("plot line: ", self.line_start_x, self.line_start_y, self.line_end_x, self.line_end_y)

        self.setFixedHeight(600)
        self.setFixedWidth(600)

        self.show()

    def paintEvent(self, event):
        qp = QPainter()

        qp.begin(self)
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        qp.setPen(pen)
        qp.drawLine(self.line_start_x, self.line_start_y, self.line_end_x, self.line_end_y)
        qp.end()

class MyApp(QWidget):
    signal_plot = pyqtSignal(list)

    def __init__(self, parent= None):
        super(MyApp, self).__init__(parent)
        self.plotter = Plot()
        self.initUI()

    def initUI(self):
        self.btn1 = QPushButton("plot 4 lines in 4 windows", self)
        self.btn1.clicked.connect(self.start)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.btn1)
        self.setLayout(self.layout)
        self.show()

    def start(self):
        for i in range(len(lines)):  #use for loop to control
            self.signal_plot.connect(self.plotter.getvalue)
            self.signal_plot.emit(lines[i])
            self.signal_plot.disconnect(self.plotter.getvalue)

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

1 个答案:

答案 0 :(得分:1)

我不明白为什么要使用信号,在这种情况下没有必要,同样,如果要存储点,请不要使用n个变量,而只能使用一个QLine变量。

您说: 4个窗口,但是您仅创建Plot(),必须创建4:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

lines = [[1, 1, 100, 100], [10, 10, 400, 400], [20, 20, 600, 600], [60, 60, 10, 10]]  #I want to paint 4 lines.

class Plot(QDialog):
    def __init__(self, parent=None):
        super(Plot, self).__init__(parent)
        self.line = QLine()
        self.setFixedSize(600, 600)

    def setPoints(self, points):
        self.line = QLine(*points)
        self.update()

    def paintEvent(self, event):
        qp = QPainter(self)
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        qp.setPen(pen)
        qp.drawLine(self.line)

class MyApp(QWidget):
    def __init__(self, parent= None):
        super(MyApp, self).__init__(parent)
        self.plots = [Plot(self) for i in range(len(lines))]
        self.initUI()

    def initUI(self):
        self.btn1 = QPushButton("plot 4 lines in 4 windows", self)
        self.btn1.clicked.connect(self.start)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.btn1)
        self.setLayout(self.layout)
        self.show()

    def start(self):
        for plot, line in zip(self.plots, lines):  #use for loop to control
            plot.setPoints(line)
            plot.show()

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

有信号:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

lines = [[1, 1, 100, 100], [10, 10, 400, 400], [20, 20, 600, 600], [60, 60, 10, 10]]  #I want to paint 4 lines.

class Plot(QDialog):
    def __init__(self, parent=None):
        super(Plot, self).__init__(parent)
        self.line = QLine()
        self.setFixedSize(600, 600)

    @pyqtSlot(list)
    def setPoints(self, points):
        self.line = QLine(*points)
        self.update()

    def paintEvent(self, event):
        qp = QPainter(self)
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        qp.setPen(pen)
        qp.drawLine(self.line)

class MyApp(QWidget):
    signal_plot = pyqtSignal(list)

    def __init__(self, parent= None):
        super(MyApp, self).__init__(parent)
        self.plots = [Plot(self) for i in range(len(lines))]
        self.initUI()

    def initUI(self):
        self.btn1 = QPushButton("plot 4 lines in 4 windows", self)
        self.btn1.clicked.connect(self.start)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.btn1)
        self.setLayout(self.layout)
        self.show()

    def start(self):
        for plot, line in zip(self.plots, lines):  #use for loop to control
            self.signal_plot.connect(plot.setPoints)
            self.signal_plot.emit(line)
            self.signal_plot.disconnect(plot.setPoints)
            plot.show()

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