【QT实战の心情笔记】

打印 上一主题 下一主题

主题 943|帖子 943|积分 2829

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
此步伐使用 Qt Widgets 和 SQLite 来实现,并展示了笔记的标题、内容以及修改时间。
为了帮助你明白如何计划这个个人笔记应用的界面,下面我将具体描述界面布局以及各个控件的功能。由于我无法直接生成图像,我会尽量通过笔墨和布局描述来帮助你在 Qt Designer 中实现界面。
界面布局

重要界面分为三部分:


  • 笔记列表区域(显示全部笔记的标题和修改时间)
  • 笔记内容编辑区域(编辑选中的笔记的内容)
  • 操纵按钮区域(添加、删除、生存等按钮)
1. 笔记列表区域



  • 使用 QListView 来显示笔记的标题和修改时间。
  • 每个笔记的显示格式为:

    • 标题(一行笔墨)
    • 修改时间(时间戳,格式为 yyyy-MM-dd HH:mm:ss)

QListView 上需要显示的内容是:
  1. Title 1
  2. 2024-12-18 15:30:00
  3. Title 2
  4. 2024-12-17 14:20:00
复制代码
2. 笔记内容编辑区域



  • 使用 QTextEdit 来显示和编辑选中的笔记的具体内容。
  • 用户可以在这里修改笔记内容,当生存时内容会更新到数据库。
3. 操纵按钮区域



  • 添加按钮:用户点击后可以创建一个新笔记。
  • 删除按钮:用户点击后可以删除当前选中的笔记。
  • 生存按钮:用户点击后生存当前编辑的笔记内容。
Qt Designer 界面计划步调

Qt Designer 中,你可以按照以下步调创建界面:

  • 创建一个 QMainWindow

    • 选择 QMainWindow 作为主窗口类型。

  • 添加一个 QWidget

    • 将一个 QWidget 拖入 QMainWindow 中,作为主界面的容器。

  • 设置布局

    • 在 QWidget 中,设置一个 QHBoxLayoutQVBoxLayout 来构造控件。
    • 推荐使用 QVBoxLayout,将界面分成垂直区域。

  • 添加 QListView 控件

    • 在上方区域添加一个 QListView 控件,宽度设置为适当的巨细来显示笔记的标题和修改时间。
    • 设置 QListView 的巨细策略,使其能够添补上方区域。

  • 添加 QTextEdit 控件

    • 在下方区域添加一个 QTextEdit 控件,用来显示和编辑笔记的内容。
    • 设置其巨细策略,使其添补剩余的空间。

  • 添加操纵按钮

    • 在界面底部,使用 QHBoxLayout 添加三个按钮:

      • Add 按钮:用于创建新笔记。
      • Delete 按钮:用于删除选中的笔记。
      • Save 按钮:用于生存当前编辑的笔记。


完整界面布局图

  1. --------------------------------------
  2. |             笔记列表区域            |
  3. |------------------------------------|
  4. | Title 1       2024-12-18 15:30:00  |
  5. | Title 2       2024-12-17 14:20:00  |
  6. | ...                                  |
  7. --------------------------------------
  8. |             笔记内容编辑区域        |
  9. |------------------------------------|
  10. | [此处显示或编辑笔记内容]              |
  11. --------------------------------------
  12. |      [Add]    [Delete]    [Save]    |
  13. --------------------------------------
复制代码
各控件设置和属性


  • QListView

    • 需要绑定一个 QStringListModelQListView 模型来显示笔记的标题和修改时间。
    • 在模型中,每个条目应当是 “标题 + 修改时间”。

  • QTextEdit

    • 设置 setPlainText()setHtml() 方法来显示笔记内容。
    • 用户可以编辑其中的内容。

  • 按钮

    • 设置按钮的文本为 AddDeleteSave,并为这些按钮添加相应的信号与槽连接。

Qt Designer 文件 (.ui)

你可以按照以上描述在 Qt Designer 中创建界面,下面是一个简化版的 .ui 文件布局,你可以在 Qt Designer 中直接创建这个界面。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MainWindow</class>
  4. <widget class="QMainWindow" name="MainWindow">
  5.   <widget class="QWidget" name="centralwidget">
  6.    <layout class="QVBoxLayout" name="verticalLayout">
  7.     <widget class="QListView" name="noteListView"/>
  8.     <widget class="QTextEdit" name="noteTextEdit"/>
  9.     <layout class="QHBoxLayout" name="buttonLayout">
  10.      <widget class="QPushButton" name="addButton">
  11.       <property name="text">
  12.        <string>Add</string>
  13.       </property>
  14.      </widget>
  15.      <widget class="QPushButton" name="deleteButton">
  16.       <property name="text">
  17.        <string>Delete</string>
  18.       </property>
  19.      </widget>
  20.      <widget class="QPushButton" name="saveButton">
  21.       <property name="text">
  22.        <string>Save</string>
  23.       </property>
  24.      </widget>
  25.     </layout>
  26.    </layout>
  27.   </widget>
  28.   <centralwidget/>
  29.   <statusbar/>
  30. </widget>
  31. </ui>
复制代码
数据库表布局

首先,我们需要一个数据库表来存储笔记。创建数据库和表布局。
SQL 表布局:

  1. CREATE TABLE IF NOT EXISTS notes (
  2.     id INTEGER PRIMARY KEY AUTOINCREMENT,
  3.     title TEXT NOT NULL,
  4.     content TEXT,
  5.     modified_time TEXT
  6. );
复制代码


  • id:笔记的唯一标识符。
  • title:笔记的标题。
  • content:笔记的内容。
  • modified_time:笔记的修改时间(格式为 yyyy-MM-dd HH:mm:ss)。
逻辑代码

1. 项目布局

该项目包含以下文件:


  • Note.h 和 Note.cpp:定义笔记类。
  • DatabaseManager.h 和 DatabaseManager.cpp:管理数据库的类。
  • MainWindow.ui:Qt Designer 计划的主窗口界面。
  • MainWindow.cpp 和 MainWindow.h:主窗口的实现和交互逻辑。
2. Note 类 (Note.h 和 Note.cpp)

Note 类用于表示单个笔记,包含标题、内容和修改时间等信息。
Note.h

  1. #ifndef NOTE_H
  2. #define NOTE_H
  3. #include <QString>
  4. class Note
  5. {
  6. public:
  7.     Note();
  8.     int getId() const;
  9.     QString getTitle() const;
  10.     QString getContent() const;
  11.     QString getModifiedTime() const;
  12.     void setId(int id);
  13.     void setTitle(const QString &title);
  14.     void setContent(const QString &content);
  15.     void setModifiedTime(const QString &modifiedTime);
  16. private:
  17.     int id;
  18.     QString title;
  19.     QString content;
  20.     QString modifiedTime;
  21. };
  22. #endif // NOTE_H
复制代码
Note.cpp

  1. #include "Note.h"
  2. Note::Note() : id(-1), title(""), content(""), modifiedTime("") {}
  3. int Note::getId() const { return id; }
  4. QString Note::getTitle() const { return title; }
  5. QString Note::getContent() const { return content; }
  6. QString Note::getModifiedTime() const { return modifiedTime; }
  7. void Note::setId(int id) { this->id = id; }
  8. void Note::setTitle(const QString &title) { this->title = title; }
  9. void Note::setContent(const QString &content) { this->content = content; }
  10. void Note::setModifiedTime(const QString &modifiedTime) { this->modifiedTime = modifiedTime; }
复制代码
3. DatabaseManager 类 (DatabaseManager.h 和 DatabaseManager.cpp)

DatabaseManager 类用于操纵 SQLite 数据库,实行插入、删除、更新和查询等操纵。
DatabaseManager.h

  1. #ifndef DATABASEMANAGER_H
  2. #define DATABASEMANAGER_H
  3. #include <QObject>
  4. #include <QSqlDatabase>
  5. #include <QSqlQuery>
  6. #include <QSqlError>
  7. #include <QVariant>
  8. #include <QList>
  9. #include "Note.h"
  10. class DatabaseManager : public QObject
  11. {
  12.     Q_OBJECT
  13. public:
  14.     DatabaseManager();
  15.     bool openDatabase();
  16.     bool addNote(const QString &title, const QString &content);
  17.     bool deleteNote(int id);
  18.     bool updateNote(int id, const QString &title, const QString &content);
  19.     QList<Note> getAllNotes();
  20. private:
  21.     QSqlDatabase db;
  22. };
  23. #endif // DATABASEMANAGER_H
复制代码
DatabaseManager.cpp

  1. #include "DatabaseManager.h"
  2. #include <QSqlError>
  3. #include <QDebug>
  4. #include <QDateTime>
  5. DatabaseManager::DatabaseManager()
  6. {
  7.     db = QSqlDatabase::addDatabase("QSQLITE");
  8.     db.setDatabaseName("notes.db");
  9. }
  10. bool DatabaseManager::openDatabase()
  11. {
  12.     if (!db.open()) {
  13.         qDebug() << "Error: Failed to open database!";
  14.         return false;
  15.     }
  16.     return true;
  17. }
  18. bool DatabaseManager::addNote(const QString &title, const QString &content)
  19. {
  20.     QSqlQuery query;
  21.     query.prepare("INSERT INTO notes (title, content, modified_time) VALUES (?, ?, ?)");
  22.     query.addBindValue(title);
  23.     query.addBindValue(content);
  24.     query.addBindValue(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
  25.     if (!query.exec()) {
  26.         qDebug() << "Error: Failed to add note!";
  27.         return false;
  28.     }
  29.     return true;
  30. }
  31. bool DatabaseManager::deleteNote(int id)
  32. {
  33.     QSqlQuery query;
  34.     query.prepare("DELETE FROM notes WHERE id = ?");
  35.     query.addBindValue(id);
  36.     if (!query.exec()) {
  37.         qDebug() << "Error: Failed to delete note!";
  38.         return false;
  39.     }
  40.     return true;
  41. }
  42. bool DatabaseManager::updateNote(int id, const QString &title, const QString &content)
  43. {
  44.     QSqlQuery query;
  45.     query.prepare("UPDATE notes SET title = ?, content = ?, modified_time = ? WHERE id = ?");
  46.     query.addBindValue(title);
  47.     query.addBindValue(content);
  48.     query.addBindValue(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
  49.     query.addBindValue(id);
  50.     if (!query.exec()) {
  51.         qDebug() << "Error: Failed to update note!";
  52.         return false;
  53.     }
  54.     return true;
  55. }
  56. QList<Note> DatabaseManager::getAllNotes()
  57. {
  58.     QList<Note> notes;
  59.     QSqlQuery query("SELECT * FROM notes");
  60.     while (query.next()) {
  61.         Note note;
  62.         note.setId(query.value("id").toInt());
  63.         note.setTitle(query.value("title").toString());
  64.         note.setContent(query.value("content").toString());
  65.         note.setModifiedTime(query.value("modified_time").toString());
  66.         notes.append(note);
  67.     }
  68.     return notes;
  69. }
复制代码
4. MainWindow 类 (MainWindow.ui, MainWindow.cpp 和 MainWindow.h)

在 MainWindow 类中,我们需要显示笔记的列表,包括标题、内容和修改时间,同时实现笔记的添加、删除和生存功能。
MainWindow.ui

Qt Designer 中,计划一个界面:


  • 使用 QListView 显示笔记列表,格式化显示 标题修改时间
  • 使用 QTextEdit 编辑和显示笔记内容。
  • 使用 QPushButton 实现笔记的添加、删除和生存功能。
MainWindow.cpp

  1. #include "MainWindow.h"
  2. #include "ui_MainWindow.h"
  3. #include "DatabaseManager.h"
  4. #include "Note.h"
  5. #include <QInputDialog>
  6. #include <QMessageBox>
  7. #include <QDateTime>
  8. MainWindow::MainWindow(QWidget *parent)
  9.     : QMainWindow(parent)
  10.     , ui(new Ui::MainWindow)
  11. {
  12.     ui->setupUi(this);
  13.     dbManager = new DatabaseManager();
  14.     // 打开数据库
  15.     if (!dbManager->openDatabase()) {
  16.         qDebug() << "Failed to open the database!";
  17.         return;
  18.     }
  19.     // 更新笔记列表
  20.     updateNoteList();
  21.     // 连接按钮的信号槽
  22.     connect(ui->addButton, &QPushButton::clicked, this, &MainWindow::addNote);
  23.     connect(ui->deleteButton, &QPushButton::clicked, this, &MainWindow::deleteNote);
  24.     connect(ui->saveButton, &QPushButton::clicked, this, &MainWindow::saveNote);
  25. }
  26. MainWindow::~MainWindow()
  27. {
  28.     delete ui;
  29. }
  30. void MainWindow::updateNoteList()
  31. {
  32.     // 获取所有笔记
  33.     QList<Note> notes = dbManager->getAllNotes();
  34.     QStringList noteTitles;
  35.     // 格式化笔记标题和修改时间
  36.     for (const Note &note : notes) {
  37.         QString displayText = note.getTitle() + "\n" + note.getModifiedTime();
  38.         noteTitles.append(displayText);
  39.     }
  40.     // 更新笔记列表显示
  41.     ui->noteListView->clear();
  42.     ui->noteListView->addItems(noteTitles);
  43.     // 更新当前选中的笔记内容
  44.     if (!notes.isEmpty()) {
  45.         ui->noteTextEdit->setText(notes.first().getContent());
  46.     }
  47. }
  48. void MainWindow::addNote()
  49. {
  50.     bool ok;
  51.     QString title = QInputDialog::getText(this, "New Note", "Enter note title:", QLineEdit::Normal, "", &ok);
  52.     if (ok && !title.isEmpty()) {
  53.         dbManager->addNote(title, "");
  54.         updateNoteList();
  55.     }
  56. }
  57. void MainWindow::deleteNote()
  58. {
  59.     QModelIndex selectedIndex = ui->noteListView->currentIndex();
  60.     if (selectedIndex.isValid()) {
  61.         int noteId = selectedIndex.row() + 1;
  62.         dbManager->deleteNote(noteId);
  63.         updateNoteList();
  64.     }
  65. }
  66. void MainWindow::saveNote()
  67. {
  68.     QModelIndex selectedIndex = ui->noteListView->currentIndex();
  69.     if (selectedIndex.isValid()) {
  70.         int noteId = selectedIndex.row() + 1;
  71.         QString content = ui->noteTextEdit->toPlainText();
  72.         dbManager->updateNote(noteId, ui->noteListView->currentItem()->text(), content);
  73.         updateNoteList(); // 更新笔记列表
  74.     }
  75. }
复制代码
MainWindow.h

  1. #ifndef MAINWINDOW_H
  2. #define
  3. MAINWINDOW_H
  4. #include <QMainWindow>
  5. #include "DatabaseManager.h"
  6. namespace Ui {
  7. class MainWindow;
  8. }
  9. class MainWindow : public QMainWindow
  10. {
  11.     Q_OBJECT
  12. public:
  13.     explicit MainWindow(QWidget *parent = nullptr);
  14.     ~MainWindow();
  15. private slots:
  16.     void addNote();
  17.     void deleteNote();
  18.     void saveNote();
  19. private:
  20.     Ui::MainWindow *ui;
  21.     DatabaseManager *dbManager;
  22.     void updateNoteList();
  23. };
  24. #endif // MAINWINDOW_H
复制代码
5. 总结

这段代码实现了一个简单的笔记应用,支持:


  • 添加笔记:点击按钮输入标题创建新笔记。
  • 删除笔记:选择列表中的笔记删除。
  • 生存笔记:编辑笔记内容后点击生存。
笔记列表中显示每个笔记的 标题修改时间,通过 SQLite 数据库进行数据存储和管理。
可以进一步扩展应用功能,例如:


  • 添加笔记的分类。
  • 笔记搜刮功能。
  • 导出和导入笔记等。
   天下上的事变,最忌讳的就是个十全十美,凡事总要稍留短缺,才气持恒

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表