2015-11-07 39 views
1

我需要使用python檢查qt ui中的多個單選按鈕。 到目前爲止,我們使用的是類似於:試圖用python檢查多個qt單選按鈕

if main.ui.radioButton_1.isChecked(): 
    responses["q1"] = "1" 
elif main.ui.radioButton_2.isChecked(): 
    responses["q1"] = "2" 
elif main.ui.radioButton_3.isChecked(): 
    responses["q1"] = "3" 

if main.ui.radioButton_4.isChecked(): 
    responses["q2"] = "1" 
elif main.ui.radioButton_5.isChecked(): 
    responses["q2"] = "2" 
elif main.ui.radioButton_6.isChecked(): 
    responses["q2"] = "3" 
... 

因爲有很多按鈕和很多不同的類別(Q1,Q2,......)我在想優化它一點點。所以,這就是我希望將工作(從How to get the checked radiobutton from a groupbox in pyqt通過):

for i, button in enumerate(["main.ui.radioButton_" + str(1) for i in range(1, 8)]): 
    if button.isChecked(): 
     responses["q1"] = str(i - 1) 

我得到爲什麼這不工作,但寫它,我希望的那樣。 所以,我想通過使用類似(Is there a way to loop through and execute all of the functions in a Python class?)東西按鈕進行迭代:

for idx, name, val in enumerate(main.ui.__dict__.iteritems()): 

,然後使用一些模3和這樣的分配結果。但那也行不通。不知道是因爲我使用了__ dict __還是別的什麼。我得到的錯誤是:

TypeError: 'QLabel' object is not iterable 

現在有些人可能會說,隱含的是更好的,如果ELIF鏈是明確的,也是可讀性,因爲好的事情是這樣的,但也有400個多行的那個。在閱讀本文後,Most efficient way of making an if-elif-elif-else statement when the else is done the most?,我認爲必須有一個更好,更有效的方法來做這件事(參見接受的答案的例子3.py和4.py)。因爲我需要檢查main.ui.radioButton_1.isChecked()的布爾值,然後根據Buttons組(q1,q2,...)分配值,所以我沒有設法使用所描述的字典來實現解決方案在文中。

我是否堅持使用if elif鏈或者是否有辦法不僅可以減少LOC,還可以使代碼更高效(更快)?

回答

1

它看起來像你使用Qt設計器來創建你的用戶界面,所以我建議把每一組單選按鈕放在QButtonGroup。這將爲您提供一個簡單的現成API,用於獲取組中的選中按鈕,而無需單獨查詢每個按鈕。

在Qt Designer中,可以通過選擇按鈕將按鈕添加到按鈕組中,然後從上下文菜單中選擇指定到按鈕組>新建按鈕組。按鈕ID(以後需要使用)按按鈕的選擇順序進行分配。因此,請使用Ctrl +單擊以正確順序選擇組中的每個按鈕。每個組的ID從1開始,對於添加到該組的每個按鈕只增加1。

當添加一個新的按鈕組時,它將出現在對象檢查器中。這將允許您選擇它並給它一個更有意義的名稱。

一旦你創建了所有的組,可以得到一組的檢查按鈕這樣的:

responses["q1"] = str(main.ui.groupQ1.checkedId()) 
    responses["q2"] = str(main.ui.groupQ2.checkedId()) 
    # etc... 

這可以進一步簡化處理在循環中的所有組:

for index in range(1, 10): 
     key = 'q%d' % index 
     group = 'groupQ%d' % index 
     responses[key] = str(getattr(main.ui, group).checkedId()) 
0

另一種方法是使用信號。如果您在應用程序中有很多單選按鈕,我懷疑這種方法會顯着加快。例如:

import sys 
from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

class MoodExample(QGroupBox): 

    def __init__(self): 
     super(MoodExample, self).__init__() 

     # Create an array of radio buttons 
     moods = [QRadioButton("Happy"), QRadioButton("Sad"), QRadioButton("Angry")] 

     # Set a radio button to be checked by default 
     moods[0].setChecked(True) 

     # Radio buttons usually are in a vertical layout 
     button_layout = QVBoxLayout() 

     # Create a button group for radio buttons 
     self.mood_button_group = QButtonGroup() 

     for i in xrange(len(moods)): 
      # Add each radio button to the button layout 
      button_layout.addWidget(moods[i]) 
      # Add each radio button to the button group & give it an ID of i 
      self.mood_button_group.addButton(moods[i], i) 
      # Connect each radio button to a method to run when it's clicked 
      self.connect(moods[i], SIGNAL("clicked()"), self.radio_button_clicked) 

     # Set the layout of the group box to the button layout 
     self.setLayout(button_layout) 

    #Print out the ID & text of the checked radio button 
    def radio_button_clicked(self): 
     print(self.mood_button_group.checkedId()) 
     print(self.mood_button_group.checkedButton().text()) 

app = QApplication(sys.argv) 
mood_example = MoodExample() 
mood_example.show() 
sys.exit(app.exec_()) 

我發現了更多的信息請訪問:

http://codeprogress.com/python/libraries/pyqt/showPyQTExample.php?index=387&key=QButtonGroupClick

http://www.pythonschool.net/pyqt/radio-button-widget/