Qt Designer: how to add custom slot and code to a button - python

I use Qt4 Designer and I want that when I click on the "yes" button, some code will execute. And when I click on the "no", some other code will be execute. How can I do it?

Click on the Edit Signal/Slots tool.
Create a connection for your button. For this, select your button in the designer by pressing on it with the left button of the mouse. Move the mouse to some place in the main window to create a connection with the main window (it is like a red line with a earth connection).
When you release the mouse button, the Configure Connection dialog appears.
In this dialog select a signal in the left text control (the sender), for example, pressed().
Then press edit in the right text control (the receiver). A dialog for the Signals/Slots of MainWindow appears.
In the slot panel add a new slot (green cross). The text slot1() appears. Double click on it to edit the line and write instead the name of your function doit_when_yes_ispressed(). Accept.
Now in the Configure Connection dialog you will see your function in the right text control. Select and Accept.
In the designer now you can see the signal and your function in the widget.

Right-click on your widget
Select "Go to slot..."
Select a signal and click OK
Your custom slot declaration and definition for that signal will be added to *.cpp and *.h files. Its name will be generated automatically.
upd:
Sorry, I didn't notice that the question is about Python & QtDesigner itself, I was thinking of the designer mode in QtCreator IDE. However, this still may be useful for someone who is looking for Qt/C++ info, so I leave the answer.

Related

Is there a way to select pyqt window/ui on mouse hover?

I have a little problem, I am using PyQt to make UI in Maya. Problem is that my current UI has hotkeys, but unless you click on the window, you can't use it because the window of UI is not selected(running on the back when you are doing things in Maya). UI being active all the time is also inconvenient because some hotkeys are the same as Maya's. So I was wondering is there any way to select a window without clicking on it, just mouse hover. Thank you!

Closing an .ui file on a click of a button (pyqt5)

I need my dialog box to close when the ok button is clicked. How do I do that ?
Assuming you created the button you should be able to just set the button click event as follows:
button.clicked.connect(self.close) where button is the button name.
Although since you did not provide any code with your question it's hard to know if this is what you're asking for...

Deactivate Checkbox in QT Designer

I have a (very rough) GUI created using QT Designer.
Within it there are two radio buttons. When Button A is toggled I would like the check boxes below to be unavailable for use.
When A is toggled, I would like to activate them.
Is this possible?
I have tried using the signal and slots mode, but I can only find an option to setChecked when B is toggled, not deactivate when A is toggled
It's quite simple: just connect radio button signal toggled() with group box's setVisible() slot.
This will completely hide the checkboxes.
You can also connect toggled() with every checkbox's setDisabled() slot just to deactivate them.

Make a window appear on top of another, block access to other windows until button clicked

Python 2.7, PyQt4.8.5
I want to have a main app window and then a second pop up window to display com port settings. This window should always be on top of the parent window until either the ok or the cancel button is clicked; closing the child window. (sort of like a required answer i.e. cant process until you choose the settings from the child window)
Is there a Python Qt command to do this?
Apologies if this has been asked/answered before, my search returned nothing useful.
You want a modal dialog. For example:
dialog = QInputDialog()
dialog.exec_()
You can either implement your own dialog widget (by subclassing QDialog) or use one of the several available.

Selecting Widgets

In Tkinter I'm trying to make it so when a command is run a widget is automatically selected, so that a one may bind events to the newly selected widget.
Basically I want it so when I press a button a text widget appears. When it appears normally one would have to click the text widget to facilitate the running of events bound to the text widget. I want that behavior to automatically happen when the user clicks the button. So that one does not have to click the button and then the text widget, but simply the button.
I'd also like it so if one started typing after the button was pressed it would automatically start filling the text widget. Again to cut out having to click on the text widget.
What bit of code does the above?
The terminology which describes what you want is "focus" -- you want to set the keyboard focus to your text widget. To do that you need to use the focus_set() and/or focus_force() methods on the text widget.

Categories

Resources