2
0
mirror of https://github.com/pyqt/examples.git synced 2025-08-30 13:27:42 +00:00

Update for PyQt6 / PySide6

This commit is contained in:
Michael Herrmann 2022-03-29 12:40:07 +02:00
parent 57e4a75032
commit 0cbdafd894
29 changed files with 105 additions and 105 deletions

View File

@ -1,18 +1,18 @@
# PyQt examples 2021
# PyQt examples 2022
These PyQt examples show you how to create a desktop app with Python and Qt. Start with "Hello World" or browse the official PyQt demos. You can run every example yourself on Windows, Mac or Linux. All you need is Python 3. For instructions, please see [below](#running-the-examples).
| <a href="src/01 PyQt QLabel"><img src="src/screenshots/pyqt-qlabel.png" alt="PyQt QLabel" width=100px></a> | <a href="src/02 PyQt Widgets"><img src="src/screenshots/pyqt-widgets.png" alt="PyQt widgets screenshot" width=200px></a> | <a href="src/03 QVBoxLayout PyQt5"><img src="src/screenshots/qvboxlayout-pyqt5.png" alt="QVBoxLayout PyQt5" width=100px></a> | <a href="src/04 PyQt Signals and Slots"><img src="src/screenshots/pyqt-signals-and-slots.jpg" alt="PyQt Signals and Slots" width=170px></a> | <a href="src/05 Qt Designer Python"><img src="src/screenshots/qt-designer-windows.png" alt="Qt Designer Python" width=190px></a> |
| <a href="src/01 PyQt QLabel"><img src="src/screenshots/pyqt-qlabel.png" alt="PyQt QLabel" width=100px></a> | <a href="src/02 PyQt Widgets"><img src="src/screenshots/pyqt-widgets.png" alt="PyQt widgets screenshot" width=200px></a> | <a href="src/03 QVBoxLayout PyQt6"><img src="src/screenshots/qvboxlayout-pyqt5.png" alt="QVBoxLayout PyQt6" width=100px></a> | <a href="src/04 PyQt Signals and Slots"><img src="src/screenshots/pyqt-signals-and-slots.jpg" alt="PyQt Signals and Slots" width=170px></a> | <a href="src/05 Qt Designer Python"><img src="src/screenshots/qt-designer-windows.png" alt="Qt Designer Python" width=190px></a> |
| :--: | :--: | :--: | :--: | :--: |
| <a href="src/01 PyQt QLabel">Hello World!</a> | <a href="src/02 PyQt Widgets">Common PyQt Widgets</a> | <a href="src/03 QVBoxLayout PyQt5">Layouts</a> | <a href="src/04 PyQt Signals and Slots">Signals and Slots</a> | <a href="src/05 Qt Designer Python">Qt Designer & Python</a> |
| <a href="src/01 PyQt QLabel">Hello World!</a> | <a href="src/02 PyQt Widgets">Common PyQt Widgets</a> | <a href="src/03 QVBoxLayout PyQt6">Layouts</a> | <a href="src/04 PyQt Signals and Slots">Signals and Slots</a> | <a href="src/05 Qt Designer Python">Qt Designer & Python</a> |
| <a href="src/06 QML Python example"><img src="src/screenshots/qml-python-example.png" alt="QML Python example" width=200px></a> | <a href="src/07 Qt Text Editor"><img src="src/screenshots/qt-text-editor.png" alt="Qt Text Editor" width=180px></a> | <a href="src/08 PyQt5 exe"><img src="src/screenshots/pyqt5-exe.png" alt="PyQt5 exe" width=213px></a> | <a href="src/09 Qt dark theme"><img src="src/screenshots/qt-dark-theme.png" alt="Qt dark theme" width=180px></a> |
| <a href="src/06 QML Python example"><img src="src/screenshots/qml-python-example.png" alt="QML Python example" width=200px></a> | <a href="src/07 Qt Text Editor"><img src="src/screenshots/qt-text-editor.png" alt="Qt Text Editor" width=180px></a> | <a href="src/08 PyQt6 exe"><img src="src/screenshots/pyqt5-exe.png" alt="PyQt6 exe" width=213px></a> | <a href="src/09 Qt dark theme"><img src="src/screenshots/qt-dark-theme.png" alt="Qt dark theme" width=180px></a> |
| :--: | :--: | :--: | :--: |
| <a href="src/06 QML Python example">QML Python example</a> | <a href="src/07 Qt Text Editor">Qt Text Editor</a> | <a href="src/08 PyQt5 exe">Packaging & deployment</a> | <a href="src/09 Qt dark theme">Qt Dark Theme</a> |
| <a href="src/06 QML Python example">QML Python example</a> | <a href="src/07 Qt Text Editor">Qt Text Editor</a> | <a href="src/08 PyQt6 exe">Packaging & deployment</a> | <a href="src/09 Qt dark theme">Qt Dark Theme</a> |
| <a href="src/10 QPainter Python example"><img src="src/screenshots/qpainter-python-example.png" alt="QPainter Python example" width=200px></a> | <a href="src/11 PyQt Thread example"><img src="src/screenshots/pyqt-thread-example.png" alt="PyQt Thread example" width=175px></a> | <a href="src/12 QTreeView example in Python"><img src="src/screenshots/qtreeview-example-in-python.png" alt="QTreeView example in Python" width=260px></a> | <a href="src/13 PyQt5 QListView"><img src="src/screenshots/pyqt5-qlistview.png" alt="PyQt5 QListView" width=138px></a> |
| <a href="src/10 QPainter Python example"><img src="src/screenshots/qpainter-python-example.png" alt="QPainter Python example" width=200px></a> | <a href="src/11 PyQt Thread example"><img src="src/screenshots/pyqt-thread-example.png" alt="PyQt Thread example" width=175px></a> | <a href="src/12 QTreeView example in Python"><img src="src/screenshots/qtreeview-example-in-python.png" alt="QTreeView example in Python" width=260px></a> | <a href="src/13 PyQt6 QListView"><img src="src/screenshots/pyqt5-qlistview.png" alt="PyQt6 QListView" width=138px></a> |
| :--: | :--: | :--: | :--: |
| <a href="src/10 QPainter Python example">Action Shooter</a> | <a href="src/11 PyQt Thread example">Chat Client</a> | <a href="src/12 QTreeView example in Python">Tree Views</a> | <a href="src/13 PyQt5 QListView">Lists</a> |
| <a href="src/10 QPainter Python example">Action Shooter</a> | <a href="src/11 PyQt Thread example">Chat Client</a> | <a href="src/12 QTreeView example in Python">Tree Views</a> | <a href="src/13 PyQt6 QListView">Lists</a> |
| <a href="src/14 QAbstractTableModel example"><img src="src/screenshots/qabstracttablemodel-example.png" alt="QAbstractTableModel example" height=120px></a> | <a href="src/15 PyQt database example"><img src="src/screenshots/pyqt-database-example.png" alt="QAbstractTableModel example" height=120px></a> |
| :--: | :--: |
@ -21,9 +21,9 @@ These PyQt examples show you how to create a desktop app with Python and Qt. Sta
These examples are taken from the following book:
<p align="center">
<a href="https://build-system.fman.io/pyqt5-book"><img src="https://build-system.fman.io/static/public/img/pyqt5-book.jpg" alt="PyQt5 book"></a>
<a href="https://build-system.fman.io/pyqt6-book"><img src="https://build-system.fman.io/static/public/img/pyqt6-book.jpg" alt="PyQt6 book"></a>
<br/>
<a href="https://build-system.fman.io/pyqt5-book">Python and Qt: The Best Parts</a>
<a href="https://build-system.fman.io/pyqt6-book">Python and Qt: The Best Parts</a>
<br/>
by Michael Herrmann
</p>
@ -82,13 +82,13 @@ You'll find a `.py` file there, typically `main.py`. You can run it with the com
Please note that the virtual environment must still be active for this to work.
## Using PySide2
## Using PySide
This repository uses PyQt5 to use Qt from Python. Another, alternative binding is PySide2 (also called "Qt for Python"). It is less mature than PyQt5 but has the advantage that you can use it for free in commercial projects.
This repository uses PyQt6 to use Qt from Python. Another, alternative binding is PySide6 (also called "Qt for Python"). It is less mature than PyQt6 but has the advantage that you can use it for free in commercial projects.
If you want to use PySide2 instead of PyQt5, simply replace all mentions of the latter by the former. For instance, in [`src/requirements.txt`](src/requirements.txt), replace `PyQt5` by `PySide2`. Similarly for any code examples: `from PyQt5.QtWidgets ...` becomes `from PySide2.QtWidgets ...` etc.
If you want to use PySide6 instead of PyQt6, simply replace all mentions of the latter by the former. For instance, in [`src/requirements.txt`](src/requirements.txt), replace `PyQt6` by `PySide6`. Similarly for any code examples: `from PyQt6.QtWidgets ...` becomes `from PySide6.QtWidgets ...` etc.
Alternatively, if you don't want to commit to either of the two bindings at this stage, you can also use [Qt.py](https://github.com/mottosso/Qt.py). This is an abstraction over PySide2 and PyQt5. It loads whichever of the two bindings is available. To use it for the examples presented here, replace all mentions of `PyQt5` by just `Qt`.
Alternatively, if you don't want to commit to either of the two bindings at this stage, you can also use [Qt.py](https://github.com/mottosso/Qt.py). This is an abstraction over PySide6 and PyQt6. It loads whichever of the two bindings is available. To use it for the examples presented here, replace all mentions of `PyQt6` by just `Qt`.
## Licensing
@ -96,4 +96,4 @@ Except where otherwise indicated, you may use the source code of examples 1 - 15
The official PyQt demos in [`src/pyqt-official`](src/pyqt-official) are [licensed under the GPL](src/pyqt-official/LICENSE).
The screenshots in this repository may be used under the terms of the [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/) if you prominently mention and link to [Michael Herrmann's PyQt5 book](https://build-system.fman.io/pyqt5-book).
The screenshots in this repository may be used under the terms of the [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/) if you prominently mention and link to [Michael Herrmann's PyQt6 book](https://build-system.fman.io/pyqt6-book).

View File

@ -5,18 +5,18 @@ This example shows how you can create a Hello World app using PyQt. It uses a [`
![PyQt QLabel screenshot](../screenshots/pyqt-qlabel.png)
```
from PyQt5.QtWidgets import *
from PyQt6.QtWidgets import *
app = QApplication([])
label = QLabel('Hello World!')
label.show()
app.exec_()
app.exec()
```
For instructions how you can run this code, please see the [top-level README](../../README.md#running-the-examples).
The code works as follows: First, we import the necessary PyQt classes via the statement:
from PyQt5.QtWidgets import *
from PyQt6.QtWidgets import *
Next, we create a [`QApplication`](https://doc.qt.io/Qt-5/qapplication.html). This is required in every PyQt app. In a sense, it initializes PyQt:
@ -32,7 +32,7 @@ By calling `.show()` on a [widget](../02%20PyQt%20Widgets), we can spawn a windo
Finally, we hand control over to Qt:
app.exec_()
app.exec()
This too is required in every Qt application. It gives Qt a chance to run and process user input, such as for instance when the user clicks the "Window close" button.

View File

@ -1,5 +1,5 @@
from PyQt5.QtWidgets import *
from PyQt6.QtWidgets import *
app = QApplication([])
label = QLabel('Hello World!')
label.show()
app.exec_()
app.exec()

View File

@ -42,8 +42,8 @@
#############################################################################
from PyQt5.QtCore import QDateTime, Qt, QTimer
from PyQt5.QtWidgets import (QApplication, QCheckBox, QComboBox, QDateTimeEdit,
from PyQt6.QtCore import QDateTime, Qt, QTimer
from PyQt6.QtWidgets import (QApplication, QCheckBox, QComboBox, QDateTimeEdit,
QDial, QDialog, QGridLayout, QGroupBox, QHBoxLayout, QLabel, QLineEdit,
QProgressBar, QPushButton, QRadioButton, QScrollBar, QSizePolicy,
QSlider, QSpinBox, QStyleFactory, QTableWidget, QTabWidget, QTextEdit,
@ -240,4 +240,4 @@ if __name__ == '__main__':
app = QApplication(sys.argv)
gallery = WidgetGallery()
gallery.show()
sys.exit(app.exec_())
sys.exit(app.exec())

View File

@ -1,12 +1,12 @@
# QVBoxLayout PyQt5
# QVBoxLayout PyQt6
Layouts let you position GUI elements next to each other. [`QVBoxLayout`](https://doc.qt.io/qt-5/qvboxlayout.html) for instance arranges items vertically:
<p align="center"><img src="../screenshots/qvboxlayout-pyqt5.png" alt="QVBoxLayout PyQt5"></p>
<p align="center"><img src="../screenshots/qvboxlayout-pyqt5.png" alt="QVBoxLayout PyQt6"></p>
The [source code for this example](main.py) is not much more complex than for our [Hello World app](../01%20PyQt%20QLabel). First, we import PyQt5:
The [source code for this example](main.py) is not much more complex than for our [Hello World app](../01%20PyQt%20QLabel). First, we import PyQt6:
from PyQt5.QtWidgets import *
from PyQt6.QtWidgets import *
Then, we create the required `QApplication`:
@ -34,7 +34,7 @@ Finally, we add the layout - and thus its contents - to the `window` we created
We conclude by showing the window and (as is required) handing control over to Qt:
window.show()
app.exec_()
app.exec()
For instructions how you can run this example yourself, please see [here](../../README.md#running-the-examples).

View File

@ -1,4 +1,4 @@
from PyQt5.QtWidgets import *
from PyQt6.QtWidgets import *
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
@ -6,4 +6,4 @@ layout.addWidget(QPushButton('Top'))
layout.addWidget(QPushButton('Bottom'))
window.setLayout(layout)
window.show()
app.exec_()
app.exec()

View File

@ -4,9 +4,9 @@ PyQt Signals let you react to user input such as mouse clicks. A *slot* is a fun
<p align="center"><img src="../screenshots/pyqt-signals-and-slots.jpg" alt="PyQt Signals and Slots"></p>
The code begins in the usual way. First, we import PyQt5 and create a `QApplication`:
The code begins in the usual way. First, we import PyQt6 and create a `QApplication`:
from PyQt5.QtWidgets import *
from PyQt6.QtWidgets import *
app = QApplication([])
Next, we create a button:
@ -18,7 +18,7 @@ Then we define a function. It will be called when the user clicks the button. Yo
def on_button_clicked():
alert = QMessageBox()
alert.setText('You clicked the button!')
alert.exec_()
alert.exec()
And here is where signals and slots come into play: We instruct Qt to invoke our function by _connecting_ it to the `.clicked` signal of our button:
@ -27,6 +27,6 @@ And here is where signals and slots come into play: We instruct Qt to invoke our
Finally, we show the button on the screen and hand control over to Qt:
button.show()
app.exec_()
app.exec()
For instructions how you can run this example yourself, please see [here](../../README.md#running-the-examples).

View File

@ -1,4 +1,4 @@
from PyQt5.QtWidgets import *
from PyQt6.QtWidgets import *
app = QApplication([])
button = QPushButton('Click')
@ -6,8 +6,8 @@ button = QPushButton('Click')
def on_button_clicked():
alert = QMessageBox()
alert.setText('You clicked the button!')
alert.exec_()
alert.exec()
button.clicked.connect(on_button_clicked)
button.show()
app.exec_()
app.exec()

View File

@ -12,15 +12,15 @@ The dialog in the following screenshot comes from the file [`dialog.ui`](dialog.
The [`main.py`](main.py) script (also in this directory) loads and invokes `dialog.ui` from Python. The steps with which it does this are quite easy.
First, [`main.py`](main.py) imports the `uic` module from PyQt5:
First, [`main.py`](main.py) imports the `uic` module from PyQt6:
from PyQt5 import uic
from PyQt6 import uic
It also imports `QApplication`. Like all (Py)Qt apps, we must create an instance of this class.
from PyQt5.QtWidgets import QApplication
from PyQt6.QtWidgets import QApplication
Then, we use [`uic.loadUiType(...)`](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html#PyQt5.uic.loadUiType) to load the `.ui` file. This returns two classes, which we call `Form` and `Window`:
Then, we use [`uic.loadUiType(...)`](https://www.riverbankcomputing.com/static/Docs/PyQt6/designer.html#PyQt6.uic.loadUiType) to load the `.ui` file. This returns two classes, which we call `Form` and `Window`:
Form, Window = uic.loadUiType("dialog.ui")
@ -42,6 +42,6 @@ Next, we instantiate the `Form`. We invoke its `.setupUi(...)` method, passing t
We've now connected the necessary components for displaying the user interface given in the `.ui` file. All that remains is to `.show()` the window and kick off Qt's event processing mechanism:
window.show()
app.exec_()
app.exec()
For instructions how to run this example yourself, please see [here](../../README.md#running-the-examples).

View File

@ -1,5 +1,5 @@
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
from PyQt6 import uic
from PyQt6.QtWidgets import QApplication
Form, Window = uic.loadUiType("dialog.ui")
@ -8,4 +8,4 @@ window = Window()
form = Form()
form.setupUi(window)
window.show()
app.exec_()
app.exec()

View File

@ -44,13 +44,13 @@ Window {
Executing the QML from Python is even easier. The code is in [`main.py`](main.py):
```
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtWidgets import QApplication
from PyQt6.QtQml import QQmlApplicationEngine
from PyQt6.QtWidgets import QApplication
app = QApplication([])
engine = QQmlApplicationEngine()
engine.load("main.qml")
app.exec_()
app.exec()
```
If you'd like further instructions how you can run this code for yourself, please see [here](../../README.md#running-the-examples).

View File

@ -1,7 +1,7 @@
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtWidgets import QApplication
from PyQt6.QtQml import QQmlApplicationEngine
from PyQt6.QtWidgets import QApplication
app = QApplication([])
engine = QQmlApplicationEngine()
engine.load("main.qml")
app.exec_()
app.exec()

View File

@ -1,5 +1,5 @@
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QKeySequence
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QKeySequence
class MainWindow(QMainWindow):
def closeEvent(self, e):
@ -81,4 +81,4 @@ def show_about_dialog():
about_action.triggered.connect(show_about_dialog)
window.show()
app.exec_()
app.exec()

View File

@ -1,16 +1,16 @@
# PyQt5 exe
# PyQt6 exe
Once you have a PyQt5 application, you want to compile your Python source code into a standalone executable. Furthermore, you normally want to create an installer so your users can easily set up your app.
Once you have a PyQt6 application, you want to compile your Python source code into a standalone executable. Furthermore, you normally want to create an installer so your users can easily set up your app.
This example uses [fbs](https://build-system.fman.io) to create a standalone executable and an installer for the text editor in [example 07](../07%20Qt%20Text%20Editor).
<img src="../screenshots/pyqt5-exe.png" alt="PyQt5 exe installer" height="250px"> <img src="../screenshots/pyqt5-installer-mac.png" alt="Installer for a PyQt5 Mac application" height="250px">
<img src="../screenshots/pyqt5-exe.png" alt="PyQt6 exe installer" height="250px"> <img src="../screenshots/pyqt5-installer-mac.png" alt="Installer for a PyQt6 Mac application" height="250px">
You can find a modified version of the ["old" main.py](../07%20Qt%20Text%20Editor/main.py) in [`src/main/python/main.py`](src/main/python/main.py). It only has a few extra lines:
We import fbs's `ApplicationContext`:
from fbs_runtime.application_context.PyQt5 import ApplicationContext
from fbs_runtime.application_context.PyQt6 import ApplicationContext
Further down, we instantiate it:
@ -34,9 +34,9 @@ To handle this, the new code uses fbs's [`ApplicationContext.get_resource(...)`]
This automatically handles the different possible locations of the image.
Because we didn't create the `QApplication` ourselves, we finally use the following call instead of only `app.exec_()`:
Because we didn't create the `QApplication` ourselves, we finally use the following call instead of only `app.exec()`:
appctxt.app.exec_()
appctxt.app.exec()
To run this example yourself, you need fbs installed as per the instructions [here](../../README.md#running-the-examples). Then, you can do use the following command to run the text editor:

View File

@ -1,12 +1,12 @@
from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.QtWidgets import QMainWindow
from fbs_runtime.application_context.PyQt6 import ApplicationContext
from PyQt6.QtWidgets import QMainWindow
import sys
appctxt = ApplicationContext() # 1. Instantiate ApplicationContext
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QKeySequence
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QKeySequence
class MainWindow(QMainWindow):
def closeEvent(self, e):
@ -80,10 +80,10 @@ def show_about_dialog():
% appctxt.get_resource("icon.svg")
about_dialog = QMessageBox(window)
about_dialog.setText(text)
about_dialog.exec_()
about_dialog.exec()
about_action.triggered.connect(show_about_dialog)
window.show()
exit_code = appctxt.app.exec_() # 2. Invoke appctxt.app.exec_()
exit_code = appctxt.app.exec() # 2. Invoke appctxt.app.exec()
sys.exit(exit_code)

View File

@ -1,6 +1,6 @@
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QKeySequence, QPalette, QColor
from PyQt5.QtCore import Qt
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QKeySequence, QPalette, QColor
from PyQt6.QtCore import Qt
app = QApplication([])
@ -102,4 +102,4 @@ def show_about_dialog():
about_action.triggered.connect(show_about_dialog)
window.show()
app.exec_()
app.exec()

View File

@ -1,7 +1,7 @@
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import QSound
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtCore import *
from PyQt6.QtMultimedia import QSound
class PlainTextEdit(QPlainTextEdit):
def __init__(self):
@ -100,4 +100,4 @@ def show_about_dialog():
about_action.triggered.connect(show_about_dialog)
window.show()
app.exec_()
app.exec()

View File

@ -1,5 +1,5 @@
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from requests import Session
name = input("Please enter your name: ")
@ -34,4 +34,4 @@ timer = QTimer()
timer.timeout.connect(display_new_messages)
timer.start(1000)
app.exec_()
app.exec()

View File

@ -1,5 +1,5 @@
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from requests import Session
from threading import Thread
from time import sleep
@ -46,4 +46,4 @@ timer = QTimer()
timer.timeout.connect(display_new_messages)
timer.start(1000)
app.exec_()
app.exec()

View File

@ -1,5 +1,5 @@
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from requests import Session
from threading import Thread
from threadutil import run_in_main_thread
@ -40,4 +40,4 @@ message.returnPressed.connect(send_message)
thread = Thread(target=fetch_new_messages, daemon=True)
thread.start()
app.exec_()
app.exec()

View File

@ -1,4 +1,4 @@
from PyQt5.QtCore import QObject, pyqtSignal
from PyQt6.QtCore import QObject, pyqtSignal
class CurrentThread(QObject):

View File

@ -11,8 +11,8 @@ It allows you to receive results from the function invocation:
"""
from functools import wraps
from PyQt5.QtCore import pyqtSignal, QObject, QThread
from PyQt5.QtWidgets import QApplication
from PyQt6.QtCore import pyqtSignal, QObject, QThread
from PyQt6.QtWidgets import QApplication
from threading import Event, get_ident
def run_in_thread(thread_fn):

View File

@ -1,6 +1,6 @@
# QTreeView example in Python
A _tree view_ is what's classicaly used to display files and folders: A hierarchical structure where items can be expanded. This example application shows how PyQt5's [`QTreeView`](https://doc.qt.io/qt-5/qtreeview.html) can be used to display your local files.
A _tree view_ is what's classicaly used to display files and folders: A hierarchical structure where items can be expanded. This example application shows how PyQt6's [`QTreeView`](https://doc.qt.io/qt-5/qtreeview.html) can be used to display your local files.
<p align="center"><img src="../screenshots/qtreeview-example-in-python.png" alt="QTreeView example in Python"></p>
@ -18,6 +18,6 @@ The nice thing about the Model/View distinction is that it lets you visualize th
view = QListView()
The next example, [PyQt5 QListview](../13%20PyQt5%20QListView), shows another way of using `QListView`.
The next example, [PyQt6 QListview](../13%20PyQt6%20QListView), shows another way of using `QListView`.
To run this example yourself, please follow [the instructions in the README of this repository](../../README.md#running-the-examples).

View File

@ -1,5 +1,5 @@
from os.path import expanduser
from PyQt5.QtWidgets import *
from PyQt6.QtWidgets import *
home_directory = expanduser('~')
@ -9,4 +9,4 @@ view = QTreeView()
view.setModel(model)
view.setRootIndex(model.index(home_directory))
view.show()
app.exec_()
app.exec()

View File

@ -1,8 +1,8 @@
# PyQt5 QListView
# PyQt6 QListView
This example shows how you can use a PyQt5 [`QListView`](https://doc.qt.io/qt-5/qlistview.html) to display a list.
This example shows how you can use a PyQt6 [`QListView`](https://doc.qt.io/qt-5/qlistview.html) to display a list.
<p align="center"><img src="../screenshots/pyqt5-qlistview.png" alt="PyQt5 QListView"></p>
<p align="center"><img src="../screenshots/pyqt5-qlistview.png" alt="PyQt6 QListView"></p>
It simply shows a static list of strings. Technically, the data is managed by Qt's [`QStringListModel`](https://doc.qt.io/qt-5/qstringlistmodel.html). The important steps of the [code](main.py) are:

View File

@ -1,5 +1,5 @@
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QStringListModel
from PyQt6.QtWidgets import *
from PyQt6.QtCore import QStringListModel
app = QApplication([])
model = QStringListModel([
@ -8,4 +8,4 @@ model = QStringListModel([
view = QListView()
view.setModel(model)
view.show()
app.exec_()
app.exec()

View File

@ -1,5 +1,5 @@
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
headers = ["Scientist name", "Birthdate", "Contribution"]
rows = [("Newton", "1643-01-04", "Classical mechanics"),
@ -25,4 +25,4 @@ model = TableModel()
view = QTableView()
view.setModel(model)
view.show()
app.exec_()
app.exec()

View File

@ -1,6 +1,6 @@
from os.path import exists
from PyQt5.QtWidgets import *
from PyQt5.QtSql import *
from PyQt6.QtWidgets import *
from PyQt6.QtSql import *
import sys
@ -18,4 +18,4 @@ model.select()
view = QTableView()
view.setModel(model)
view.show()
app.exec_()
app.exec()

View File

@ -1,3 +1,3 @@
fbs==0.8.3
PyQt5==5.12.3
fbs
PyQt6
requests