QMainWindow会发生什么

时间:2015-01-14 09:50:53

标签: python pyside

我想用这段代码创建GUI。当我单击添加新对象按钮时,它将显示弹出窗口(我使用QMainWindown)但我想将QLabel放在这里,它无法正常工作

我不知道为什么。我希望每个人都能给我更多建议。谢谢你

这是我的代码:

from PySide import QtCore, QtGui
import sys

app = QtGui.QApplication.instance()
if not app:
    app = QtGui.QApplication([])

class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.mainLayout = QtGui.QGridLayout()
        self.mainLayout.addWidget(self.First(), 0, 0, 2, 0)
        self.setLayout(self.mainLayout)
        self.setWindowTitle("Library")
        self.resize(700,660)
    #----------------------------------------FIRST COLUMN-------------------------------------
    def First(self):
        FirstFrame = QtGui.QFrame()
        FirstFrame.setFixedSize(230,700)

    # LABEL
        renderer_lb  = QtGui.QLabel("Renderer :")
        folders_lb = QtGui.QLabel("Folder :")
    #COMBOBOX
        self.renderer_cbx = QtGui.QComboBox()
        self.renderer_cbx.addItem("Vray")
        self.renderer_cbx.addItem("Octane")
    # LIST VIEW FOLDER
        self.folders_lv = QtGui.QListView()
    # BUTTON
        addnewobject_btn = QtGui.QPushButton("Add New Objects")
        newset_btn = QtGui.QPushButton("New Set")

    # DEFINE THE FUNCTION FOR FIRST FRAME
        Firstbox = QtGui.QGridLayout()
        Firstbox.addWidget(renderer_lb,0,0)
        Firstbox.addWidget(folders_lb,2,0,1,4)
        Firstbox.addWidget(self.renderer_cbx,0,1,1,3)
        Firstbox.addWidget(self.folders_lv,3,0,1,4)
        Firstbox.addWidget(addnewobject_btn,4,0,1,2)
        Firstbox.addWidget(newset_btn,4,3)
        Firstbox.setColumnStretch(1, 1)
        FirstFrame.setLayout(Firstbox)

        addnewobject_btn.clicked.connect(self.addnewobject)

        return FirstFrame

    def addnewobject(self):
        window = QtGui.QMainWindow(self)
        window.setWindowTitle('Select folder of new objects')
        window.setFixedSize(450,90)
        window.show()

        folder_lb = QtGui.QLabel("Folder : ")

        browser = QtGui.QGridLayout()
        browser.addWidget(folder_lb,0,0)
        window.setLayout(browser)

if __name__ == '__main__':
    window = Window()
    sys.exit(window.exec_())

1 个答案:

答案 0 :(得分:1)

正如您在First()函数中所做的那样,您可以使用QFrame创建一个自制的小部件。然后,您可以为新窗口设置中央窗口小部件。

from PySide import QtCore, QtGui
import sys

app = QtGui.QApplication.instance()
if not app:
    app = QtGui.QApplication([])

class Window(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.mainLayout = QtGui.QGridLayout()
        self.mainLayout.addWidget(self.First(), 0, 0, 2, 0)
        self.setLayout(self.mainLayout)
        self.setWindowTitle("Library")
        self.resize(700,660)
        self.show()
    #----------------------------------------FIRST COLUMN-------------------------------------
    def First(self):
        FirstFrame = QtGui.QFrame()
        FirstFrame.setFixedSize(230,700)

    # LABEL
        renderer_lb  = QtGui.QLabel("Renderer :")
        folders_lb = QtGui.QLabel("Folder :")
    #COMBOBOX
        self.renderer_cbx = QtGui.QComboBox()
        self.renderer_cbx.addItem("Vray")
        self.renderer_cbx.addItem("Octane")
    # LIST VIEW FOLDER
        self.folders_lv = QtGui.QListView()
    # BUTTON
        addnewobject_btn = QtGui.QPushButton("Add New Objects")
        newset_btn = QtGui.QPushButton("New Set")

    # DEFINE THE FUNCTION FOR FIRST FRAME
        Firstbox = QtGui.QGridLayout()
        Firstbox.addWidget(renderer_lb,0,0)
        Firstbox.addWidget(folders_lb,2,0,1,4)
        Firstbox.addWidget(self.renderer_cbx,0,1,1,3)
        Firstbox.addWidget(self.folders_lv,3,0,1,4)
        Firstbox.addWidget(addnewobject_btn,4,0,1,2)
        Firstbox.addWidget(newset_btn,4,3)
        Firstbox.setColumnStretch(1, 1)
        FirstFrame.setLayout(Firstbox)

        addnewobject_btn.clicked.connect(self.addnewobject)

        return FirstFrame

    def addnewobject(self):
        secondFrame = QtGui.QFrame()
        secondFrame.setFixedSize(230,700)
    # LABEL
        folders_lb = QtGui.QLabel("Folder :")

    # DEFINE THE FUNCTION FOR FIRST FRAME
        secondGridLayout = QtGui.QGridLayout()
        secondGridLayout.addWidget(folders_lb,2,0,1,4)
        secondGridLayout.setColumnStretch(1, 1)
        secondFrame.setLayout(secondGridLayout)

        window = QtGui.QMainWindow(self)
        window.setWindowTitle('Select folder of new objects')
        window.setFixedSize(600,700)

        window.setCentralWidget(secondFrame) # Here is the main change: setLayout(QLayout) to setCentralWidget(QWidget)
        window.show()

if __name__ == '__main__':
    window = Window()
    sys.exit(window.exec_())

这是用于Maya吗?如果是的话,我建议你不要使用模态窗口,因为它会很快让用户感到厌烦。