QT小项目-简单的记事本

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

.h

cpp 复制代码
#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

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

cpp 复制代码
<?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>
相关推荐
Scott9999HH5 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社6 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海6 小时前
python 列表、元组、集合和字典
开发语言·python
萧瑟余晖7 小时前
JDK 26 新特性详解
java·开发语言
马优晨8 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
人邮异步社区10 小时前
怎么把C语言学到精通?
c语言·开发语言
心平气和量大福大10 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai11 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
এ慕ོ冬℘゜11 小时前
前端基础:什么是时间戳?JS获取时间戳三种方法与实战用途
开发语言·前端·javascript
执明wa11 小时前
LayoutInflater详解: XML是如何变成View的?
android·xml·开发语言·android studio