2017-10-10 89 views
0

我想写一个比较xml的小应用程序。但是,我正在做UI的时候很困难。我有一个触发QFileDialog的按钮。我接受字符串输入并填充组合框。不幸的是,组合框保持空白。它似乎工作,当我硬编码。但我无法让应用程序动态地执行此操作。有什么我失踪?动态填充组合框Qt

下面的代码:

import sys 
from qtpy import QtCore, QtWidgets, uic 
from qtpy.QtWidgets import QMainWindow, QApplication, QFileDialog 
from qtpy.QtCore import QObject 

class CompareSiteAndRepoWindow(QMainWindow): 

    def __init__(self): 
     super(CompareSiteAndRepoWindow,self).__init__() 
     uic.loadUi('CompareSiteAndRepo.ui',self) 
     self.BrowseLRPath.clicked.connect(self.browseFile) 
     self.buttonBox.rejected.connect(self.reject) 

     self.show() 

    def reject(self): 
     self.close() 

    def browseFile(self): 

     fileDiag = QFileDialog.getOpenFileName(self, 'Open file', 
    'c:\\',"xml/html (*.xml *.html)") 

     if(not fileDiag[0]): 
      print(fileDiag[0]) 
      self.LRPathComboBox.addItem(fileDiag[0],0) 



if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    window = CompareSiteAndRepoWindow() 

    sys.exit(app.exec()) 

的CompareSiteAndRepo.ui文件

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>CompareLabToSiteDLG</class> 
<widget class="QMainWindow" name="CompareLabToSiteDLG"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>316</width> 
    <height>262</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>MainWindow</string> 
    </property> 
    <widget class="QWidget" name="centralwidget"> 
    <layout class="QGridLayout" name="gridLayout"> 
    <item row="0" column="0"> 
    <layout class="QVBoxLayout" name="verticalLayout"> 
     <item> 
     <widget class="QLabel" name="LRLabel"> 
     <property name="text"> 
     <string>Load Report</string> 
     </property> 
     </widget> 
     </item> 
     <item> 
     <widget class="QSplitter" name="splitter"> 
     <property name="orientation"> 
     <enum>Qt::Horizontal</enum> 
     </property> 
     <widget class="QComboBox" name="LRPathComboBox"/> 
     <widget class="QPushButton" name="BrowseLRPath"> 
     <property name="text"> 
      <string>Browse</string> 
     </property> 
     </widget> 
     </widget> 
     </item> 
     <item> 
     <widget class="QLabel" name="LP2Label"> 
     <property name="text"> 
     <string>LaunchPadData repo layout</string> 
     </property> 
     </widget> 
     </item> 
     <item> 
     <widget class="QSplitter" name="splitter_2"> 
     <property name="orientation"> 
     <enum>Qt::Horizontal</enum> 
     </property> 
     <widget class="QComboBox" name="LP2RepoPath"/> 
     <widget class="QPushButton" name="BrowseLP2RepoPath"> 
     <property name="text"> 
      <string>Browse</string> 
     </property> 
     </widget> 
     </widget> 
     </item> 
     <item> 
     <widget class="QDialogButtonBox" name="buttonBox"> 
     <property name="standardButtons"> 
     <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> 
     </property> 
     </widget> 
     </item> 
    </layout> 
    </item> 
    </layout> 
    </widget> 
    <widget class="QMenuBar" name="menubar"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>316</width> 
    <height>26</height> 
    </rect> 
    </property> 
    </widget> 
    <widget class="QStatusBar" name="statusbar"/> 
</widget> 
<resources/> 
<connections/> 
</ui> 

回答

0

问题的声明if (not fileDiag[0]):fileDialog[0]是一个文本,如果我们评价它为布尔值将会返回True任何除了它是空的文本,如果你否认它将是假如果文本不是空的或真如果是,这是违背你想要的:

fileDiag[0] not fileDiag[0] 
+--------------+--------------+ 
""    True 
"some text"  False 

一种解决方案是抑制不:

if fileDiag[0]: 
    [...] 

但另一种解决方案是比较,如果该文本不是空的,因为我在下面:

def browseFile(self): 
    filename, _ = QFileDialog.getOpenFileName(self, 'Open file', 'c:\\',"xml/html (*.xml *.html)") 
    if filename != "": 
     self.LRPathComboBox.addItem(filename, 0)