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

QT小项目-简单的记事本

这次用的配置好了QT环境的vs2022编写,与之前有些不同,比如QT creator里,ui界面是用指针编写,在vs则变为结构体,其次,由于vs是用的MSVC套件,一般作用与windows系统,不考虑跨平台,如需跨平台则用QT creator编写,项目编写与QT creator区别不是很大,注意细节,第一次使用VS编写随便写个简单的项目
.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_Notepad.h"
#include <QDebug>
#include <qfiledialog.h>
#include <qstandardpaths.h>
#include <qfile.h>
#include <qsavefile.h>
#include <qmessagebox.h>
#include <qtextstream.h>


class Notepad : public QMainWindow
{
    Q_OBJECT

public:
    Notepad(QWidget *parent = nullptr);
    ~Notepad();
public slots:
    void openfile();
    void savefile();

private:
    Ui::NotepadClass ui;
};

.cpp

#include "Notepad.h"

Notepad::Notepad(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    connect(ui.open_action, &QAction::triggered, this, &Notepad::openfile);
    connect(ui.save_action, &QAction::triggered, this, &Notepad::savefile);
}

void Notepad::openfile()
{
    qDebug() << "open file";
    QString path = QFileDialog::getOpenFileName(this, tr("open file"), QStandardPaths::standardLocations
    (QStandardPaths::DocumentsLocation)[0]);
    if (path.isEmpty())
    {
        return;
    }

    QFile file(path);
    
    if (!file.open(QFile::ReadOnly))
    {
        qDebug() << "File opening failed";
        return;
    }
    QByteArray date = file.readAll();
    //QString str = QString::fromLocal8Bit(date);//适用于处理系统本地编码格式的字符串
    QString str = QString::fromUtf8(date);//用于明确知道数据是 UTF-8 编码的情况
    ui.plainTextEdit->clear();
    ui.plainTextEdit->setPlainText(str);
}

void Notepad::savefile()
{
    QString path = QFileDialog::getSaveFileName(this, "Saved location");
    if (path.isEmpty())
        return;
    QSaveFile file(path);
    if (!file.open(QFile::WriteOnly))
    {
        QMessageBox::critical(this, "ERROR", "File save failed");
        return;
    }
    QTextStream out(&file);//利用QT文本流,将内容输入到文本
    out << ui.plainTextEdit->toPlainText();
    if (!file.commit())//目的检测是否成功修改文本
    {
        QMessageBox::critical(this, "ERROR", "File save failed");
        return;
    }
    else
    {
        QMessageBox::information(this, "tips", "File saved successfully");
    }
}

Notepad::~Notepad()
{
}

.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>NotepadClass</class>
 <widget class="QMainWindow" name="NotepadClass">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>600</width>
    <height>400</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Notepad</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QPlainTextEdit" name="plainTextEdit"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>600</width>
     <height>33</height>
    </rect>
   </property>
   <widget class="QMenu" name="fileMenu">
    <property name="title">
     <string>文件</string>
    </property>
    <addaction name="open_action"/>
    <addaction name="separator"/>
    <addaction name="save_action"/>
   </widget>
   <widget class="QMenu" name="editMenu">
    <property name="title">
     <string>编辑</string>
    </property>
   </widget>
   <widget class="QMenu" name="styleMenu">
    <property name="title">
     <string>格式</string>
    </property>
   </widget>
   <widget class="QMenu" name="viewMenu">
    <property name="title">
     <string>查看</string>
    </property>
   </widget>
   <widget class="QMenu" name="helpMenu">
    <property name="title">
     <string>帮助</string>
    </property>
   </widget>
   <addaction name="fileMenu"/>
   <addaction name="editMenu"/>
   <addaction name="styleMenu"/>
   <addaction name="viewMenu"/>
   <addaction name="helpMenu"/>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="open_action">
   <property name="text">
    <string>打开文件</string>
   </property>
  </action>
  <action name="save_action">
   <property name="text">
    <string>保存文件</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="Notepad.qrc"/>
 </resources>
 <connections/>
</ui>


http://www.kler.cn/a/581162.html

相关文章:

  • 1.5 双指针专题:有效三⻆形的个数(medium)
  • Flink之水印(watermark)的补充理解
  • Linux驱动开发-设备树
  • python高效试用17---两个字符串组成一个新的字符串和两个字符串组成元组作为key哪个更高效
  • PyCharm 接入 DeepSeek、OpenAI、Gemini、Mistral等大模型完整版教程(通用)!
  • Qt不同窗口类的控件信号和槽绑定
  • Excel 中如何实现数据透视表?
  • 复现无人机的项目,项目名称为Evidential Detection and Tracking Collaboration
  • NPM安装与配置全流程详解(2025最新版)
  • Python基础之threading多线程同时运行程序
  • 衣联网的商品列表页面结构是怎样的?
  • 前端项目中创建自动化部署脚本,用于 Jenkins 触发 npm run publish 来完成远程部署
  • 外层元素旋转,其包括在内的子元素一并旋转(不改变旋转中心),单元测试
  • 爱普生可编程晶振SG-8200CJ特性与应用
  • Sentinel熔断降级
  • Seata简要说明
  • 《C#上位机开发从门外到门内》2-2:I2C总线协议及其应用详解
  • lua如何写出高性能的kong网关插件
  • ctf-web: php原生类利用 -- GHCTF Popppppp
  • 说一下spring的事务隔离级别?