PySide: Connecting QComboBox indices with strings - python

I would like to connect the QComboBox indeces with specific strings (i.e. when I select "A", I want that it prints "A has been selected", and when I select "B", then "B has been selected").
I am new to PySide and learning, so I am sure there exists a simple solution. Help is appreciated.
from PySide import QtGui
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
v_global_layout = QtGui.QVBoxLayout()
method_selection = QtGui.QComboBox()
method_selection.addItem("A")
method_selection.addItem("B")
v_global_layout.addWidget(method_selection)
self.setLayout(v_global_layout)
def do_somethinh():
print("A has been selected!!!")
method_selection.activated.connect(do_somethinh)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
main_window = Widget()
main_window.setGeometry(100, 100, 640, 480)
main_window.show()
sys.exit(app.exec_())

The QComboBox.activated signal has two overloads: one which sends the index of the chosen item, and one which sends its text. The default overload sends the index. To select the other overload, you need slightly different syntax:
def do_somethinh(text):
print(text, "has been selected!!!")
method_selection.activated[str].connect(do_somethinh)
So signal objects have __getitem__ support, which lets you select a specific overload of a signal by passing the argument type as a key (if there's more than one argument, you can pass a tuple of types).

Related

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)

How to connect QTreeWidget and QStackedWidget in PyQt4?

I'm sorry but just a beginner of Python.
I just want to change index of QStackedWidget by the item click of QTreeWidget. I searched for the tutorials of SIGNAL and SLOT online, but just cannot solve the problem.
The parameters in QTreeWidget signal and QStackedWidget slot are not fitted.
self.connect(qtree, QtCore.SIGNAL("itemClicked(QTreeWidgetItem*,int)"), stack, QtCore.SLOT("setCurrentIndex(int)"))
And I tried this:
qtree.itemClicked.connect(stack.setCurrentIndex)
It just showed the error:
TypeError: setCurrentIndex(self, int): argument 1 has unexpected type 'QTreeWidgetItem'
I think there may be a method, but I cannot find on the network.
Like this:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class StockDialog(QDialog):
def __init__(self,parent=None):
super(StockDialog,self).__init__(parent)
mainSplitter=QSplitter(Qt.Horizontal)
treewidget = QTreeWidget(mainSplitter)
treewidget.setHeaderLabels(["Tree"])
treeroot = QTreeWidgetItem(treewidget, ["Stack"])
treeitem1 = QTreeWidgetItem(["WorkSpace"])
treeitem2 = QTreeWidgetItem(["About"])
treeroot.addChild(treeitem1)
treeroot.addChild(treeitem2)
stack=QStackedWidget(mainSplitter)
stack.setFrameStyle(QFrame.Panel|QFrame.Raised)
stackworkspace=StackWorkSpace()
stackabout=StackAbout()
stack.addWidget(stackworkspace)
stack.addWidget(stackabout)
closePushButton=QPushButton(self.tr("Close"))
self.connect(treewidget,
SIGNAL("itemClicked(int)"),
stack,SLOT("setCurrentIndex(int)"))
self.connect(closePushButton,
SIGNAL("clicked()"),
self,SLOT("close()"))
layout=QVBoxLayout(self)
layout.addWidget(mainSplitter)
layout.addWidget(closePushButton)
self.setLayout(layout)
class StackWorkSpace(QWidget):
def __init__(self,parent=None):
super(StackWorkSpace,self).__init__(parent)
widget1=QTextEdit(self.tr("WorkSpace"))
widget2=QTextEdit(self.tr("WorkSpace"))
layout=QGridLayout(self)
layout.addWidget(widget1,0,0)
layout.addWidget(widget2,0,1)
class StackAbout(QDialog):
def __init__(self,parent=None):
super(StackAbout,self).__init__(parent)
self.setStyleSheet("background: red")
app=QApplication(sys.argv)
main=StockDialog()
main.show()
app.exec_()
When change the QTreeWidget to the QListWidget in StockDialog class, it works.
class StockDialog(QDialog):
def __init__(self,parent=None):
super(StockDialog,self).__init__(parent)
mainSplitter=QSplitter(Qt.Horizontal)
listwidget=QListWidget(mainSplitter)
listwidget.insertItem(0,self.tr("WorkSpace"))
listwidget.insertItem(1,self.tr("About"))
stack=QStackedWidget(mainSplitter)
stack.setFrameStyle(QFrame.Panel|QFrame.Raised)
stackworkspace=StackWorkSpace()
stackabout=StackAbout()
stack.addWidget(stackworkspace)
stack.addWidget(stackabout)
closePushButton=QPushButton(self.tr("Close"))
self.connect(listwidget,
SIGNAL("currentRowChanged(int)"),
stack,SLOT("setCurrentIndex(int)"))
self.connect(closePushButton,
SIGNAL("clicked()"),
self,SLOT("close()"))
layout=QVBoxLayout(self)
layout.addWidget(mainSplitter)
layout.addWidget(closePushButton)
self.setLayout(layout)
Now, I want to do this with QTreeWidget, how can I do?
The strategy to solve this problem is to save the index information associated with each widget in the QTreeWidgetItem. QTreeWidgetItem has the setData() method that allows us to save information in the item and in this case we will save the index. The index is returned every time you add a widget to QStackedWidget through addWidget(), so in summary we will do the following:
treeitem1.setData(0, Qt.UserRole, stack.addWidget(stackworkspace))
treeitem2.setData(0, Qt.UserRole, stack.addWidget(stackabout))
After connecting the itemClicked signal of QTreeWidget, this returns the column and the item pressed, with this information we obtain the QStackedWidget index for it we recover the data saved through the function data():
treewidget.itemClicked.connect(lambda item, column: stack.setCurrentIndex(item.data(column, Qt.UserRole))
if item.data(column, Qt.UserRole) is not None else None)
The necessary code can be found in the following section:
class StockDialog(QDialog):
def __init__(self, parent=None):
super(StockDialog, self).__init__(parent)
mainSplitter = QSplitter(Qt.Horizontal)
treewidget = QTreeWidget(mainSplitter)
treewidget.setHeaderLabels(["Tree"])
treeroot = QTreeWidgetItem(treewidget, ["Stack"])
treeitem1 = QTreeWidgetItem(["WorkSpace"])
treeitem2 = QTreeWidgetItem(["About"])
treeroot.addChild(treeitem1)
treeroot.addChild(treeitem2)
stack = QStackedWidget(mainSplitter)
stack.setFrameStyle(QFrame.Panel | QFrame.Raised)
stackworkspace = StackWorkSpace()
stackabout = StackAbout()
treeitem1.setData(0, Qt.UserRole, stack.addWidget(stackworkspace))
treeitem2.setData(0, Qt.UserRole, stack.addWidget(stackabout))
closePushButton = QPushButton(self.tr("Close"))
treewidget.itemClicked.connect(lambda item, column: stack.setCurrentIndex(item.data(column, Qt.UserRole))
if item.data(column, Qt.UserRole) is not None else None)
layout = QVBoxLayout(self)
layout.addWidget(mainSplitter)
layout.addWidget(closePushButton)
self.setLayout(layout)

Accessing property of dynamically created QStandardItems in PyQt5

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)

Editing a dictionary with a QTreeView

I have a reduced code sample in which a window is created with just one Button.
Pressing it will pop a Qt dialog TestDialog which takes as parameter a Python dictionary. This dictionary is displayed in an editable QTreeView inside the dialog.
After changing some values you can click on Ok or Cancel to either accept or discard the changes. Once the dialog is closed my intention is to retrieve from the main window the modified dictionary calling dialog.get_data() which, right now only returns the original unmodified dictionary. After clicking the Ok button, the retrieved dictionary is printed to stdout.
My question is, how can I modify the dictionary when the tree view is modified? Is there a way to automatically attach a function to be executed on each item modification? So that when editing, for example, a float in the tree view, then the corresponding value will be updated as float in the dictionary?
The dictionary does not have a fixed size and the types on it may change. The list of types is limited and known though and, for this example, could be reduced to {int, str, float, Other}. It can be assumed as well that the parents are not supposed to be editable and the children are only editable in the second column, just as it is shown in the example bellow.
Here is the code I have:
import sys
from PyQt4 import QtGui, QtCore, uic
from copy import deepcopy
class TestDialog(QtGui.QDialog):
def __init__(self, data):
super(TestDialog, self).__init__()
self.data = deepcopy(data)
# Layout
btOk = QtGui.QPushButton("OK")
btCancel = QtGui.QPushButton("Cancel")
self.tree = QtGui.QTreeView()
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(btOk)
hbox.addWidget(btCancel)
vbox = QtGui.QVBoxLayout()
vbox.addLayout(hbox)
vbox.addWidget(self.tree)
self.setLayout(vbox)
self.setGeometry(300, 300, 600, 400)
# Button signals
btCancel.clicked.connect(self.reject)
btOk.clicked.connect(self.accept)
# Tree view
self.tree.setModel(QtGui.QStandardItemModel())
self.tree.setAlternatingRowColors(True)
self.tree.setSortingEnabled(True)
self.tree.setHeaderHidden(False)
self.tree.setSelectionBehavior(QtGui.QAbstractItemView.SelectItems)
self.tree.model().setHorizontalHeaderLabels(['Parameter', 'Value'])
for x in self.data:
if not self.data[x]:
continue
parent = QtGui.QStandardItem(x)
parent.setFlags(QtCore.Qt.NoItemFlags)
for y in self.data[x]:
value = self.data[x][y]
child0 = QtGui.QStandardItem(y)
child0.setFlags(QtCore.Qt.NoItemFlags |
QtCore.Qt.ItemIsEnabled)
child1 = QtGui.QStandardItem(str(value))
child1.setFlags(QtCore.Qt.ItemIsEnabled |
QtCore.Qt.ItemIsEditable |
~ QtCore.Qt.ItemIsSelectable)
parent.appendRow([child0, child1])
self.tree.model().appendRow(parent)
self.tree.expandAll()
def get_data(self):
return self.data
class Other(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return '(%s, %s)' % (self.x, self.y)
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
btn = QtGui.QPushButton('Button', self)
btn.resize(btn.sizeHint())
btn.clicked.connect(self.show_dialog)
self.data = {}
# This example will be hidden (has no parameter-value pair)
self.data['example0'] = {}
# A set example with an integer and a string parameters
self.data['example1'] = {}
self.data['example1']['int'] = 14
self.data['example1']['str'] = 'asdf'
# A set example with a float and other non-conventional type
self.data['example2'] = {}
self.data['example2']['float'] = 1.2
self.data['example2']['other'] = Other(4, 8)
def show_dialog(self):
dialog = TestDialog(self.data)
accepted = dialog.exec_()
if not accepted:
return
self.data = deepcopy(dialog.get_data())
print self.data
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
You can connect to the model's itemChanged signal:
self.tree.model().itemChanged.connect(self.handleItemChanged)
The handler would look something like this:
def handleItemChanged(self, item):
parent = self.data[item.parent().text()]
key = item.parent().child(item.row(), 0).text()
parent[key] = type(parent[key])(item.text())
Note that the conversion of values using type won't necessarily work for custom classes like Other. So you will have to either ensure that the constructor for such classes can convert a string representation, or parse the string into the appropriate arguments before passing them to the constructor.
Also note that I haven't bothered to deal with QString values in the above example code. If you use Python 3, this is not an issue, because they are automatically converted to/from Python strings by default. But for Python 2, you can switch this behaviour on by doing the following:
import sip
sip.setapi('QString', 2)
from PyQt4 import QtGui, QtCore, uic
For more details on this, see Selecting Incompatible APIs in the PyQt4 Docs.

Categories

Resources