【问题标题】:QWidget::setLayout: Attempting to set QLayout "" on Window "", which already has a layoutQWidget::setLayout: 试图在已经有布局的窗口“”上设置 QLayout“”
【发布时间】:2016-05-04 21:37:24
【问题描述】:

正如标题所述,我在尝试将表单布局设置为窗口时遇到问题 - 我不确定“已经有布局”的确切含义,因为我没有明确调用 setLayout,所以是有一些默认布局可以应用于窗口吗?还是和我的超级电话有关?

搜索没有给我一个正确的答案(至少不是我的情况),所以我想我会发一个帖子。

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

#Window class
class Window(QMainWindow):
    def __init__(self): # constructor for Window (passes "self" -aka Window- to __init__ to initialize it)
        super(Window, self).__init__() #inherits from QMainWindow
        self.setGeometry(50,50,800,600) #Set window dimensions
        self.setWindowTitle("Google Music Playlist Transfer") #Set window title
        self.setWindowIcon(QIcon('gmusic.png')) #Set window icon
        self.home()

    def home(self):     
        #Set email field
        email = QLineEdit()
        email.setMaxLength(110)
        email.setAlignment(Qt.AlignLeft)

        #Set password field
        pwd = QLineEdit()
        pwd.setAlignment(Qt.AlignLeft)
        pwd.setEchoMode(QLineEdit.Password)

        #Form layout
        layout = QFormLayout()
        layout.addRow("Email: ", email)
        layout.addRow("Password: ", pwd)

        #Login button
        login_btn = QPushButton("Login", self) #login button
        login_btn.clicked.connect(QCoreApplication.instance().quit) #tell button what to do
        login_btn.resize(100, 100)
        login_btn.move(100,100)
        self.setLayout(layout)
        self.show() #Show window

【问题讨论】:

    标签: python-3.x pyqt5


    【解决方案1】:

    我为任何未来的路人解决了这个问题(虽然我不知道这是一个问题的确切原因,也许有人可以在评论中扩展它) - 我是从 QWindow 而不是 QWidget 继承的,显然这会导致一些设置布局的一些问题 - 再次,不知道为什么,但将其更改为 QWidget 确实解决了问题!

    【讨论】:

    • 感谢上帝。那会花费我很多年
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多