返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
PyQt

PyQt5 Development Practice (Part 1: Preparation)

Preface Over the past year I've developed quite a few small PyQt projects. Since I hadn't used Qt with C++ before, you could say I started from scratch, learning as I went. In the process I once again felt how lacking the domestic tech community is... There's not exactly little material on PyQt in China, but not a lot either—it's mainly scattered, and there aren't really any decent systematic learning resources... So for this PyQt, it all came down to reading the docs and overseas communities, plus the most important thing: figuring it out myself... Setting Up the Development Environment The development environment setup in this article is partly learned from a book, and partly from practice tot

曦远 (DealiAxy)约 15 分钟
目录8 个章节

Preface

Over the past year I've developed quite a few small PyQt projects. Since I hadn't used Qt with C++ before, you could say I started from scratch, learning as I went. In the process I once again felt how lacking the domestic tech community is... There's not exactly little material on PyQt in China, but not a lot either—it's mainly scattered, and there aren't really any decent systematic learning resources... So for this PyQt, it all came down to reading the docs and overseas communities, plus the most important thing: figuring it out myself...

Setting Up the Development Environment

The development environment setup in this article is partly learned from a book and partly summarized from practice. In short, given that there currently isn't a particularly good PyQt integrated development tool, this achieves a reasonably efficient development workflow for the time being.

The PyQt development approach I currently use follows the traditional MVC architecture, separating UI layout from logic implementation~

Specifically:

  • Design the UI with QtDesigner, save it as a *.ui file, then use pyuic5 from the PyQt tools to convert the ui file into a py file—this is the view
  • Create corresponding py files in the controller package to implement the corresponding UI logic

Project structure for reference:

Using QtDesigner

This QtDesigner is pretty crude. Never mind comparing it to VS—even compared to the Swing UI designer in Android Studio or IDEA, it's the little brother. It's maybe about 60–70% of the level of VB 6.0 from 1998...

Still, it's better than nothing. At least you can design simple UIs, and that's enough.

Having written a lot of Flutter and Android apps, I'm very used to various linear layouts. Qt isn't bad at layout either—there are Hlayout and Vlayout you can use, and they're convenient in QtDesigner as well: create a Main window, drop a few widgets on it, right-click to set the layout, then you can freely use HLayout, VLayout, GridLayout and other components for layout, as shown:

Installing PyQt Tools

To use the tools mentioned above such as QtDesigner and pyuic5, you need to install them first.

pip install pyqt5
pip install pyqt5-tools

After installation, you can find the PyQt5 and pyqt5-tools folders under Lib/site-packages in the Python installation directory. The tools we need, such as QtDesigner and pyuic5, are in the pyqt5-tools folder.

Configuring External Tools in PyCharm

To make PyQt development more convenient in PyCharm, you also need to configure a few external tools. After configuring them you can do some quick operations: for example, right-click a ui file to open it directly with QtDesigner, or convert a UI file to a py file, as shown:

The specific configuration method is as follows:

First, File->Settings, open PyCharm's settings, and go to the External Tools screen, as shown

Here click the plus button to open the add page. We'll add QtDesigner first, as shown

Next add the pyuic5 tool. This tool's command argument format is as follows:

{pyuic5_path} -o {target_path} {file_path}

So the configuration is like this:

I'll copy the arguments part again:

-o $FileDir$/$FileNameWithoutExtension$.py $FilePath$

Recommending a Small Open Source Project

This is a simple video merging tool I wrote before. Below is a brief project introduction

I wanted to merge videos but couldn't find a decent tool—many of them charge money. I thought, "This crappy feature and they still want to charge?", so I decided to make one myself. At first I made one with Python; it worked pretty well and perfectly completed my video merging tasks, but it had no GUI, so I couldn't share it with friends to use. Then I wrote a GUI with Qt and put it on top, and that's how this project came about

Link: https://github.com/Deali-Axy/VideoMerge

Recommending a Book

Although systematic learning materials in China are scarce, I did manage to find this one

I forget where I found the PDF. It has no table of contents, so looking things up is a pain. I spent time going through the whole book and manually marked up a table of contents, as shown

I'd say I more or less understand the contents of the whole book. The earlier parts on setting up a PyQt development environment and such are still somewhat helpful; later on, the introductions to various widgets aren't that detailed. Some useful things aren't even covered in the book, so you still have to rely on Google. So, this book can only be described as the tallest among dwarfs—barely satisfactory~

Welcome to Discuss

Programming Lab focuses on exploring popular new internet technologies and team agile development practices. Reply linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account "Programming Lab" to get related technical articles and materials. You can also leave a message in the official account backend if you have any questions~

_EOF_

PyQt5 Development Practice (Part 1: Preparation)

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…