PyQt5中QWidget的叠加

时间:2017-01-23 22:03:15

标签: python pyqt5

我不知道为什么我的2个自定义小部件(SquareCalc,LinePainting)当我在QXBoxLayout(twoWidgets)中使用它们时,彼此叠加了一个。

我使用python 3.5.2和PyQt5

这是我的代码:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QPainter, QPen
from PyQt5.QtWidgets import (QApplication, QWidget,
                             QGridLayout, QVBoxLayout, QHBoxLayout,QLabel, QLineEdit, QPushButton)

class SquareCalc(QWidget):
    def __init__(self):
        super().__init__()

    def initUI(self):

        self.setGeometry(0,0,100,100)
        self.inputLine = QLineEdit()
        self.outputLine = QLineEdit()
        self.outputLine.setReadOnly(True)

        self.inputLine.returnPressed.connect(self.calc)
        self.calcButton = QPushButton("&Calc")
        self.calcButton.clicked.connect(self.calc)


        lineLayout = QGridLayout()
        lineLayout.addWidget(QLabel("num"), 0, 0)
        lineLayout.addWidget(self.inputLine, 0, 1)
        lineLayout.addWidget(QLabel("result"), 1, 0)
        lineLayout.addWidget(self.outputLine, 1, 1)

        buttonLayout = QHBoxLayout()
        buttonLayout.addWidget(self.calcButton)

        mainLayout = QVBoxLayout()
        mainLayout.addLayout(lineLayout)
        mainLayout.addLayout(buttonLayout)

        self.setLayout(mainLayout)


    def calc(self):
        n = int(self.inputLine.text())
        r = n**2
        self.outputLine.setText(str(r))

class LinePainting(QWidget):
    def __init__(self):
        super().__init__()

    def initPainting(self):      
        self.setGeometry(0, 0, 300, 300)
        self.setWindowTitle('Pen styles')
        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self)        
        self.show()


    def paintEvent(self, e):

        qp = QPainter()
        qp.begin(self)
        self.drawLines(qp)
        qp.end()


    def drawLines(self, qp):

        pen = QPen(Qt.black, 2, Qt.SolidLine)

        qp.setPen(pen)
        qp.drawLine(20, 40, 250, 40)

        pen.setStyle(Qt.DashLine)
        qp.setPen(pen)
        qp.drawLine(20, 80, 250, 80)

        pen.setStyle(Qt.DashDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 120, 250, 120)

        pen.setStyle(Qt.DotLine)
        qp.setPen(pen)
        qp.drawLine(20, 160, 250, 160)

        pen.setStyle(Qt.DashDotDotLine)
        qp.setPen(pen)
        qp.drawLine(20, 200, 250, 200)

        pen.setStyle(Qt.CustomDashLine)
        pen.setDashPattern([1, 4, 5, 4])
        qp.setPen(pen)
        qp.drawLine(20, 240, 250, 240)




class twoWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.widget1 = SquareCalc()
        self.widget2 = LinePainting()

        mainLayout = QHBoxLayout()
        mainLayout.addWidget(self.widget1)
        mainLayout.addWidget(self.widget2)


        self.setLayout(mainLayout)
        self.setWindowTitle("Mix line/factorial")
        self.widget2.initPainting()
        self.widget1.initUI()
        self.show()




if __name__ == '__main__':

    app = QApplication(sys.argv)
    main_window = twoWidget()
    main_window.setStyleSheet(open("lol.qss", "r").read())
    main_window.show()

sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我发现问题,我只是不需要将固定大小放到我的小部件SquareCalc上, 只需添加

self.setFixedSize(sizeX,sizeY)

取代:

self.setGeometry(0, 0, 300, 300)

仅仅因为QPainter不需要最小尺寸,相反QLineEdit()和QPushButton(),是

相关问题