简体   繁体   English

动态填充comboBox Qt

[英]dynamically populate comboBox Qt

I am trying to write a small app that compares xml. 我正在尝试编写一个比较xml的小应用程序。 But right I am having a hard time doing the UI. 但是没错,我在做UI时遇到了困难。 I have a button that triggers a QFileDialog. 我有一个触发QFileDialog的按钮。 I take the string input and populate a combobox. 我输入字符串并填充组合框。 Unfortunately, the combobox stays empty. 不幸的是,组合框保持为空。 It seems to work when I hardcode it. 当我对其进行硬编码时,它似乎可以工作。 But I am unable to get the app to do it dynamically. 但是我无法让该应用程序动态地执行此操作。 Is there something I am missing? 我有什么想念的吗?

Here's the code: 这是代码:

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())

the CompareSiteAndRepo.ui file 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>

the problem is the statement if (not fileDiag[0]): , fileDialog[0] is a text that if we evaluate it as boolean will return True for any text except it is empty and if you deny it will be False if the text is not empty or True if it is, which is contrary to what you want: 问题是语句if (not fileDiag[0]): fileDialog[0]是一个文本,如果我们将其评估为布尔值,则对任何文本(除了为空)都将返回True;如果您拒绝,则该文本为False不为空,如果为True,则为True,这与您想要的相反:

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

One solution is to suppress the not: 一种解决方案是抑制不:

if fileDiag[0]:
    [...]

But another solution is to compare if the text is not empty as I show below: 但是另一种解决方案是比较文本是否为空,如下所示:

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