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 的高级功能。

相关推荐
llwszx14 分钟前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务
YongGit29 分钟前
探索 AI + MCP 渲染前端 UI
前端·后端·node.js
77qqqiqi1 小时前
正则表达式
java·后端·正则表达式
@大迁世界2 小时前
AR 如何改变我们构建网站的方式
后端·ar·restful
RainbowSea2 小时前
问题:后端由于字符内容过长,前端展示精度丢失修复
java·spring boot·后端
风象南2 小时前
SpringBoot 控制器的动态注册与卸载
java·spring boot·后端
前端付豪2 小时前
17、自动化才是正义:用 Python 接管你的日常琐事
后端·python
我是一只代码狗2 小时前
springboot中使用线程池
java·spring boot·后端
PanZonghui3 小时前
Centos项目部署之安装数据库MySQL8
linux·后端·mysql
Victor3563 小时前
MySQL(119)如何加密存储敏感数据?
后端