2017-01-23 8 views
0

QXBoxLayout(twoWidgets)で使用すると、2つのカスタムウィジェット(SquareCalc、LinePainting)を使用する理由は分かりませんが、お互いに重ね合わされています。私はPyQt5PyQt5でのQWidgetの重ね合わせ

でのpython 3.5.2を使用してい

これは私のコードです:私は、問題を見つける

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

答えて

0

、私はちょうど何がちょうど私のウィジェットSquareCalc、 に固定サイズを置く必要はありません

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

self.setFixedSize(sizeX,sizeY) 

ではと置き換える追加します

ちょうどQPainterが最小サイズを持つ必要がないため、QLineEdit()およびQPushButton()、yes

関連する問題