I am currently building an interface using PyQt5 and I was wondering if there is an elegant way to connect several signals to the same function?
The straight-forward solution is this:
self.ui.button1.clicked.connect(self.Function)
self.ui.button2.clicked.connect(self.Function)
self.ui.button3.clicked.connect(self.Function)
self.ui.button4.clicked.connect(self.Function)
but is there a nicer, more readable option? For example something that would look like:
self.Function.connect(self.ui.button1.clicked,
self.ui.button2.clicked,
self.ui.button3.clicked,
self.ui.button4.clicked)
Also I started reading about QSignalMappers but if they can be avoided it's nice.
Thank you in advance!
You could make a function that takes a tuple or list of buttons as an argument, then connects the click signal for each one.
self.connect_buttons((self.ui.button1, self.ui.button2, self.ui.button3, self.ui.button4))
def connect_buttons(self, button_tup):
for button in button_tup:
button.clicked.connect(self.Function)
Related
I have a basic PyQt5 application. I want to make it so that when a certain button is clicked, a function is called that will run until another button is clicked. How is it possible to implement it?
Something like this (of course it doesn't work):
...
self.ui.start_button.clicked.connect(infinity_before_cancel_method)
self.ui.stop_button.clicked.connect(infinity_before_cancel_method)
...
def infinity_before_cancel_method():
...
while True:
do_something()
I have absolutely no idea how to do this in PyQt5. I've seen that this can be done using a multiprocessing library or similar, but I don't quite understand how to apply it to a PyQt5 application. Any hints would be helpful!
I am fairly new to PyQt5 or rather to Qt in general, so sorry if this question is really dumb or basic.
I am assigned working on an application for some rather complex scientific data analysis that uses PyQt5. Now my current task is to create an interface to enter all kinds of parameters in a treelike structure. I use a class TreeView(QWidget) for this purpose, loosely following this guide: http://pharma-sas.com/common-manipulation-of-qtreeview-using-pyqt5/
Now my boss wants that for some of the items in the tree it should be possible to click them just like a QPushButton to open a submenu, in which one can enter some more parameters (could be a QTableView I guess). As far as I know it is not possible to append QPushButtons just like QStandardItems right?
So what would be the best way to do this? Is it even possible?
my excuses if that question is very simple. I have a GUI with a few pushbuttons (QT with PySide). I want to connect 2 buttons with one function which is supposed to do the same thing with one difference. For that I need to pass an argument. Here is the type of code:
def test(flag):
if flag:
do something
else:
do something else
# button calls:
self.ui.button1.clicked.connect(test(True))
self.ui.button2.clicked.connect(test(False))
I have tried things as above, but it does not like the test(True). I have also played with lambda's and I am getting stuck at the same spot. So far I have been calling two separate functions, but that is not a good practice. Any help is appreciated.
Thanks
It works! Much simpler than I thought. Many thanks.
You said you used lambdas, did you try this
self.ui.button1.clicked.connect(lambda:test(True))
self.ui.button2.clicked.connect(lambda:test(False))
This should work.
I need some advise about how to read PyQt's documentation. Because on my own I can hardly figure anything out. I'm a programming newbie so sorry if my question is confusing. I'll try to explain the best I can :)
This is an example of where I got stuck. I was experimenting around with QListView. Basically just trying to print out data of what I have selected in the view. I got stuck until Justin, a very patient Python tutor, showed me this bit of code.
listView.clicked.connect(B)
def B(index):
record = sqlmodel.record(index.row())
It connects a clicked signal from QListView to function B. I was very surprised that right away the clicked event sends index to B by itself. I tried to look through QListView's documentation but can't find anything that explains this.
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qlistview.html
In this case, where in the docs should I look at to learn about this clicked event, and the index it sends out?
Would really appreciate any advise. :)
Unfortunately PyQt documentation is not full. Better source for knowledge is Qt documentation.
In your case QTableView inherits QAbstractItemView, and there is no clicked signal in QTableView docs, you can find it in List of all members, including inherited members page. And you can see, that this signal comes from QAbstractItemView and it's defined as:
void QAbstractItemView::clicked ( const QModelIndex & index )
Here you can see type of function arguments (clickable).
So, index passed to you function will be instance of QModelIndex and it has row method.
If you are confused with C++ syntax, another option is to use PySide documentation, which is more Python friendly.
QAbstractItemView.clicked in PySide docs
The following code connects QTableView's clicked signal to your function. QTableView emits clicked whenever someone clicks an item, which means that your function will be called automatically, since it's connected to that signal.
listView.clicked.connect(viewItemClicked)
Or am I missing something in your question? Read up on signal-slots in Qt if the above is unclear. (PyQt allows any function (i.e. python callable) to be connected to a signal, not just a slot (as it is in C++).
How can I do it, even if my application.exe is not the focused window ?
For example, like the Windows+D shortcut... Works everywhere...
I want that ALT+1 does a function, ALT+2 does another one, and so on...
You mean like this?
You need to use pyHook.
I have a pyHook example on my site. http://fadedbluesky.com/2011/using-pyhook-to-block-windows-key/
I originally wrote this to block keys in a game written using pygame. You should be able to easily adapt it to other windows programs.