使用QListWidget选择连接QLabel

2024-09-28 13:18:38 发布

您现在位置:Python中文网/ 问答频道 /正文

(我是一个绝对的PyQt初学者。)

每当用户使用箭头键向下滚动QListWidget中显示的很长的选项列表或单击QListWidget中的选项时,我都希望用软件选项的描述更新QLabel。我已经成功地连接了click选项来做我想做的事情,但是我不知道如何检测箭头键的按下。你知道吗

到目前为止,我的情况是:

主.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>

测试.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_())
  1. 如何检测箭头键的按下并更新QLabel。你知道吗
  2. 如何使用uic.loadUi文件有相对路径吗?我试过uic.loadUi文件('主.ui,自我)uic.loadUi文件('./主.ui,self),但即使两个文件位于同一文件夹中,它也无法工作。你知道吗

Tags: namerectselfui选项propertywidgetitem
1条回答
网友
1楼 · 发布于 2024-09-28 13:18:38

无需使用itemClicked()如果要获取当前项,则必须使用每次选择新项时发出的信号itemSelectionChanged()。造成路径问题的原因是,路径取决于执行脚本的位置,而不是脚本所在的位置,对于这些情况,建议代码识别文件的完整路径。具体实施步骤如下:

import os
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__()
        dirname = os.path.dirname(os.path.abspath(__file__))
        uic.loadUi(os.path.join(dirname,'main.ui'), self)
        self.accepted.connect(self.read_value)
        self.lwOptions.addItems(self.listOptions)
        self.lbDescription.setText(self.dicDescriptions[self.listOptions[0]])
        self.lwOptions.itemSelectionChanged.connect(self.update_description)

    def update_description(self):
        currentItem = self.lwOptions.currentItem().text()
        self.lbDescription.setText(self.dicDescriptions[currentItem])

    def read_value(self):
        currentItem = self.lwOptions.currentItem().text()
        QMessageBox.information(self, "Selection", "You've selected: " + currentItem)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = GUI()
    window.show()
    sys.exit(app.exec_())

相关问题 更多 >

    热门问题