PyQt By Example (Session 4) | Lateral Opinion
PyQt By Example (Session 4)
Posted: 2009-03-07 00:24:00 | Leer en español | More posts about kde programming python qt pyqtRequirements
If you have not done it yet, please check the previous sessions:
All files for this session are here: Session 4 at GitHub. You can use them, or you can follow these instructions starting with the files from Session 3 and see how well you worked!
Action!
What's an Action?
When we finished session 3 we had a basic todo application, with very limited functionality: you can mark tasks as done, but you can't edit them, you can't create new ones, and you can't remove them, much less do things like filtering them.
Today we will start writing code and designing UI to do those things.
The key concept here is Actions.
- Help? That's an action
- Open File? That's an action
- Cut / Copy / Paste? Those are actions too.
Let's quote The Fine Manual:
The QAction class provides an abstract user interface action that can be inserted into widgets.
In applications many common commands can be invoked via menus, tool bar buttons, and keyboard shortcuts. Since the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action.
Actions can be added to menus and tool bars, and will automatically keep them in sync. For example, in a word processor, if the user presses a Bold tool bar button, the Bold menu item will automatically be checked.
A QAction may contain an icon, menu text, a shortcut, status text, "What's This?" text, and a tooltip.
The beauty of actions is that you don't have to code things twice. Why add a "Copy" button to a tool bar, then a "Copy" menu entry, then write two handlers?
Create actions for everything the user can do then plug them in your UI in the right places. If you put them in a menu, it's a menu entry. If you put it in a tool bar, it's a button. Then write a handler for the action, connect it to the right signal, and you are done.
Let's start with a simple action: Remove Task. We will be doing the first half of the job, creating the action itself and the UI, using designer.
Creating Actions in Designer
First, let's go to the Action Editor and obviously click on the "New Action" button and start creating it:
A few remarks:
- If you don't know where the "X" icon came from, you have not read session 3 ;-)
- The actionDelete_Task object name is automatically generated from the text field. In some cases that can lead to awful names. If that's the case, you can just change the object name.
- The same text is used for the iconText and toolTip properties. If that's not correct, you can change it later.
Once you create the action, it will not be marked as "Used" in the action editor. That's because it exists, but has not been made available to the user anywhere in the window we are creating.
There are two obvious places for this action: a tool bar, and a menu bar.
Adding Actions to a Tool Bar
To add an action to a tool bar, first make sure there is one. If you don't have one in your "Object Inspector", then right click on MainWindow (either the actual window, or its entry in the inspector), and choose "Add Tool Bar".
You can add as many tool bars as you want, but try to want only one, unless you have a very good reason (we will have one in session 5 ;-)
After you create the tool bar, you will see empty space between the menu bar (that says "Type Here") and the task list widget. That space is the tool bar.
Drag your action's icon from the action editor to the tool bar.
That's it!
Coming Soon
Well, that was a rather long explanation for a small feature, wasn't it? Don't worry, the next actions will be much easier to add, because I expect you to read "I added an action called New Task" and know what I am talking about.
And in the next session, we will do just that. And we will create our first dialog.
Further Reading
- PyQt's Actions: 1 2
- Toolbar Design
- More Toolbar Design