简体   繁体   中英

Superposition of QWidget in PyQt5

I don't know why but my 2 custom widgets(SquareCalc, LinePainting) when i use them in QXBoxLayout(twoWidgets) , there are being superimposed each other one.

I'm using python 3.5.2 with PyQt5

This is my code:

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

I find the problem , i just need no put a fixed size to my widget SquareCalc, just add

self.setFixedSize(sizeX,sizeY)

In replace of :

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

Just because QPainter no need to have a minimum size , on the contrary QLineEdit() and QPushButton(), yes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM