PyQt 高级部分实战

在这篇文章中,我们将通过一个实战项目来综合运用之前学习的 PyQt 高级部分的知识。我们将创建一个简单的文件管理器。

项目需求

  • 文件和文件夹列表展示
  • 打开文件和文件夹
  • 删除文件或文件夹
  • 数据库记录用户操作

环境准备

确保你已经安装了 PyQt5 和 PyQt5-sip。

bash 复制代码
pip install PyQt5 PyQt5-sip

代码实现

主窗口

首先,我们创建一个主窗口来展示文件和文件夹。

python 复制代码
from PyQt5.QtWidgets import QMainWindow, QTreeView, QFileSystemModel, QVBoxLayout, QWidget, QPushButton
from PyQt5.QtCore import QDir

class FileManager(QMainWindow):
    def __init__(self):
        super().__init__()

        self.fileSystemModel = QFileSystemModel()
        self.fileSystemModel.setRootPath(QDir.rootPath())

        self.tree = QTreeView()
        self.tree.setModel(self.fileSystemModel)
        self.tree.setRootIndex(self.fileSystemModel.index(QDir.rootPath()))

        self.deleteButton = QPushButton("Delete")
        self.deleteButton.clicked.connect(self.delete_item)

        layout = QVBoxLayout()
        layout.addWidget(self.tree)
        layout.addWidget(self.deleteButton)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

删除功能

我们添加一个按钮来删除选中的文件或文件夹。

python 复制代码
from PyQt5.QtWidgets import QMessageBox

def delete_item(self):
    index = self.tree.currentIndex()
    file_path = self.fileSystemModel.filePath(index)

    msgBox = QMessageBox()
    msgBox.setText(f"Are you sure you want to delete {file_path}?")
    msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
    returnValue = msgBox.exec()

    if returnValue == QMessageBox.Yes:
        self.fileSystemModel.remove(index)

数据库记录

我们使用 SQLite 数据库来记录用户的删除操作。

python 复制代码
from PyQt5.QtSql import QSqlDatabase, QSqlQuery

def init_db(self):
    self.db = QSqlDatabase.addDatabase("QSQLITE")
    self.db.setDatabaseName("file_manager.db")

    if not self.db.open():
        print("Cannot open database")
        return

    query = QSqlQuery()
    query.exec_("CREATE TABLE IF NOT EXISTS operations (id INTEGER PRIMARY KEY, action TEXT, path TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)")

delete_item 方法中,我们添加数据库记录。

python 复制代码
def delete_item(self):
    # ... (existing code)
    if returnValue == QMessageBox.Yes:
        query = QSqlQuery()
        query.prepare("INSERT INTO operations (action, path) VALUES (:action, :path)")
        query.bindValue(":action", "delete")
        query.bindValue(":path", file_path)
        query.exec_()
        self.fileSystemModel.remove(index)

完整代码

将以上代码片段整合,形成完整的文件管理器应用。

运行

运行你的代码,你将看到一个简单但功能完整的文件管理器。

这就是我们的 PyQt 高级部分实战项目。通过这个项目,你应该能更好地理解如何在实际应用中使用 PyQt 的高级功能。

相关推荐
武子康1 分钟前
大数据-255 离线数仓 - Apache Atlas 数据血缘与元数据管理实战指南
大数据·后端·apache hive
javaTodo1 分钟前
IntelliJ IDEA 2026.1 上强度了:Spring 运行时 Debug + AI 全面接入,太香了
后端
晴栀ay39 分钟前
Generator + RxJS 重构 LLM 流式输出的“丝滑”架构
javascript·后端·llm
下次一定x42 分钟前
深度解析 Kratos 客户端服务发现与负载均衡:从 Dial 入口到 gRPC 全链路落地(下篇)
后端·go
彭于晏Yan2 小时前
SpringBoot整合ECC实现文件签名与验签
java·spring boot·后端
pupudawang2 小时前
Spring EL 表达式的简单介绍和使用
java·后端·spring
xianjian09122 小时前
springboot与springcloud以及springcloudalibaba版本对照
spring boot·后端·spring cloud
羊小猪~~2 小时前
【QT】-- QMainWindow简介
开发语言·数据库·c++·后端·qt·前端框架·求职招聘
ruxingli3 小时前
GoLang的并发如何避免死锁
开发语言·后端·golang
Tyooughtul3 小时前
MySQL篇 索引失效
后端