• QT记事本+登陆界面的简单实现


    主体头文件

    #ifndef JSB_H
    #define JSB_H
    
    #include 
    #include //菜单栏
    #include //工具栏
    #include //状态栏
    #include //文本
    #include //标签
    #include //打印
    #include //获取当前时间
    #include //桌面服务
    #include //资源定位
    #include "ipv.h"
    #include //文件对话框
    #include //颜色对话框
    #include //字体对话框
    #include //文本流
    #include 
    #include 
    #include 
    class JSB : public QMainWindow
    {
        Q_OBJECT
    
    public:
        JSB(QWidget *parent = nullptr);
        ~JSB();
    
    public slots:
        void open_slot();
        void new_slot();
        void save_slot();
        void exit_slot();
    
        void copy_slot();
        void patse_slot();
        void cut_slot();
        void redo_slot();
        void undo_slot();
    
        void time_slot();
    
        void color_slot();
        void font_slot();
    
        void about_slot();
        void web_slot();
        void ipv_slot();
    
    
       void closeEvent(QCloseEvent *event);
    
    private:
        //菜单栏
        QMenuBar * bar=NULL;
        //文件菜单
        QMenu *File = NULL;
        //打开
        QAction *Open = NULL;
        //新建
        QAction *New = NULL;
        //保存
        QAction *Save = NULL;
        //退出
        QAction *Exit = NULL;
    
        //编辑菜单
        QMenu *Edit = NULL;
        //恢复
        QAction *Redo = NULL;
        //撤销
        QAction *Undo = NULL;
        //复制
        QAction *Copy = NULL;
        //粘贴
        QAction *Patse = NULL;
        //剪切
        QAction *Cut = NULL;
    
        //工具菜单
        QMenu * Tool = NULL;
        //时间
        QAction *Time = NULL;
    
        //格式菜单
        QMenu * ForMat = NULL;
        //颜色
        QAction *Color = NULL;
        //字体
        QAction *Font = NULL;
    
        //帮助
        QMenu *Help = NULL;
        //关于
        QAction *About = NULL;
        //官网
        QAction *Web = NULL;
        //版本
        QAction *IPv = NULL;
    
        //工具栏
        QToolBar *ToolBar = NULL;
        //状态栏
        QStatusBar *staBar = NULL;
    
    
        //文本
        QTextEdit *edit = NULL;
    
    
    };
    #endif // JSB_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    主体的函数实现

    #include "jsb.h"
    
    JSB::JSB(QWidget *parent)
        : QMainWindow(parent)
    {
        //设置窗口大小
        this->resize(600,600);
        //设置窗口文本
        this->setWindowTitle("记事本");
        //设置窗口图标
        this->setWindowIcon(QIcon(":/image/Jsb.png"));
    
        //初始化菜单
        this->bar = menuBar();
        //指定父类
        bar->setParent(this);
        //将菜单栏放入到窗口
        setMenuBar(bar);
        //创建文件菜单
        this->File = bar->addMenu("文件(&F)");
        //菜单项
        this->New = File->addAction("新建");
        //快捷键
        this->New->setShortcut(tr("Ctrl+N"));
        //分隔符
        this->File->addSeparator();
        //新建图标
        this->New->setIcon(QIcon(":/image/New.png"));
        connect(New,&QAction::triggered,this,&JSB::new_slot);
    
        this->Open = File->addAction("打开");
        //打开快捷键
        this->Open->setShortcut(tr("Ctrl+O"));
        //图标
        this->Open->setIcon(QIcon(":/image/Open.png"));
        connect(Open,&QAction::triggered,this,&JSB::open_slot);
    
    
        this->Save = File->addAction("保存");
        this->Save->setShortcut(tr("Ctrl+S"));
        this->Save->setIcon(QIcon(":/image/Save.png"));
        connect(Save,&QAction::triggered,this,&JSB::save_slot);
    
        //分隔符
        this->File->addSeparator();
        this->Exit = File->addAction("退出");
        this->Exit->setShortcut(tr("Ctrl+Q"));
        this->Exit->setIcon(QIcon(":/image/Exit.png"));
        connect(Exit,&QAction::triggered,this,&JSB::exit_slot);
    
    
        //创建编辑菜单
        this->Edit = bar->addMenu("编辑(&E)");
        this->Redo = Edit->addAction("恢复");
        this->Redo->setShortcut(tr("Ctrl+R"));
        this->Redo->setIcon(QIcon(":/image/Redo.png"));
        connect(Redo,&QAction::triggered,this,&JSB::redo_slot);
    
        this->Undo = Edit->addAction("撤销");
        this->Undo->setShortcut(tr("Ctrl+U"));
        this->Undo->setIcon(QIcon(":/image/Undo.png"));
        //分隔符
        this->Edit->addSeparator();
        connect(Undo,&QAction::triggered,this,&JSB::undo_slot);
    
        this->Copy = Edit->addAction("复制");
        this->Copy->setShortcut(tr("Ctrl+C"));
        this->Copy->setIcon(QIcon(":/image/Copy.png"));
        connect(Copy,&QAction::triggered,this,&JSB::copy_slot);
    
        this->Patse = Edit->addAction("粘贴");
        this->Patse->setShortcut(tr("Ctrl+V"));
        this->Patse->setIcon(QIcon(":/image/Paste.png"));
        connect(Patse,&QAction::triggered,this,&JSB::patse_slot);
    
        this->Cut = Edit->addAction("剪切");
        this->Cut->setShortcut(tr("Ctrl+X"));
        this->Cut->setIcon(QIcon(":/image/Cut.png"));
        connect(Cut,&QAction::triggered,this,&JSB::cut_slot);
        //创建工具菜单
        this->Tool = bar->addMenu("工具(&T)");
        this->Time = Tool->addAction("时间");
        this->Time->setShortcut(tr("Ctrl+A"));
        this->Time->setIcon(QIcon(":/image/time.png"));
         connect(Time,&QAction::triggered,this,&JSB::time_slot);
        //创建格式菜单
        this->ForMat = bar->addMenu("格式(&M)");
        this->Color = ForMat->addAction("颜色");
        this->Color->setShortcut(tr("Ctrl+Alt+C"));
        this->Color->setIcon(QIcon(":/image/Color (5).png"));
        connect(Color,&QAction::triggered,this,&JSB::color_slot);
    
        this->Font = ForMat->addAction("字体");
        this->Font->setShortcut(tr("Ctrl+Alt+F"));
        this->Font->setIcon(QIcon(":/image/Font (2).png"));
        connect(Font,&QAction::triggered,this,&JSB::font_slot);
    
        //创建帮助菜单
        this->Help = bar->addMenu("帮助(&H)");
        this->About = Help->addAction("关于");
        this->About->setShortcut(tr("Ctrl+Shift+A"));
        this->About->setIcon(QIcon(":/image/About.png"));
        this->Web = Help->addAction("官网");
        this->Web->setShortcut(tr("Ctrl+Shift+W"));
        this->Web->setIcon(QIcon(":/image/web.png"));
        connect(Web,&QAction::triggered,this,&JSB::web_slot);
    
        this->IPv = Help->addAction("版本");
        this->IPv->setShortcut(tr("Ctrl+Shift+I"));
        this->IPv->setIcon(QIcon(":/image/Window.png"));
         connect(IPv,&QAction::triggered,this,&JSB::ipv_slot);
        //工具栏
        this->ToolBar = new QToolBar;
        this->ToolBar->setParent(this);
        addToolBar(ToolBar);
        this->ToolBar->setMovable(false);
        this->ToolBar->addAction(Open);
        this->ToolBar->addAction(Exit);
    
        this->ToolBar->addSeparator();
        this->ToolBar->addAction(Redo);
        this->ToolBar->addAction(Undo);
    
        this->ToolBar->addSeparator();
        this->ToolBar->addAction(Color);
        this->ToolBar->addAction(Font);
    
        this->ToolBar->addSeparator();
        this->ToolBar->addAction(Web);
        this->ToolBar->addAction(IPv);
    
        //状态栏
        this->staBar = statusBar();
        staBar->setParent(this);
        setStatusBar(staBar);
    
        //创建标签
        QLabel *label = new QLabel("UTF-8",this);
    
        staBar->addWidget(label);
    
        //文本
        this->edit = new QTextEdit(this);
        setCentralWidget(edit);
    
        //设置默认字体
        QFont font;
        font.setFamily("楷体");
        font.setPixelSize(20);
        font.setBold(true);
        this->edit->setFont(font);
    
    
        //设置默认颜色
        QColor color;
        color.setRed(100);
    
        this->edit->setTextColor(color);
    
    }
    
    JSB::~JSB()
    {
    }
     void JSB::closeEvent(QCloseEvent *event)
     {
    
         if(this->edit->document()->isModified())
         {
             QMessageBox msgBox;
             msgBox.setText("内容以改变.");
             msgBox.setInformativeText("是否保存新内容?");
             msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
             msgBox.setButtonText(QMessageBox::Save,"保存");
             msgBox.setButtonText(QMessageBox::Discard,"取消");
             msgBox.setButtonText(QMessageBox::Cancel,"不保存");
             msgBox.setDefaultButton(QMessageBox::Save);
            int ret = msgBox.exec();
             switch (ret) {
                case QMessageBox::Save:
                    // Save was clicked
                    this->save_slot();
                    break;
                case QMessageBox::Discard:
                    // Don't Save was clicked
                    event->ignore();
                    break;
                case QMessageBox::Cancel:
                    // Cancel was clicked
                    event->accept();
                    break;
                default:
                    // should never be reached
                    break;
             }
       }
         else {
             event->accept();
         }
     }
    void JSB::open_slot()
    {
       QString filename = QFileDialog::getOpenFileName(
                    this,"打开",".","*.*");
    
       QFile *file = new QFile;
       //设置打开的路径
       file->setFileName(filename);
       //只读
       bool ok = file->open(QIODevice::ReadOnly);
    
       if(ok)
       {
           //问件指针关联文本流
           QTextStream stream(file);
           //UTF-8
           stream.setCodec("UTF-8");
           this->edit->setText(stream.readAll());
    
           //关闭文件
           file->close();
           delete file;
    
           //获取当前光标的位置
           QTextCursor cursor = this->edit->textCursor();
           //将光标移动到末尾
           cursor.movePosition(QTextCursor::End);
           //将设置好的光标放到edit中
           this->edit->setTextCursor(cursor);
       }
    
    }
    void JSB::new_slot()
    {
        JSB *w = new JSB;
        w->show();
        this->close();
    }
    void JSB::save_slot()
    {
        QString filename = QFileDialog::getSaveFileName(
                     this,"保存",".","*.*");
    
        QFile *file = new QFile;
        //设置打开的路径
        file->setFileName(filename);
        //只写
        bool ok = file->open(QIODevice::WriteOnly);
    
        if(ok)
        {
            //文件指针关联文本流
            QTextStream stream(file);
            //UTF-8
            stream.setCodec("UTF-8");
    
            //QString str = this->edit->toPlainText();
             QString str = this->edit->toHtml();
            stream<<str;
            //关闭文件
            file->close();
            delete file;
    
            //清空edit
            this->edit->clear();
        }
    }
    void JSB::exit_slot()
    {
        this->close();
    }
    
    void JSB::copy_slot()
    {
        this->edit->copy();
    }
    void JSB::patse_slot()
    {
        this->edit->paste();
    }
    void JSB::cut_slot()
    {
        this->edit->cut();
    }
    void JSB::redo_slot()
    {
        this->edit->redo();
    }
    void JSB::undo_slot()
    {
        this->edit->undo();
    }
    
    void JSB::time_slot()
    {
        QDateTime Time = QDateTime::currentDateTime();
        QString str = Time.toString("yyyy-MM-dd hh:mm:ss");
    
        this->edit->append(str);
    }
    
    void JSB::color_slot()
    {
        QColor color = QColorDialog::getColor(Qt::red,this,"颜色");
    
        if(color.isValid())
        {
            this->edit->setTextColor(color);
        }
    }
    void JSB::font_slot()
    {
        bool ok;
        QFont font = QFontDialog::getFont(&ok,QFont("楷体",18,5,true),this,"字体");
        if(ok)
        {
            this->edit->setFont(font);
        }
    }
    
    void JSB::about_slot()
    {
    
    }
    void JSB::web_slot()
    {
        QDesktopServices::openUrl(QUrl("https://blog.csdn.net/m0_72847002?spm=1010.2135.3001.5421"));
    }
    void JSB::ipv_slot()
    {
        IPV * ipv = new IPV;
        ipv->show();
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335

    登录功能的头文件

    #ifndef LOGIN_H
    #define LOGIN_H
    
    #include 
    
    namespace Ui {
    class Login;
    }
    
    class Login : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Login(QWidget *parent = nullptr);
        ~Login();
    public slots:
        void login();
    
    private:
        Ui::Login *ui;
    };
    
    #endif // LOGIN_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    登录功能的函数实现

    #include "login.h"
    #include "ui_login.h"
    #include "jsb.h"
    Login::Login(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Login)
    {
        ui->setupUi(this);
    
        this->setWindowTitle("登录");
        this->setWindowIcon(QIcon(":/image/Jsb3.png"));
    
        connect(ui->dl,&QPushButton::clicked,this,&Login::login);
    
    }
    
    Login::~Login()
    {
        delete ui;
    }
    
    void Login::login()
    {
    
    
        if(ui->id->text() == "1" && ui->passwd->text()=="1")
        {
            JSB *jsb = new JSB;
            jsb->show();
            this->close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    登录功能的UI绘制

    在这里插入图片描述

    版本展示头文件

    #ifndef IPV_H
    #define IPV_H
    
    #include 
    #include 
    namespace Ui {
    class IPV;
    }
    
    class IPV : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit IPV(QWidget *parent = nullptr);
        ~IPV();
    
    private:
        Ui::IPV *ui;
    };
    
    #endif // IPV_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    版本号功能实现

    #include "ipv.h"
    #include "ui_ipv.h"
    
    IPV::IPV(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::IPV)
    {
        ui->setupUi(this);
    
        QMovie *movie = new QMovie(":/image/q.gif");
        ui->label->setMovie(movie);
        movie->start();
    
    }
    
    IPV::~IPV()
    {
        delete ui;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    版本号界面UI绘制

    在这里插入图片描述

    效果图

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    我开始搞研发管理和项目管理了,发现最难管理的还是人
    css基础
    systemui屏蔽通知栏
    「Spring Boot 系列」08. Spring Boot整合MyBatis
    C# 优雅的处理TCP数据(心跳,超时,粘包断包,SSL加密 ,数据处理等)
    C#创建AutoMapper的映射配置
    对标金九银十!各大厂面试八股文,三天背完分分钟斩下offer!
    有交互作用的正交实验设计及数据分析
    专访乐凯撒CTO黄道泳:看一盒披萨背后的技术之路
    【Codeforces】 CF1685C Bring Balance
  • 原文地址:https://blog.csdn.net/m0_72847002/article/details/133046058