Accessing property of dynamically created QStandardItems in PyQt5 - python

I'm having a problem determining whether or not the checkboxes that are dynamically created have been checked or unchecked by the user in a simple GUI I've created.
I've adapted the relevant code and pasted it below. Although it may be easy to just create and name 4 QStandardItems, I'm dealing with many lists containing many different items that change quite a lot, so it isn't really feasible to create them myself.
Any help finding out how to access these properties would be much appreciated.
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Splash(QWidget):
def __init__(self):
super().__init__()
# imagine this is a very long list...
self.seasons = ['summer','autumn','winter','spring']
self.initUI()
def initUI(self):
layout = QVBoxLayout()
list = QListView()
model = QStandardItemModel()
list.setModel(model)
printbtn = QPushButton('print values')
printbtn.clicked.connect(self.print_action)
for season in self.seasons:
item = QStandardItem(season)
item.setCheckable(True)
model.appendRow(item)
model.dataChanged.connect(lambda: self.print_action(item.text()))
layout.addWidget(printbtn)
layout.addWidget(list)
self.setLayout(layout)
self.show()
def print_action(self, item):
print('changed', item)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
ex = Splash()
sys.exit(app.exec_())
In short - I can detect when an item has been checked using model.dataChanged and connecting that to a function, but it cannot differentiate between the seasons.

If you keep a reference to the list (or the model), you can search for the items by their text, and then get their check-state:
def print_action(self):
model = self.list.model()
for text in 'summer', 'autumn', 'winter', 'spring':
items = model.findItems(text)
if items:
checked = items[0].checkState() == Qt.Checked
print('%s = %s' % (text, checked))

It seems you want to get notified when the checkState of a item has been changed.
In my opinion, there are possible two ways.
First way, QModel will emit "dataChanged" to refresh the view, so you can connect the signal which means the checkState of a item might be changed.
model.dataChanged.connect(self.test)
def test(self):
pass
Second way, use a timer to notify yourself and you check it by yourselves.
timer = QTimer()
timer.timeout.connect(self.test)
timer.start(1000)

Related

Why can't I sort columns in my PyQt5 QTableWidget using UserRole data? [duplicate]

This question already has answers here:
How to customize sorting behaviour in QTableWidget
(2 answers)
Closed 9 days ago.
I am trying to sort my QTableWidget columns by the values stored in the user role of each QTableWidgetItem, but I am unable to do so. I have enabled sorting with self.setSortingEnabled(True), and I have set the data in each QTableWidgetItem with item.setData(Qt.DisplayRole, f'M - {r}') and item.setData(Qt.UserRole, r). However, when I try to sort the columns by the values stored in the user role, it sorts the columns by the values stored in the display role instead.
Here is a minimal working example of my code:
from random import randint
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QWidget, QGridLayout, \
QTableWidgetItem, QPushButton
class Table(QTableWidget):
def __init__(self):
super().__init__()
self.setSortingEnabled(True)
def populate(self):
self.clear()
self.setColumnCount(3)
self.setRowCount(200)
for row in range(500):
for column in range(3):
r = randint(0, 1000)
item = QTableWidgetItem()
item.setData(Qt.DisplayRole, f'M - {r}')
item.setData(Qt.UserRole, r)
self.setItem(row, column, item)
class MainApp(QMainWindow):
def __init__(self):
super().__init__()
self.table = Table()
self.button = QPushButton('Roll')
self.button.clicked.connect(self.table.populate)
layout = QWidget()
self.setCentralWidget(layout)
grid = QGridLayout()
layout.setLayout(grid)
grid.addWidget(self.button)
grid.addWidget(self.table)
if __name__ == '__main__':
app = QApplication([])
main_app = MainApp()
main_app.showMaximized()
app.exec()
Additionally, I tried using EditRole, but the values that appear in the table are not the values from DisplayRole. For example, in the code below, I set item.setData(Qt.DisplayRole, f'M - {r}'), but even though r is an integer, the display role value is a string ('M - {r}'). I was hoping that sorting by UserRole or EditRole would sort based on the integer value of r, but that doesn't seem to be the case.
item.setData(Qt.DisplayRole, f'M - {r}')
item.setData(Qt.EditRole, int(r))
Sorting is always based on Qt.DisplayRole.
Trying to use the EditRole is pointless, as the setData() documentation points out:
Note: The default implementation treats Qt::EditRole and Qt::DisplayRole as referring to the same data.
The Qt.UserRole is a custom role that could be used for anything (and containing any type) and by default is not used for anything in Qt. Setting a value with the UserRole doesn't change the sorting, because Qt knows nothing about its existence or how the value should be used.
Since you are using strings for the sorting, the result is that numbers are not sorted as you may think: for instance "120" is smaller than "13", because "12" comes before "13".
The only occurrence in which the DisplayRole properly sorts number values is when it is explicitly set with a number:
item.setData(Qt.DisplayRole, r)
Which will not work for you, as you want to show the "M - " prefix. Also, a common mistake is trying to use that in the constructor:
item = QTableWidgetItem(r)
And while the syntax is correct, its usage is wrong, as the integer constructor of QTableWidgetItem is used for other purposes.
If you want to support custom sorting, you must create a QTableWidgetItem subclass and implement the < operator, which, in Python, is the __lt__() magic method:
class SortUserRoleItem(QTableWidgetItem):
def __lt__(self, other):
return self.data(Qt.UserRole) < other.data(Qt.UserRole)
Then you have to create new items using that class. Note that:
you should always try to use existing items, instead of continuously creating new ones;
as explained in the setItem() documentation, you should always disable sorting before adding new items, especially when using a loop, otherwise the table will be constantly sorted at each insertion (thus making further insertion inconsistent);
you're using the a range (500) inconsistent with the row count (200);
you should also set an item prototype based on the subclass above;
class Table(QTableWidget):
def __init__(self):
super().__init__()
self.setSortingEnabled(True)
self.setItemPrototype(SortUserRoleItem())
def populate(self):
self.setSortingEnabled(False)
self.setColumnCount(3)
self.setRowCount(200)
for row in range(200):
for column in range(3):
r = randint(0, 1000)
item = self.item(row, column)
if not item:
item = SortUserRoleItem()
self.setItem(row, column, item)
item.setData(Qt.DisplayRole, 'M - {}'.format(r))
item.setData(Qt.UserRole, r)
self.setSortingEnabled(True)
Note that, as an alternative, you could use a custom delegate, then just set the value of the item as an integer (as shown above) and override the displayText():
class PrefixDelegate(QStyledItemDelegate):
def displayText(self, text, locale):
if isinstance(text, int):
text = f'M - {text}'
return text
class Table(QTableWidget):
def __init__(self):
super().__init__()
self.setItemDelegate(PrefixDelegate(self))
# ...
def populate(self):
# ...
item = self.item(row, column)
if not item:
item = QTableWidgetItem()
self.setItem(row, column, item)
item.setData(Qt.DisplayRole, r)
Use a QTableView instead. Widgets are meant for very basic use cases. It's important to invoke setSortRole on the model. Also, your setData arguments were in reverse order, correct is data, role.
from random import randint
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
class MainApp(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.table_view = QtWidgets.QTableView()
self.table_view.setSortingEnabled(True)
self.model = QtGui.QStandardItemModel()
self.model.setSortRole(QtCore.Qt.UserRole)
self.table_view.setModel(self.model)
self.button = QtWidgets.QPushButton('Roll')
layout = QtWidgets.QWidget()
self.setCentralWidget(layout)
grid = QtWidgets.QGridLayout()
layout.setLayout(grid)
grid.addWidget(self.button)
grid.addWidget(self.table_view)
self.button.clicked.connect(
self.populate
)
def populate(self):
self.table_view.model().clear()
for _ in range(500):
r = randint(0, 1000)
item = QtGui.QStandardItem()
item.setData(f'M - {r}', QtCore.Qt.DisplayRole)
item.setData(r, QtCore.Qt.UserRole)
self.table_view.model().appendRow(item)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
main_app = MainApp()
main_app.showMaximized()
app.exec()

Dynamically add QTableView to dynamically created tab pages (QTabWidget)

I am trying to have a series of QTableView created at runtime and added to newly created pages of a multipage QTabWidget.
All seems to go fine, but the QTableView don't show up.
The QTabWidget gets zeroed (reset to no pages) and refurbished (...) flawlessly (at least it looks like so) depending on the selection of a combobox (and the dictionaries therein related).
I am also using a delegate callback to include a column of checkboxes to the QTableView (thanks to https://stackoverflow.com/a/50314085/7710452), which works fine stand alone.
Here is the code.
Main Window
EDIT
as recommended by eyllanesc, here is the standalone module (jump to the end of the post for details on the part I think is problematic):
"""
qt5 template
"""
import os
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import uic
from configparser import ConfigParser, ExtendedInterpolation
from lib.SearchControllers import findGuis, get_controller_dict, show_critical, show_exception
import resources.resources
from lib.CheckBoxesDelegate import CheckBoxDelegate
myForm_2, baseClass = uic.loadUiType('./forms/setup.ui')
class MainWindow(baseClass):
def __init__(self, config_obj: ConfigParser,
config_name: str,
proj_name: str,
*args,
**kwargs):
super().__init__(*args, **kwargs)
self.ui = myForm_2()
self.ui.setupUi(self)
# your code begins here
self.setWindowTitle(proj_name + " Setup")
self.ui.logo_lbl.setPixmap(qtg.QPixmap(':/logo_Small.png'))
self.config_obj = config_obj
self.config_name = config_name
self.proj_filename = proj_name
self.proj_config = ConfigParser(interpolation=ExtendedInterpolation())
self.proj_config.read(proj_name)
self.guis_dict = {}
self.components = {}
self.cdp_signals = {}
self.root_path = self.config_obj['active']['controllers']
self.tableViews = []
self.tabs = []
self.iniControllersBox()
self.setActSignals()
self.load_bulk()
self.set_signals_table()
self.update_CurController_lbl()
self.update_ControllersTab() # here is where the action gets hot
# your code ends here
self.show() # here crashes if I passed the new tab to the instance of
# QTabView. otherwise it shows empty tabs
#########################################################
def load_bulk(self):
# get the list of running components into a dictionary
for i in self.list_controllers:
i_path = os.path.join(self.root_path, i)
print(i)
self.components[i] = get_controller_dict(i_path,
self.config_obj,
'Application.xml',
'Subcomponents/Subcomponent',
'Name',
'src')
for j in self.components[i]:
print(j)
signals_key = (i , j)
tgt = os.path.join(self.root_path, self.components[i][j])
self.cdp_signals[signals_key] = get_controller_dict(i_path,
self.config_obj,
self.components[i][j],
'Signals/Signal',
'Name',
'Type',
'Routing')
def set_signals_table(self):
self.ui.MonitoredDevicesTable.setHorizontalHeaderItem(0, qtw.QTableWidgetItem('GUI caption'))
self.ui.MonitoredDevicesTable.setHorizontalHeaderItem(1, qtw.QTableWidgetItem('Monitored Signal'))
def setActSignals(self):
self.ui.controllersBox.currentIndexChanged.connect(self.update_guis_list)
self.ui.controllersBox.currentIndexChanged.connect(self.update_CurController_lbl)
self.ui.controllersBox.currentIndexChanged.connect(self.update_ControllersTab)
def update_ControllersTab(self):
self.ui.componentsTab.clear() # this is the QTabWidget
self.tabs = []
self.tableViews = []
curr_controller = self.ui.controllersBox.currentText()
for i in self.components[curr_controller]:
if len(self.cdp_signals[curr_controller, i]) == 0:
continue
self.tabs.append(qtw.QWidget())
tabs_index = len(self.tabs) - 1
header_labels = ['', 'Signal', 'Type', 'Routing', 'Input']
model = qtg.QStandardItemModel(len(self.cdp_signals[curr_controller, i]), 5)
model.setHorizontalHeaderLabels(header_labels)
# in the next line I try to create a new QTableView passing
# the last tab as parameter, in the attempt to embed the QTableView
# into the QWidget Tab
self.tableViews.append(qtw.QTableView(self.tabs[tabs_index]))
tbw_Index = len(self.tableViews) - 1
self.tableViews[tbw_Index].setModel(model)
delegate = CheckBoxDelegate(None)
self.tableViews[tbw_Index].setItemDelegateForColumn(0, delegate)
rowCount = 0
for row in self.cdp_signals[curr_controller, i]:
for col in range(len(self.cdp_signals[curr_controller, i][row])):
index = model.index(rowCount, col, qtc.QModelIndex())
model.setData(index, self.cdp_signals[curr_controller, i][row][col])
try:
self.ui.componentsTab.addTab(self.tabs[tabs_index], i) # no problems, some controllers ask up to
except Exception as ex:
print(ex)
def update_CurController_lbl(self):
self.ui.active_controller_lbl.setText(self.ui.controllersBox.currentText())
def iniControllersBox(self):
self.list_controllers = [os.path.basename(f.path) for f in os.scandir(self.root_path) if f.is_dir() and str(
f.path).upper().endswith('NC')]
self.ui.controllersBox.addItems(self.list_controllers)
for i in range(self.ui.controllersBox.count()):
self.ui.controllersBox.setCurrentIndex(i)
newKey = self.ui.controllersBox.currentText()
cur_cntrlr = os.path.join(self.config_obj['active']['controllers'], self.ui.controllersBox.currentText())
self.guis_dict[newKey] = findGuis(cur_cntrlr, self.config_obj)
self.ui.controllersBox.setCurrentIndex(0)
self.update_guis_list()
def update_guis_list(self, index=0):
self.ui.GuisListBox.clear()
self.ui.GuisListBox.addItems(self.guis_dict[self.ui.controllersBox.currentText()])
if __name__ == '__main__':
config = ConfigParser()
config.read('./config.ini')
app = qtw.QApplication([sys.argv])
w = MainWindow(config, './config.ini',
'./test_setup_1.proj')
sys.exit(app.exec_())
and here the external to add the checkboxes column:
class CheckBoxDelegate(QtWidgets.QItemDelegate):
"""
A delegate that places a fully functioning QCheckBox cell of the column to which it's applied.
"""
def __init__(self, parent):
QtWidgets.QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
"""
Important, otherwise an editor is created if the user clicks in this cell.
"""
return None
def paint(self, painter, option, index):
"""
Paint a checkbox without the label.
"""
self.drawCheck(painter, option, option.rect, QtCore.Qt.Unchecked if int(index.data()) == 0 else QtCore.Qt.Checked)
def editorEvent(self, event, model, option, index):
'''
Change the data in the model and the state of the checkbox
if the user presses the left mousebutton and this cell is editable. Otherwise do nothing.
'''
if not int(index.flags() & QtCore.Qt.ItemIsEditable) > 0:
return False
if event.type() == QtCore.QEvent.MouseButtonRelease and event.button() == QtCore.Qt.LeftButton:
# Change the checkbox-state
self.setModelData(None, model, index)
return True
if event.type() == QtCore.QEvent.MouseButtonPress or event.type() == QtCore.QEvent.MouseMove:
return False
return False
def setModelData (self, editor, model, index):
'''
The user wanted to change the old state in the opposite.
'''
model.setData(index, 1 if int(index.data()) == 0 else 0, QtCore.Qt.EditRole)
The 1st picture shows the layout in QTDesigner, the 2nd the result (emtpy tabs) when avoiding the crashing.
the QTabWidget has no problems in zeroing, or scale up, back to as many tab as I need, it's just that I have no clue on how to show the QTabview. My approach was to try to embed the QTabView in the tabpage passing it as parameter to the line creating the new QTabView.
Since I am using rather convoluted dictionaries, calling an XML parser to fill them up, not to mention the config files, I know even this version of my script is hardly reproduceable/runnable.
If someone had the patience of focusing on the update_ControllersTab method though, and tell me what I am doing wrong handling the QWidgets, it'd be great.
Again the basic idea is to clear the QTabWidget any time the user selects a different controller (combo box on the left):
self.ui.componentsTab.clear() # this is the QTabWidget
self.tabs = [] # list to hold QTabView QWidgets (pages) throughout the scope
self.tableViews = [] # list to hold QTabView(s) thorughout the scope
count how many tabs (pages) and hence embedded TabViews I need with the new controllers selected.
and then for each tab needed:
create a new tab (page)
self.tabs.append(qtw.QWidget())
tabs_index = len(self.tabs) - 1
create a new QTabView using a model:
header_labels = ['', 'Signal', 'Type', 'Routing', 'Input']
model = qtg.QStandardItemModel(len(self.cdp_signals[curr_controller, i]), 5)
model.setHorizontalHeaderLabels(header_labels)
self.tableViews.append(qtw.QTableView(self.tabs[tabs_index]))
tbw_Index = len(self.tableViews) - 1
self.tableViews[tbw_Index].setModel(model)
populate the TableView with data, and then finally add the tab widget (with the suppposedly embedded QTableView to the QTabWidget (the i argument is a string from my dbases Names:
self.ui.componentsTab.addTab(self.tabs[tabs_index], i)
This method is called also by the __init__ to initialize and apparently all goes error free, until the last 'init' statement:
`self.show()`
at which point the app crashes with:
Process finished with exit code 1073741845
on the other hand, if here instead of trying to embed the QTableView:
self.tableViews.append(qtw.QTableView(self.tabs[tabs_index]))
I omit the parameter, that is:
self.tableViews.append(qtw.QTableView())
the app doesn't crash anymore, but of course no QtableViews are shown, only empty tabpages:
As stupid as this may sound the problem is in... the delegate class that creates the checkboxes in the first column (see https://stackoverflow.com/a/50314085/7710452)
I commented out those two lines:
delegate = CheckBoxDelegate(None)
self.tableViews[tbw_Index].setItemDelegateForColumn(0, delegate)
and... bingo!
the CheckBoxDelegate works fine in the example shown in the post (a single QTableView form). I also tinkered around adding columns and rows, and moving the checkbox column back and forth with no problems. In that standalone. But as soon as I add the class and set the delegate, i am back at square zero, app crashing with:
Process finished with exit code 1073741845
so I am left with this problem now. Thnx to whomever read this.
Problem solved, see comment to post above.

Access 'isChecked' property from QWidgetItem class object

I'm dynamically creating UI elements in layers of a QStackedLayout widget. I have created a generator that stores the objects for a targeted stacked widget layer so that I can collect settings made with UI elements contained in the layer when needed. I'm certain that I have isolated a radio button, but I do not know how to access the 'isChecked()' property. If I have this item isolated and stored as a variable using this line ('items' is the Generator):
target_radio_button = items.__next__()
how can I get to the 'isChecked' property to query whether or not it is checked, knowing that this QWidgetItem is a QRadioButton? If I print the type of the isolated object using:
print (type(target_radio_button))
This is what is returned:
<class 'PySide2.QtWidgets.QWidgetItem'>
Here is the minimal code requested:
from PySide2 import QtWidgets
import sys
class Test(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
self.setWindowTitle("Test")
self.setGeometry(50, 50, 200, 200)
self.test_layout = QtWidgets.QVBoxLayout()
self.setLayout(self.test_layout)
self.main_stacked_layout = QtWidgets.QStackedLayout()
self.test_layout.addLayout(self.main_stacked_layout)
self.widget = QtWidgets.QWidget()
self.widget_layout = QtWidgets.QVBoxLayout()
self.widget.setLayout(self.widget_layout)
self.radio = QtWidgets.QRadioButton('Sample Radio Button')
self.h_layout = QtWidgets.QHBoxLayout()
self.h_layout.addWidget(self.radio)
self.widget_layout.addLayout(self.h_layout)
self.layout_container = [self.widget]
self.main_stacked_layout.addWidget(self.widget)
self.demo()
def demo(self):
target_widget = self.layout_container[0]
target_layout = target_widget.layout()
for layout_item in target_layout.findChildren(QtWidgets.QHBoxLayout):
items = (layout_item.itemAt(i) for i in range(layout_item.count()))
self.get_settings_value(items, 'Boolean')
# for item in target_layout.findChildren(QtWidgets.QRadioButton):
# print (item.text())
# print (item.isChecked())
def get_settings_value(self, items, value_type):
if value_type == 'Boolean':
target_radio_button = items.__next__()
print (type(target_radio_button))
#print (target_radio_button.isChecked())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
form = Test()
form.show()
sys.exit(app.exec_())
There are two areas I commented out- the first block I disabled demonstrates a way that I can see to get the information I'm looking for, but its not versatile enough. What I need is to capture the children in the target layout without filtering for a specific type (in the case of the sample code this would be for a QRadioButton). I want to send that group of found children to the simplified "get_settings_value" function, and from there break out instructions on what to look for. I intend to have multiple different "value_type" arguments to extract values depending on the UI widget. The second commented out area is the print statement checking if the element 'isChecked()'. This throws the error, and I'm trying to understand how to gain access to this property.
The itemAt() method returns a QLayoutItem that is a generic container and its derived classes are QLayout, QSpacerItem, and QWidgetItem. So if you want to get the widget of a QLayoutItem then you must use the widget() method:
def get_settings_value(self, items, value_type):
if value_type == 'Boolean':
layoutitem = next(items)
target_radio_button = layoutitem.widget()
print(type(target_radio_button))
#print (target_radio_button.isChecked())

Is it possible to restrict QListWidget so users can only select 3 items max?

I know that you can change the selection mode to select more than one item from a list. However, changing to multiselection means users can choose to select all items in a list if they wanted to. I was wondering if it was possible to allow users to select multiple items but set a max number of items (ie users can select 1-3 items from a list of 20 items).
I've looked through the documentation and various questions, but can't see any methods that would do this.
import sys
from PyQt5.QtWidgets import QAbstractItemView, QApplication, QListWidget, QListWidgetItem, QVBoxLayout, QWidget
class Example(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(50,50,320,200)
layout = QVBoxLayout(self)
combo = QListWidget(self)
combo.setSelectionMode(QAbstractItemView.MultiSelection)
counter = 1
while (counter < 21):
combo.addItem(str(counter))
counter = counter + 1
layout.addWidget(combo)
self.setWindowTitle("QListWidget")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
My example code displays a list of 20 items. It has multiselection set so users can select multiple but no current restrictions.
One way is to subclass QListWidget and override selectionCommand, e.g.
from PyQt5.QtCore import QItemSelectionModel
class MyListWidget(QListWidget):
def __init__(self, parent=None, max_selected = 3):
super().__init__(parent)
self.max_selected = max_selected
def selectionCommand(self, index, event):
if len(self.selectedItems()) >= self.max_selected:
return QItemSelectionModel.Deselect
else:
return super().selectionCommand(index, event)
Okay an excerpt from the documentation found here:
https://doc.qt.io/qtforpython/PySide2/QtWidgets/QAbstractItemView.html
States the following:
Note that the range is not updated until the widget is shown.
Several other functions are concerned with selection control; for
example setSelectionMode() , and setSelectionBehavior() . This class
provides a default selection model to work with ( selectionModel() ),
but this can be replaced by using setSelectionModel() with an instance
of QItemSelectionModel
So yes it is possible to do this (as it is with all coding endeavors -- anything is possible) and the above states the how you just need to figure out how you are going to implement it -- probably going to need to use Behavior or maybe make your own Model
This may work for similiar cases.
list is a QListWidget defined in example.ui. change 3 in len(selected_items) > 3 to any value you desire.
ui_filename = "example.ui"
baseUIClass, baseUIWidget = uic.loadUiType(ui_filename)
class Example(baseUIWidget, baseUIClass):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.setupUi(self)
self.list.itemSelectionChanged.connect(self.Enforce_Selection_Size)
def Enforce_Selection_Size(self):
selected_items = self.list.selectedItems()
if len(selected_items) > 3:
selected_items[3].setSelected(False)

PyQt Get specific value in ListWidget

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 ?
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

Categories

Resources