QFileDialog.getExistingDirectory会发生什么?

时间:2015-01-13 14:43:43

标签: pyside 3dsmax

我是Pyside的新人,我正在尝试这个项目可以在3Dsmax上运行

我对此代码的问题是:我按下“browse_btn”按钮,它无法链接到“def browse”。没有任何事情发生。

这是我的代码:

    def addnewobject():
        w = QtGui.QWidget()
        w.setFixedSize(450,90)
        w.setWindowTitle('Select folder of new objects')
        _GCProtector.widgets.append(w)
        w.show()

        folder_lb = QtGui.QLabel("Folder : ")
        directoryComboBox = QtGui.QComboBox(QtCore.QDir.currentPath())
        browse_btn=QtGui.QPushButton("Browse...",browse())
        cancel_btn = QtGui.QPushButton("Cancel")
        ok_btn = QtGui.QPushButton("OK")

        browser = QtGui.QGridLayout()
        browser.addWidget(folder_lb,0,0)
        browser.addWidget(directoryComboBox,0,1,1,4)
        browser.addWidget(browse_btn,0,5)
        browser.addWidget(cancel_btn,2,4)
        browser.addWidget(ok_btn,2,5)
        browser.setColumnStretch(1, 1)
        w.setLayout(browser)

        cancel_btn.clicked.connect(w.close)

    def browse():
        directory = QtGui.QFileDialog.getExistingDirectory("Find Files",QtCore.QDir.currentPath())

        if directory:
            if directoryComboBox.findText(directory) == -1:
                directoryComboBox.addItem(directory)

            directoryComboBox.setCurrentIndex(directoryComboBox.findText(directory))

我希望每个人都能给我一些建议。感谢

1 个答案:

答案 0 :(得分:0)

为了简化操作,您应该重新组织代码以使用类。这样,您就可以使用self以其他方式访问子窗口小部件。

以下是该课程的外观:

class Widget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.setFixedSize(450, 90)
        self.setWindowTitle('Select folder of new objects')

        self.folder_lb = QtGui.QLabel("Folder : ")
        self.directoryComboBox = QtGui.QComboBox()
        self.directoryComboBox.addItem(QtCore.QDir.currentPath())
        self.browse_btn = QtGui.QPushButton("Browse...")
        self.cancel_btn = QtGui.QPushButton("Cancel")
        self.ok_btn = QtGui.QPushButton("OK")

        browser = QtGui.QGridLayout()
        browser.addWidget(self.folder_lb, 0, 0)
        browser.addWidget(self.directoryComboBox, 0, 1, 1, 4)
        browser.addWidget(self.browse_btn, 0, 5)
        browser.addWidget(self.cancel_btn, 2, 4)
        browser.addWidget(self.ok_btn, 2, 5)
        browser.setColumnStretch(1, 1)
        self.setLayout(browser)

        self.browse_btn.clicked.connect(self.browse)
        self.cancel_btn.clicked.connect(self.close)

    def browse(self):
        directory = QtGui.QFileDialog.getExistingDirectory(
            self, "Find Files", QtCore.QDir.currentPath())    
        if directory:
            if self.directoryComboBox.findText(directory) == -1:
                self.directoryComboBox.addItem(directory)
            self.directoryComboBox.setCurrentIndex(
                self.directoryComboBox.findText(directory))

你可以像这样使用它:

def addnewobject():
   widget = Widget()
   widget.show()
    _GCProtector.widgets.append(widget)