2017-12-29 29 views
0

(私は絶対PyQtは初心者です。)QListWidgetの選択でQLabelを接続し

が、私は、ユーザーが非常に下にスクロールするには、矢印キーを使用するたびソフトウェアオプションの説明をQLabelを更新したいと思いますQListWidgetに表示されるオプションの長いリスト、またはQListWidgetのオプションをクリックします。私はすでに私がしたいことをするためにクリックオプションを接続するために管理する必要がありますが、私は矢印キーのプレスを検出する方法を把握することはできません。

これは私がこれまで持っているものです。

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_()) 
  1. とQLabelを更新してください。
  2. どのようにすればいいですかuic.loadUi相対パスを使用していますか?私はのuic.loadUi( 'main.ui'、self)uic.loadUi( './ main.ui'、self)を試しましたが、両方のファイルが同じであっても動作しませんでしたフォルダ。

答えて

1

あなたが現在の項目を取得する場合、あなたは新しいアイテムが選択されるたびに発行された信号itemSelectionChanged()を使用する必要がありますitemClicked()を使用する必要はありません。パスの問題は、スクリプトの実行場所に依存するため、スクリプトの場所に依存しないために発生します。この場合、コードがファイルの完全パスを認識することが推奨されます。実装は次のセクションにあります:

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_()) 
+0

あなたの素早く非常に有用な回答ありがとう! –

関連する問題