如何获取QComboBox的价值?
How to get value of QtComboBox?
我正在使用 QGIS 插件生成器模板创建 QGIS 插件。
def __init__(self, iface):
#some code
self.dlg = QtGui.QDialog();
main_layout = QtGui.QVBoxLayout()
city = QtGui.QComboBox()
city.addItem("Tucson")
city.addItem("Austin")
city_label = QtGui.QLabel("City", city)
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self.dlg)
buttons.accepted.connect(self.dlg.accept)
buttons.rejected.connect(self.dlg.reject)
main_layout.addWidget(city)
self.dlg.setLayout(main_layout)
def run(self):
self.dlg.show()
result = self.dlg.exec_()
if result:
selected_city = city.currentText()
print selected_city
pass
我收到一条错误消息,因为无法识别 "city"。每当用户更改值时,如何在 QTComboBox 中获取所选值?有没有更简单的方法来创建 UI?我基本上只需要在用户每次选择不同的值时更新城市(和其他一些)信息,并每 5 分钟更改一次组合框中的选项。
在你说 'city' 的地方你应该说 'self.city' 这样城市就附加到对象上了。然后你可以得到它的文本 'self.city.currentText()'.
我正在使用 QGIS 插件生成器模板创建 QGIS 插件。
def __init__(self, iface):
#some code
self.dlg = QtGui.QDialog();
main_layout = QtGui.QVBoxLayout()
city = QtGui.QComboBox()
city.addItem("Tucson")
city.addItem("Austin")
city_label = QtGui.QLabel("City", city)
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
Qt.Horizontal, self.dlg)
buttons.accepted.connect(self.dlg.accept)
buttons.rejected.connect(self.dlg.reject)
main_layout.addWidget(city)
self.dlg.setLayout(main_layout)
def run(self):
self.dlg.show()
result = self.dlg.exec_()
if result:
selected_city = city.currentText()
print selected_city
pass
我收到一条错误消息,因为无法识别 "city"。每当用户更改值时,如何在 QTComboBox 中获取所选值?有没有更简单的方法来创建 UI?我基本上只需要在用户每次选择不同的值时更新城市(和其他一些)信息,并每 5 分钟更改一次组合框中的选项。
在你说 'city' 的地方你应该说 'self.city' 这样城市就附加到对象上了。然后你可以得到它的文本 'self.city.currentText()'.