当前位置: 首页 > news >正文

Qt项目锻炼——TODO清单(三)

创建主窗口 MainWindow,添加 QTableView 显示任务

步骤

  • 创建 MainWindow 类,继承自 QMainWindow。
  • 在 MainWindow 的构造函数中,创建 TaskModel 和 QTableView,并将 TaskModel 设置为 QTableView 的模型。

头文件

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_MainWindow.h"
#include"Task.h"
#include<qtableview.h>class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindowClass ui;QTableView* taskTableView; TaskModel* taskModel;
};

为什么成员变量不使用智能指针,因为Qt有自己的一套资源回收机制,通过指定父窗口实现当父窗口销毁时,子窗口一同销毁。

源文件

#include "MainWindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);taskModel = new TaskModel(this);taskTableView = new QTableView(this);taskTableView->setModel(taskModel);setCentralWidget(taskTableView);
}MainWindow::~MainWindow()
{}

实现任务的增删改查功能

步骤

  • 在 MainWindow 中添加按钮用于添加、删除、修改和查询任务。
  • 连接按钮的点击信号到相应的槽函数,在槽函数中调用 TaskModel 的方法实现任务的增删改查。

头文件


class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_addButton_clicked();void on_deleteButton_clicked();void on_updateButton_clicked();void on_searchButton_clicked();private:Ui::MainWindowClass ui;QTableView* taskTableView; TaskModel* taskModel;
};

源文件


void MainWindow::on_addButton_clicked()
{bool ok;QString title = QInputDialog::getText(this, "Add Task", "Title:", QLineEdit::Normal, "", &ok);if (ok && !title.isEmpty()){Task task(-1, title, "", Task::Medium, QDate(), false);if (taskModel->addTask(task)){QMessageBox::information(this, "Success", "Task added successfully.");}else{QMessageBox::critical(this, "Error", "Failed to add task.");}}
}void MainWindow::on_deleteButton_clicked()
{QModelIndexList selectedRows = taskTableView->selectionModel()->selectedRows();if (!selectedRows.isEmpty()){int taskId = taskModel->data(taskModel->index(selectedRows.first().row(), TaskModel::IdColumn)).toInt();if (taskModel->deleteTask(taskId)){QMessageBox::information(this, "Success", "Task deleted successfully.");}else{QMessageBox::critical(this, "Error", "Failed to delete task.");}}
}void MainWindow::on_updateButton_clicked()
{QModelIndexList selectedRows = taskTableView->selectionModel()->selectedRows();if (!selectedRows.isEmpty()){int taskId = taskModel->data(taskModel->index(selectedRows.first().row(), TaskModel::IdColumn)).toInt();bool ok;QString title = QInputDialog::getText(this, "Update Task", "Title:", QLineEdit::Normal,taskModel->data(taskModel->index(selectedRows.first().row(), TaskModel::TitleColumn)).toString(), &ok);if (ok && !title.isEmpty()){Task task(taskId, title, "", Task::Medium, QDate(), false);if (taskModel->updateTask(task)){QMessageBox::information(this, "Success", "Task updated successfully.");}else{QMessageBox::critical(this, "Error", "Failed to update task.");}}}
}void MainWindow::on_searchButton_clicked()
{bool ok;QString searchText = QInputDialog::getText(this, "Search Task", "Search:", QLineEdit::Normal, "", &ok);if (ok){taskModel->setSearchText(searchText);}
}我先创建对应的槽函数,为后面按键信号与槽链接做准备。
http://www.dtcms.com/a/267737.html

相关文章:

  • 【论文笔记】OctoThinker:突破 Llama 推理瓶颈的中期训练范式
  • 乌邦图(20.04)添加中文拼音(中文输入法)
  • 实现电池储能装置的双向DCDC
  • Qt项目锻炼——TODO清单(二)
  • jmm--volatile
  • 前端面试专栏-算法篇:18. 查找算法(二分查找、哈希查找)
  • vue3 el-input el-select 非空校验
  • 大数据学习2:HIve
  • Linux进程管理:从基础到实战
  • Qt Ribbon效果界面
  • QT6 源(154)模型视图架构里的列表视图 QListView:先学习属性部分,
  • 认识Redis
  • Chat Model API
  • 60天python训练营打卡day52
  • 运算方法和运算器补充
  • 如何录制带备注的演示文稿(LaTex Beamer + Pympress)
  • Codeforces Round 919 (Div. 2) D. Array Repetition(分块,1900)
  • 【深圳大学机器学习】实验一:PCA算法
  • 【ACL系列论文写作指北15-如何进行reveiw】-公平、公正、公开
  • 大数据学习1:Hadoop单机版环境搭建
  • Redis 哨兵模式部署--docker版本
  • C++面试-auto,auto,auto 的区别
  • 【ESP32】2.多任务处理
  • 相机位姿估计
  • 使用接口测试工具类Postman和浏览器的差异
  • C++ 语言特性31 - 协程介绍(2)
  • 用 Turbo Vision 2 为 Qt 6 控制台应用创建 TUI 字符 MainFrame
  • Redis性能优化
  • 五、Python新特性指定类型用法
  • AI大模型(六)Langchain核心模块与实战(一)