[英]Combo box index value returning to default 0 when clicked radio buttons or change spin boxes in PyQt
我正在嘗試將組合框與 PyQt5 中的另一個組合框、單選按鈕和旋轉框連接起來。 問題是每次觸發最后一個組合框並且我單擊單選按鈕或另一個組合框時,最后一個組合框跳轉到默認索引 0。在我的示例中,最后一個組合框包含 RA、RB 和 RC。 如果我單擊單選按鈕中的 R,然后從組合框中選擇 RC,那么當我更改為 T 時,它會跳轉到 TA。 我想要的是腳本記住選項 C 並且當我在單選按鈕中從 R 更改為 T 時,相應地返回 RC 或 TC,而不跳轉到 RA 或 TA。 我無法在第一個組合框中重新創建到默認索引值的跳轉,旋轉框也沒有,但我希望您理解我的問題。 如何存儲從組合框中選擇的組合框索引,何時可以調用它以避免它返回到 0?
我試過 setCurrentIndex 但它只在我打開程序時有效。 每次我在組合框中選擇一個項目時,它都會變回 currentIndex,而不是選擇的選項。
我也試過 currentIndexChanged 但它似乎不能正常工作。 有任何想法嗎?
這是我重新創建的代碼的一部分:
import sys
from functools import partial
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, \
QHBoxLayout, QRadioButton, QMainWindow
class ComboWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ComboWidget, self).__init__(parent)
self.radio_buttons = {}
self.combo_boxes = {}
self.setGeometry(50, 50, 500, 500)
layout = QtWidgets.QHBoxLayout(self)
radio_button_names = ['R',
'T',
]
for name in radio_button_names:
self.radio_buttons[name] = QtWidgets.QRadioButton(name)
self.radio_buttons[name].toggled.connect(self._radio_button_toggled)
layout.addWidget(self.radio_buttons[name])
combo_box_names = ['RT', 'select_plot_type']
for name in combo_box_names:
self.combo_boxes[name] = QtWidgets.QComboBox()
self.combo_boxes[name].currentIndexChanged.connect(partial(self.indexChanged, name))
layout.addWidget(self.combo_boxes[name])
self.indexChanged(name, self.combo_boxes[name].currentIndex())
self.combo_boxes['RT'].addItems(['R', 'T'])
self.combo_boxes['select_plot_type'].addItems(['R A', 'R B', 'R C'])
self.show()
def indexChanged(self, name, combo_box_index):
print('combo box changed')
if combo_box_index == -1:
return
if name == 'select_plot_type':
self.combo_boxes['select_plot_type'].itemData(combo_box_index)
if name == 'RT':
self.combo_boxes['RT'].itemData(combo_box_index)
def _radio_button_toggled(self):
self.combo_boxes['RT'].clear()
self.combo_boxes['select_plot_type'].clear()
if self.radio_buttons['R'].isChecked()==True:
self.combo_boxes['RT'].addItems(['R1', 'R2', 'R3'])
self.combo_boxes['select_plot_type'].addItems(['R A', 'R B', 'R C'])
elif self.radio_buttons['T'].isChecked()==True:
self.combo_boxes['RT'].addItems(['T1', 'T2', 'T3'])
self.combo_boxes['select_plot_type'].addItems(['T A', 'T B', 'T C'])
else:
return
def main():
app = QtWidgets.QApplication(sys.argv)
w = ComboWidget()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在您的解決方案中,您正在消除和添加項目,以便默認情況下它將建立在導致您觀察到的問題的初始位置。 一種可能的解決方案是在刪除項目之前存儲 currentIndex,然后在建立新項目時將其設置為 currentIndex,但更強大的解決方案是使用具有多列的模型並根據選中的 QRadioButton 更改 modelColumn。
import sys
from PyQt5 import QtGui, QtWidgets
class ComboWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(ComboWidget, self).__init__(parent)
options = ["R", "T"]
self.setGeometry(50, 50, 500, 500)
layout = QtWidgets.QHBoxLayout(self)
group = QtWidgets.QButtonGroup(self)
for i, option in enumerate(options):
radiobutton = QtWidgets.QRadioButton(option)
group.addButton(radiobutton, i)
layout.addWidget(radiobutton)
if i == 0:
radiobutton.setChecked(True)
for values in (("{}1", "{}2", "{}3"), ("{} A", "{} B", "{} C")):
combobox = QtWidgets.QComboBox()
self.fill_model(combobox, values, options)
group.buttonClicked[int].connect(combobox.setModelColumn)
combobox.setModelColumn(group.checkedId())
layout.addWidget(combobox)
def fill_model(self, combobox, values, options):
model = QtGui.QStandardItemModel(self)
for i, text in enumerate(values):
for j, option in enumerate(options):
it = QtGui.QStandardItem(text.format(option))
model.setItem(i, j, it)
combobox.setModel(model)
def main():
app = QtWidgets.QApplication(sys.argv)
w = ComboWidget()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.