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 分钟前
彻底搞懂 Linux 基础 IO:从文件操作到缓冲区,打通底层逻辑
linux·服务器·c++·后端
lizhongxuan10 分钟前
Spec-Kit 使用指南
后端
会豪27 分钟前
工业仿真(simulation)--发生器,吸收器,缓冲区(2)
后端
SamDeepThinking27 分钟前
使用Cursor生成【财务对账系统】前后端代码
后端·ai编程·cursor
饭碗的彼岸one28 分钟前
C++ 并发编程:异步任务
c语言·开发语言·c++·后端·c·异步
会豪33 分钟前
工业仿真(simulation)--仿真引擎,离散事件仿真(1)
后端
Java微观世界34 分钟前
匿名内部类和 Lambda 表达式为何要求外部变量是 final 或等效 final?原理与解决方案
java·后端
SimonKing1 小时前
全面解决中文乱码问题:从诊断到根治
java·后端·程序员
几颗流星1 小时前
Java 中使用 CountDownLatch 增加线程竞争,帮助复现并发问题
后端
郑洁文1 小时前
基于SpringBoot的天气预报系统的设计与实现
java·spring boot·后端·毕设