🔍 This section introduces the main PyQt5 features needed to create a custom standard app.
The preferred database engine for Android is SQLite
. The latest SQLite library available is sqlite3
as introduced on the SQLite website.
PyQt5 lets you work with many other database engines as shown on tutorialspoint website, but sqlite3
is a good start for simple python apps because:
- The application file is portable across multiple platforms
- Reading/writing performance is great as the application only loads the data it needs
- Content is updated continuously and atomically for maximum reliability
- Database content can be viewed with many third-party tools
Before attempting to run any SQLite
-related actions, make sure that the library is available on your machine:
sudo apt-get install sqlite3
sudo apt-get install sqlitebrowser
To confirm that SQLite
is functional on your machine, run the following:
cd $PYQT_CROM_DIR/examples/database
python3 pyqt5_app_with_database.py
A dialog window will pop up in which you can perform the following:
- View the pre-populated database (called
sportsdatabase.db
) - Add a row to the database
- Remove a row from the database
You can view the content of the generated and edited database at any time outside of the application with:
cd $PYQT_CROM_DIR/examples/database
sqlitebrowser sportsdatabase.db
You can also run a more operational PyQt5 app boasting a database with:
cd $PYQT_CROM_DIR/examples/database/database_management_project/database_management_pkg
python3 operational_pyqt5_app_with_database.py
This demo app is built on the one highlighted in the Getting started section:
- A window appears on the screen with 2 buttons: MAGIC or EXIT
- Once MAGIC is clicked, a pop-up appears on screen stating that the button has been clicked and that a database will open
- Once the pop-up has been acknowledged, a database (called
sportsdatabase.db
) is created in thehome
folder as shown in the alert window, if not already existing - In the dialog window displaying the content of the database, rows can be added, removed or edited
💡 You can view the content of sportsdatabase.db
at any time by following the instructions in Example PyQt5 app with database after ensuring that your Database manager is correctly setup.
pyqt5_database_management_app_android.mp4
There are 2 main approaches to create 2D Graphics in Qt apps:
- QGraphics way
- QtQuick way
QGraphics relies on a database of useful shapes and widgets (QWidgets) to make the app efficient and native (the look is tied to the platform). As described on the pythonguis website, QGraphics harnesses the model-view paradigm through QGraphicsScene (model), QGraphicsView (view) and QGraphicsItems (visual elements).
QtQuick on the other hand, relies on the Qt Modeling language (QML) to define user interfaces. As described on the pythonguis website, QML is focused on custom UI design and is useful for consistent app design across multiple platforms. The look of the app will be more modern, but the development might take longer.
As we are showcasing a prototyping tool for mobile apps, we have decided to explore QGraphics options rather than follow QtQuick practices. Note that both approaches are viable and handled by the pyqtdeploy tool.
To visualise a basic example of 2D Graphics in a PyQt app, run the following:
cd $PYQT_CROM_DIR/examples/graphics
python3 pyqt5_app_with_graphics.py
A graphics window will appear, in which you can perform the following:
- Move shapes around and locate their centre thanks to the status bar prompt
- Get back to the home screen thanks to the button at the top of the window
You can also run a more operational PyQt5 app boasting a graphics playground with:
cd $PYQT_CROM_DIR/examples/graphics/graphics_playground_project/graphics_playground_pkg
python3 operational_pyqt5_app_with_graphics.py
This demo app is built on the one highlighted in the Getting started section:
- A window appears on the screen with 2 buttons: MAGIC or EXIT
- Once MAGIC is clicked, a pop-up appears on screen stating that the button has been clicked and that a graphics playground will open
- Once the pop-up has been acknowledged, a graphics playground opens up with 2 shapes that can be dragged around
- When selecting a shape, its coordinates are displayed at the bottom of the screen, in the status bar
- To exit the graphics playground, hit the HOME button
pyqt5_graphics_playground_app_android.mp4
Network is used to interface multiple devices. The network helps interconnect devices to extend the capabilities of the application.
The first simple type of network communication is Bluetooth (which is more practical than wired connection).
There are 2 main types of Bluetooth technologies:
- Classic (used for audio streaming)
- Low Energy (LE) (used for any other application)
A bigger comparison between Bluetooth Classic and Bluetooth LE is made on symmetryelectronics website.
Since the aim is not to stream audio, we will solely focus on Bluetooth LE.
QtBluetooth classes are very powerful and enable the cross-platform use of Bluetooth communications.
A set of classes is introduced on the official qt website.
QtBluetooth classes are used in this basic example of Bluetooth communications from a PyQt app. To run the example Bluetooth app, use:
cd $PYQT_CROM_DIR/examples/network
python3 pyqt5_app_with_bluetooth.py
💡 If you encounter issues with Bluetooth, please refer to the Bluetooth troubleshooting.
The example Bluetooth app provides the following experience:
- A window appears on the screen with 2 buttons: Search for Bluetooth devices and EXIT
- If you click on EXIT, the app will close itself
- If you click on Search for Bluetooth devices, the app will ask you whether your device is Bluetooth capable. If it is not, the app is actually unusable and will crash. If you do have Bluetooth on your device, you will be asked to turn it ON.
- Once your Bluetooth is ON, click again on Search for Bluetooth devices so that the app can search for nearby devices for 5 seconds
- The app displays the list of found nearby devices on the main window
To visualise an operational example of Bluetooth communications in a PyQt app, run the following:
cd $PYQT_CROM_DIR/examples/network/bluetooth_scanner_project/bluetooth_scanner_pkg
python3 operational_pyqt5_app_with_bluetooth.py
💡 If you encounter issues with Bluetooth, please refer to the Bluetooth troubleshooting.
The operational example Bluetooth app has a similar behaviour to the example Bluetooth app.