簡體   English   中英

動態填充comboBox Qt

[英]dynamically populate comboBox Qt

我正在嘗試編寫一個比較xml的小應用程序。 但是沒錯,我在做UI時遇到了困難。 我有一個觸發QFileDialog的按鈕。 我輸入字符串並填充組合框。 不幸的是,組合框保持為空。 當我對其進行硬編碼時,它似乎可以工作。 但是我無法讓該應用程序動態地執行此操作。 我有什么想念的嗎?

這是代碼:

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>

問題是語句if (not fileDiag[0]): fileDialog[0]是一個文本,如果我們將其評估為布爾值,則對任何文本(除了為空)都將返回True;如果您拒絕,則該文本為False不為空,如果為True,則為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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM