PyQt4 - list widget click event? - python

im having a problem on my mini project.
i have a method here
def PrintClick(self,name = ""):
print name
then i have a list widget named lstStudents
how do i call the method PrintClick when i click an item inside lstStudents?
also how do i pass the parameters?
i tried
self.connect(self.ui.lstStudents,QtCore.SIGNAL("clicked()"), self.PrintClick)
but i doest work.
please help me :(

You usually call the event when the list selection changes. Also, I'd use the new-style event signals. They look nicer:
self.ui.lstStudents.currentItemChanged.connect(self.PrintClick)

Related

Tkinter Class and Button use

I am having trouble finding a way to accomplish what I want using buttons in tkinter. I am programming a multi-frame tkinter app, for myself, and each frame is a class, each class has buttons that I place on the screen in the __init__ method. I am having trouble trying to link the buttons and the command functions together.
Example:
class frameHome:
def __init__(self,parent, controller)
self.frame=tk.Frame(parent)
self.buttonOne=tk.Button(self.frame,text="Click Me") # I want to add the command here
self.buttonOne.pack()
def buttonOneClick():
print("You clicked me")
When I add the command in the tk.Button() call it says buttonOneClick not defined. One video I watched said to add the function at the top of the code. I would like to keep the function as a method of the class for organization, as well as keep init at the top of the class and was wondering if there is a way to create a reference to the function so I can have it as a method after the the init method and be able to call it in the init method, because I would like the init method to create the gui and link all widget commands for that class/frame in it.
Thank you for any time and help that you can offer
in your command did you write the command as command=buttonOneClick()? if that's the case then you should replace it with self.buttonOneClick()
The answer I needed was that I forgot the self param in the buttonOneClick(self, msg) method.

How to get the objectName of a modified QSpinBox?

I want to keep track of the user modifying the company's forecast in my custom made app.
I created it using qt designer and PyQt4, and I'm using QSpinBox for the quantities (easy way to control range of values and masking the fields to be number only).
The problem I'm having is when I want to get the QSpinBox that triggered my function.
At this moment it's being triggered using valueChanged.connect but could be using anything else.
I can get the int in the spinbox but not the spinbox's name.
Thanks beforehand for the help!
SOLUTION
The QSpinBox element
self.Item = QtGui.QSpinBox(self.centralwidget)
self.Item.setObjectName(_fromUtf8("ItemName"))
Trigger
self.Item.valueChanged.connect(self.foo)
The function it calls
def foo(self,obj):
sender = MainWindow.sender()
print sender.objectName()
In this case "MainWindow" is my QtCore.QObject
AS the question tagged C++. I am answering in both C++ and pyQt4. I never worked in pyQt4. So please excuse me for syntax.
In the "valueChanged" slot use QObject::sender() to get the spin box which triggered it.
And then call "objectName()".
QObject* obj = sender();
QString objName = obj->objectName();
May be in python:
sender = QtCore.QObject.sender()
str = sender.objectName()

PyQt - trigger an event when nothing is selected in a QListWidget?

I have an object of type QListWidget where the user can select 0 or more items.
If nothing is selected, how can i trigger an event that would call a function?
I know that you can detect clicking on a specific item by using:
QListWidget.itemClicked.connect(self.item_click)
Is there something similar for when nothing is selected at all? (or in other words, the QListWidget is clear)
Thanks!
Generally, you would connect to the itemSelectionChanged signal and then check whether anything is selected.
self.listwidget.itemSelectionChanged.connect(self.on_selection_changed)
def on_selection_changed(self):
if not self.listwidget.selectedItems():
# Do Stuff Here
self.nothing_selected_function()
But that will only catch events where something was selected and then the user deselected everything. If nothing was ever selected, it's not going to trigger this signal (like the first time you build the list, and nothing is selected). You'd have to call the slot manually in that case.
self.listwidget = ... # Code that builds and populates list widget
# Call this manually the first time.
self.on_selection_changed()
But part of your question is ambiguous. Why do you want to know when something is "not selected"? What about when a new item is added to the list? Should it trigger your "not selected" function since the list has changed, but there still isn't anything selected?

wxPython - How to generate an event when selecting in a ListBox?

I have a ListBox with a few options. I would like to generate wx.EVT_LISTBOX when I call SetSelection. I understand that SetSelection does not generate events to prevent problems during initialization. So I have a function
def selectItem(self, index):
self.myList.SetSelection(index)
event = wx.PyCommandEvent(wx.EVT_LISTBOX.typeId, self.myList.GetId())
wx.PostEvent(self, event)
I also have a simple function to handle selecting an item
def OnSelect(self, event):
print event.GetEventObject().GetStringSelection()
This will run, but the when using SelectItem(), the event object is None, and thus I get an error when when I try to use GetStringSelection(). How can I fix this? Any help is appreciated. Is there a better way to do this?
Actually, rethinking this, I probably don't need to generate the event to accomplish what I'm doing, but I'm still curious as to why this isn't working as expected
it's happening because you haven't called event.SetEventObject(self.myList) when you create your event.
You did answer your own question, you shouldn't be generating an event at all in the first place but just call some DoSelect(self, string) function from both selectItem() and OnSelect() instead.

PyQt sending parameter to slot when connecting to a signal

I have a taskbar menu that when clicked is connected to a slot that gets the trigger event. Now the problem is that I want to know which menu item was clicked, but I don't know how to send that information to the function connected to. Here is the used to connect the action to the function:
QtCore.QObject.connect(menuAction, 'triggered()', menuClickedFunc)
I know that some events return a value, but triggered() doesn't. So how do I make this happen? Do I have to make my own signal?
Use a lambda
Here's an example from the PyQt book:
self.connect(button3, SIGNAL("clicked()"),
lambda who="Three": self.anyButton(who))
By the way, you can also use functools.partial, but I find the lambda method simpler and clearer.
As already mentioned here you can use the lambda function to pass extra arguments to the method you want to execute.
In this example you can pass a string obj to the function AddControl() invoked when the button is pressed.
# Create the build button with its caption
self.build_button = QPushButton('&Build Greeting', self)
# Connect the button's clicked signal to AddControl
self.build_button.clicked.connect(lambda: self.AddControl('fooData'))
def AddControl(self, name):
print name
Source: snip2code - Using Lambda Function To Pass Extra Argument in PyQt4
use functools.partial
otherwise you will find you cannot pass arguments dynamically when script is running, if you use lambda.
I'd also like to add that you can use the sender method if you just need to find out what widget sent the signal. For example:
def menuClickedFunc(self):
# The sender object:
sender = self.sender()
# The sender object's name:
senderName = sender.objectName()
print senderName
In general, you should have each menu item connected to a different slot, and have each slot handle the functionality only for it's own menu item. For example, if you have menu items like "save", "close", "open", you ought to make a separate slot for each, not try to have a single slot with a case statement in it.
If you don't want to do it that way, you could use the QObject::sender() function to get a pointer to the sender (ie: the object that emitted the signal). I'd like to hear a bit more about what you're trying to accomplish, though.

Categories