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>
相关推荐
用户805533698033 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner3 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz8 天前
QML Hello World 入门示例
qt
xcyxiner11 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner12 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner12 天前
DicomViewer (添加模型类)3
qt
xcyxiner13 天前
DicomViewer (目录调整) 2
qt
xcyxiner13 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00615 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术15 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript