1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
Chapter 3: Port ``bookdwindow.cpp`` to ``bookwindow.py``
*********************************************************
After the bookdelegate, port the C++ code for the
``BookWindow`` class. It offers a QMainWindow, containing a
``QTableView`` to present the books data, and a **Details**
section with a set of input fields to edit the selected row
in the table. To begin with, create the ``bookwindow.py``
and add the following imports to it:
.. literalinclude:: bookwindow.py
:language: python
:linenos:
:lines: 3-16
.. note:: The imports include the ``BookDelegate`` you
ported earlier and the ``Ui_BookWindow``. The pyside-uic
tool generates the ``ui_bookwindow`` Python code based
on the ``bookwindow.ui`` XML file.
To generate this Python code, run the following command on the
prompt:
.. code-block:: bash
pyside6-uic bookwindow.ui -o ui_bookwindow.py
Try porting the remaining code now. To begin with, here is
how both the versions of the constructor code looks:
C++ version
------------
.. literalinclude:: bookwindow.cpp
:language: c++
:linenos:
:lines: 20-103
Python version
---------------
.. literalinclude:: bookwindow.py
:language: python
:linenos:
:lines: 16-79
.. note:: The Python version of the ``BookWindow`` class
definition inherits from both ``QMainWindow`` and
``Ui_BookWindow``, which is defined in the
``ui_bookwindow.py`` file that you generated earlier.
Here is how the rest of the code looks like:
C++ version
------------
.. literalinclude:: bookwindow.cpp
:language: c++
:linenos:
:lines: 78-
Python version
---------------
.. literalinclude:: bookwindow.py
:language: python
:linenos:
:lines: 80-
Now that all the necessary pieces are in place, try to put
them together in ``main.py``.
.. literalinclude:: main.py
:language: python
:linenos:
:lines: 3-
Try running this to see if you get the following output:
.. image:: images/chapter3-books.png
:alt: BookWindow with a QTableView and a few input fields
Now, if you look back at :doc:`chapter2 <../chapter2/chapter2>`,
you'll notice that the ``bookdelegate.py`` loads the
``star.png`` from the filesytem. Instead, you could add it
to a ``qrc`` file, and load from it. The later approach is
rececommended if your application is targeted for
different platforms, as most of the popular platforms
employ stricter file access policy these days.
To add the ``star.png`` to a ``.qrc``, create a file called
``books.qrc`` and the following XML content to it:
.. literalinclude:: books.qrc
:linenos:
This is a simple XML file defining a list all resources that
your application needs. In this case, it is the ``star.png``
image only.
Now, run the ``pyside6-rcc`` tool on the ``books.qrc`` file
to generate ``rc_books.py``.
.. code-block:: bash
pyside6-rcc books.qrc -o rc_books.py
Once you have the Python script generated, make the
following changes to ``bookdelegate.py`` and ``main.py``:
.. literalinclude:: bookdelegate.py
:diff: ../chapter2/bookdelegate.py
.. literalinclude:: main.py
:diff: main-old.py
Although there will be no noticeable difference in the UI
after these changes, using a ``.qrc`` is a better approach.
Now that you have successfully ported the SQL Books example,
you know how easy it is. Try porting another C++ application.
|