1

I am new to python and pyqt/pyside ...

i make customwidget class consist of 2 label (title & desc) which is example instance to add to Listwidget later ...

here is the complete clean code (pyside maya)

import PySide.QtCore as qc
import PySide.QtGui as qg

class CustomQWidget (qg.QWidget):
    def __init__ (self, parent = None):
        super(CustomQWidget, self).__init__(parent)
        self.textQVBoxLayout = qg.QVBoxLayout()
        self.titleLabel    = qg.QLabel()
        self.description  = qg.QLabel()
        self.textQVBoxLayout.addWidget(self.titleLabel)
        self.textQVBoxLayout.addWidget(self.description)
        self.setLayout(self.textQVBoxLayout)
    def setTitle (self, text):
        self.titleLabel.setText(text)
    def setDescription (self, text):
        self.description.setText(text)

class example_ui(qg.QDialog):
    def __init__(self):
        qg.QDialog.__init__(self)
        self.myQListWidget = qg.QListWidget(self)
        self.myQListWidget.currentItemChanged.connect(self.getTitleValue)
        self.myQListWidget.setGeometry(qc.QRect(0,0,200,300))
        # make instance customwidget item (just one)------
        instance_1 = CustomQWidget()
        instance_1.setTitle('First title')
        instance_1.setDescription('this is a sample desc')
        myQListWidgetItem = qg.QListWidgetItem(self.myQListWidget)
        myQListWidgetItem.setSizeHint(instance_1.sizeHint())
        self.myQListWidget.addItem(myQListWidgetItem)
        self.myQListWidget.setItemWidget(myQListWidgetItem, instance_1)
    def getTitleValue(self,val):
       # i make assume something like below but didnt work
       # print (self.myQListWidget.currentItem.titleLabel.text()
       return 0
dialog = example_ui()
dialog.show()

now at getTitleValue function how do i get Title and desc value when i change selection ?

1 Answers1

1

You should remember that the list items and corresponding widgets are not the same. Luckily, QListWidget tracks them and gives you access to the displayed widget if you provide the list item:

class example_ui(qg.QDialog):
    def getTitleValue(self,val):
        # parameter val is actually the same as self.myQListWidget.currentItem
        selected_widget = self.myQListWidget.itemWidget(val)
        print selected_widget.titleLabel.text()       
        return 0

Side note: I had to add a main loop in order for the app to be executed at all:

import sys # to give Qt access to parameters
# ... class definitions etc. ... 
app = qg.QApplication(sys.argv)
dialog = example_ui()
dialog.show()
exec_status = app.exec_() # main loop
Sanyok
  • 41
  • 5
  • Thanks Sanyok for your time, i understand now, Note : i run the code inside maya and just fine, that would need a main loop (like you type) if want to run directly from python. thanks – Rifqi Khairur Rahman Nov 22 '16 at 09:37