Create a signal for each item in QListWidget - pyqt

I want to create a list of items using QListWidget and set it up so each time I click (select) an item a different method is executed. The way I thought about going about solving this is by connecting the list to a method that will check for the index and depending on the item index it will generate a signal connected to a different method. Thing is, I don't know how to generate this signal if I don't have a button or anything. The method is just checking for item index.
Any suggestions will be much appreciated!

The straightforward idea would be to connect the itemClicked signal to a slot in which a method is called depending on the clicked index. In this case you don't need a signal for every item.
But if you really want to create a "clicked" signal for each item and connect it to its respective slot(s), you could do it like this:
Subclass the QListWidgetItem and add a clicked signal to it. How to add custom signals
While filling the QListWidget, connect each item with its slots depending on its index.
Connect the itemClicked signal of the QListWidget to a slot, that emits the clicked item's signal like this:
def itemClickedSlot(item):
item.clicked.emit()

Related

Clean way to get mousePressed event notified to QGraphicsView

I inherited from QGraphicsItemGroup and made a class that keeps a pointer to its contained items so that I can later refer to them and change properties. It has an ellipse item and a line item and I want only the ellipse to be clickable. I need that press event of the ellipse to propagate to the QGraphicsView so that I can send a signal to some surrounding widgets.
So far I tried inheriting also from QGraphicsObject to have signals available but got stuck with ambigous base error when trying to use scene->addItem. I tried casting to QGraphicsItemGroup but I still get the error. I also tried inheriting from QObject with no success.
I'm new to QGraphics and I know the QGraphics framework has a lot of tools for user interaction and even interaction between GraphicsItems but this is really kicking my butt.
What would be the proper way to get this behavior?
Create a separate "emitter" class
To allow your subclass of QGraphicsItemGroup to emit signals, you can create a separate "emitter" class that inherits from QObject. Then, you can add an instance of this emitter class within your subclass of QGraphicsItemGroup. The emitter object can then emit signals for your subclass as needed.
QGraphicsItemGroup is treated as a single item
Unfortunately, an instance of QGraphicsItemGroup is treated as a single item, so each mousePressEvent will belong to the entire group rather than one of the members of that group (i.e., the ellipse item or the line item). If you want the mousePressEvent to behave differently depending on which item is clicked, they will need to be separate items, or you could try using line->setParentItem(ellipse) to link up the 2 items without using QGraphicsItemGroup.

Optimal and best way to control Recycler View items

what could be the best way to control events in RecyclerView items? That is, if we are going to change the display of the Recycler View items, or if we are going to test an event on them at all, what would be the best way?
I mean, to control these changes on each item in the onBindViewHolder() method, or in the MyViewHolder class, if the latter is more appropriate, then how do we calculate the first position?
Because Default -1 comes first and this issue causes it to crash sometimes.
Thank you if you have a better experience in this field
If you want to perform the event automatically (not from user behaviour), then you must do it within onBindViewHolder().
If you want to perform an event based on the user behaviour (like click, drag, etc.), then you can do it within the onBindViewHolder() or MyViewHolder.

How to hide OutputMessages window in SALOME ParaViS? Some ideas in question body

I want to hide Output Messages window in SALOME v.8.3.0. Now I try to get access to QWidget "OutputWindow" with inner SALOME console.
One way which I want to do it is to find a slot which connects with triggered() signal of Tools->Output Window main menu action.
But I don't know how to capture slot if I emit my signal. Also, I couldn't find the slot implementation in the source code of ParaViS module.
Maybe you know how to capture slot or how to find which QObjects are spawned in this slot?
Or you know the simple way to hide this annoying window?...

In Python3/tkinter is there a way to temporarily stop accepting clicks in a Treeview widget?

I have a GUI based in Python 3 and tkinter that has a big ttk.Treeview. I have defined methods for row selection (one click) and opening an advanced info panel (double-click). I need to ensure that, after being double-clicked, for the next one or two seconds, the Treeview state won't be changed by another click. Is it possible to deactivate Treeview mouse bindings, like what we do with buttons?
Doing a little more research, I was able to come up with a solution for this. I just created an empty method that is called when the tree widget is supposed to be inactive. So, we can use something like this to "unbind" all the mouse events and re-bind them a few seconds later, as needed:
def nothing(self, *event):
""" # Hacking moment: A function that does nothing, for those times you need it...
"""
pass
def bind_tree(self):
""" # Bind mouse and keyboard events to their respective functions or methods...
"""
self.tree.bind('<<TreeviewSelect>>', self.selectItem_popup)
self.tree.bind('<Double-1>', self.show_details)
self.tree.bind("<Button-2>", self.popupMenu)
self.tree.bind("<Button-3>", self.popupMenu)
def unbind_tree(self):
""" # Unbind all mouse and keyboard events, by binding them to an empty method...
"""
self.tree.bind('<<TreeviewSelect>>', self.nothing)
self.tree.bind('<Double-1>', self.nothing)
self.tree.bind("<Button-2>", self.nothing)
self.tree.bind("<Button-3>", self.nothing)
Then, in the rest of the code, We only need to call bind_tree() and unbind_tree() as needed.
This worked for me:
tree.bind("<ButtonRelease-1>", my_select_function)
# Do some stuff
tree.unbind("<ButtonRelease-1>")

QTreeView: Show "loading" message when expanding item

With PyQT and a QTreeView, I need to display a "loading" message or a "spinning wheel" when the user expands an item, because the childs are retrieved by making a http request.
Any ideas on how to implement this?
Thanks
If the time taken to retrieve the child items is relatively short (say, a few seconds), then by far the simplest solution is to display a busy/wait cursor.
You can either set the cursor on the treeview:
treeview.setCursor(QtCore.Qt.BusyCursor)
# retrieve and insert child items ...
treeview.unsetCursor()
or set it globally:
QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor))
# retrieve and insert child items ...
QtGui.QApplication.restoreOverrideCursor()
But other solutions will be much more complicated than this.
For instance you could show a QProgressBar in the status bar, or perhaps use a QMovie to display an animated icon somehow.

Resources