【发布时间】:2018-06-09 17:36:51
【问题描述】:
(我绝对是 PyQt 初学者。)
每当用户使用箭头键向下滚动显示在 QListWidget 中的很长的选项列表或单击 QListWidget 中的选项时,我想用软件选项的描述更新 QLabel。我已经设法连接点击选项来做我想做的事,但我不知道如何检测箭头按键。
这是我目前所拥有的:
main.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>341</width>
<height>244</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>321</width>
<height>231</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="lwOptions"/>
</item>
<item>
<widget class="QLabel" name="lbDescription">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>170</x>
<y>228</y>
</hint>
<hint type="destinationlabel">
<x>170</x>
<y>121</y>
</hint>
</hints>
</connection>
</connections>
</ui>
test.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt5 import uic, QtWidgets
from PyQt5.Qt import QMessageBox
class GUI(QtWidgets.QDialog):
listOptions = []
dicDescriptions = {}
for x in range(0, 100):
option = 'Option ' + str(x)
description = 'Description for ' + option
listOptions.append(option)
dicDescriptions[option] = description
def __init__(self):
super(GUI, self).__init__()
uic.loadUi('C:/Users/User/Desktop/main.ui', self)
self.accepted.connect(self.ReadValue)
self.lwOptions.addItems(self.listOptions)
self.lwOptions.itemClicked.connect(self.UpdateDescription)
self.lbDescription.setText(self.dicDescriptions[self.listOptions[0]])
def UpdateDescription(self):
currentItem = self.lwOptions.currentItem().text()
self.lbDescription.setText(self.dicDescriptions[currentItem])
def ReadValue(self):
currentItem = self.lwOptions.currentItem().text()
QMessageBox.information(self, "Selection", "You've selected: " + currentItem)
app = QtWidgets.QApplication(sys.argv)
window = GUI()
window.show()
sys.exit(app.exec_())
- 如何检测箭头键按下并更新 QLabel。
- 如何将 uic.loadUi 与相对路径一起使用?我试过 uic.loadUi('main.ui', self) 和 uic.loadUi('./main.ui', self),但没有即使两个文件在同一个文件夹中也无法正常工作。
【问题讨论】:
标签: python python-3.x pyqt qlistwidget qlabel