彩票走势图

Qt小部件基础教程(四):记事本源文件项目代码

转帖|使用教程|编辑:鲍佳佳|2020-07-29 10:28:36.860|阅读 300 次

概述:此为一个系列的关于qt小部件的入门教程,在本主题中,我们通过使用C ++和Qt Widgets模块实现一个简单的记事本应用程序来教授基本的Qt知识。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:

Qt是目前最先进、最完整的跨平台C++开发工具。它不仅完全实现了一次编写,所有平台无差别运行,更提供了几乎所有开发过程中需要用到的工具。如今,Qt已被运用于超过70个行业、数千家企业,支持数百万设备及应用。

点击下载Qt最新试用版

向导为Notepad类生成的源文件如下所示:

#include "notepad.h" "notepad.h"
#include "ui_notepad.h" "ui_notepad.h"

NotepadNotepad::Notepad((QWidget *parent) :
    ) :
    QMainWindow(parent)parent),
    ui((new UiUi::Notepad))
{{
    ui->setupUi((this);
    
    this->setCentralWidget(ui(ui->textEdit););

    connect(ui(ui->actionNew, &QAction::triggered, this, &Notepad::newDocument););
    connect(ui(ui->actionOpen, &QAction::triggered, this, &Notepad::open););
    connect(ui(ui->actionSave, &QAction::triggered, this, &Notepad::save););
    connect(ui(ui->actionSave_as, &QAction::triggered, this, &Notepad::saveAs););
    connect(ui(ui->actionPrint, &QAction::triggered, this, &Notepad::print););
    connect(ui(ui->actionExit, &QAction::triggered, this, &Notepad::exit););
    connect(ui(ui->actionCopy, &QAction::triggered, this, &Notepad::copy););
    connect(ui(ui->actionCut, &QAction::triggered, this, &Notepad::cut););
    connect(ui(ui->actionPaste, &QAction::triggered, this, &Notepad::paste););
    connect(ui(ui->actionUndo, &QAction::triggered, this, &Notepad::undo););
    connect(ui(ui->actionRedo, &QAction::triggered, this, &Notepad::redo););
    connect(ui(ui->actionFont, &QAction::triggered, this, &Notepad::selectFont););
    connect(ui(ui->actionBold, &QAction::triggered, this, &Notepad::setFontBold););
    connect(ui(ui->actionUnderline, &QAction::triggered, this, &Notepad::setFontUnderline););
    connect(ui(ui->actionItalic, &QAction::triggered, this, &Notepad::setFontItalic););
    connect(ui(ui->actionAbout, &QAction::triggered, this, &Notepad::about););

// Disable menu actions for unavailable features
#if !defined(QT_PRINTSUPPORT_LIB) || !QT_CONFIG(printer)
    ui->actionPrint->setEnabled((false);

#endif

#if !QT_CONFIG(clipboard)
    ui->actionCut->setEnabled((false);
    ui->actionCopy->setEnabled((false);
    ui->actionPaste->setEnabled((false);

#endif
}}

NotepadNotepad::~Notepad()()
{{
    delete ui;;
}}

void NotepadNotepad::newDocument()()
{{
    currentFile.clear();();
    ui->textEdit->setText((QString());
}}

void NotepadNotepad::open()()
{{
    QString fileName = QFileDialog::getOpenFileName((this, "Open the file");
    
    QFile file(fileName);(fileName);
    currentFile = fileName;
    ;
    if ((!file.open((QIODevice::ReadOnly  | QFile::Text)) {
        )) {
        QMessageBox::warning((this, "Warning", "Cannot open file: " + file.errorString());
        ());
        return;
    }}
    setWindowTitle(fileName);(fileName);
    QTextStream in(in(&file);
    );
    QString text = inin.readAll();();
    ui->textEdit->setText(text);(text);
    file.close();();
}}

void NotepadNotepad::save()()
{{
    QString fileName;
    ;
    // If we don't have a filename from before, get one.
    if (currentFile(currentFile.isEmpty()) {()) {
        fileName = QFileDialog::getSaveFileName((this, "Save");
        currentFile = fileName;;
    } } else {{
        fileName = currentFile;;
    }}
    QFile file(fileName);
    (fileName);
    if ((!file.open((QIODevice::WriteOnly  | QFile::Text)) {
        )) {
        QMessageBox::warning((this, "Warning", "Cannot save file: " + file.errorString());
        ());
        return;
    }}
    setWindowTitle(fileName);(fileName);
    QTextStream out(out(&file);
    );
    QString text = ui->textEdit->toPlainText();();
    out out << text;; file.close();(); }} void NotepadNotepad::saveAs()() {{ QString fileName = QFileDialog::getSaveFileName((this, "Save as"); QFile file(fileName);(fileName); if ((!file.open((QFile::WriteOnly | QFile::Text)) { )) { QMessageBox::warning((this, "Warning", "Cannot save file: " + file.errorString()); ()); return; }} currentFile = fileName;; setWindowTitle(fileName);(fileName); QTextStream out(out(&file); ); QString text = ui->textEdit->toPlainText();();
    out out << text;; file.close();(); }} void NotepadNotepad::print()() {{ #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer) QPrinter printDev; ; #if QT_CONFIG(printdialog) QPrintDialog dialog((&printDev, this); if (dialog(dialog.exec() () == QDialog::Rejected) ) return; #endif // QT_CONFIG(printdialog) // QT_CONFIG(printdialog) ui->textEdit->print((&printDev);
);
#endif // QT_CONFIG(printer) // QT_CONFIG(printer)
}}

void NotepadNotepad::exit()()
{{
    QCoreApplication::quit();();
}}

void NotepadNotepad::copy()()
{{
#if QT_CONFIG(clipboard)
    ui->textEdit->copy();
();
#endif
}}

void NotepadNotepad::cut()()
{{
#if QT_CONFIG(clipboard)
    ui->textEdit->cut();
();
#endif
}}

void NotepadNotepad::paste()()
{{
#if QT_CONFIG(clipboard)
    ui->textEdit->paste();
();
#endif
}}

void NotepadNotepad::undo()()
{{
     ui->textEdit->undo();();
}}

void NotepadNotepad::redo()()
{{
    ui->textEdit->redo();();
}}

void NotepadNotepad::selectFont()()
{{
    bool fontSelected;bool fontSelected;
    QFont font = QFontDialog::getFont((&fontSelected, this);
    
    if (fontSelected)(fontSelected)
        ui->textEdit->setFont(font);(font);
}}

以下各行包括由向导生成的Notepad类头文件和由uic工具生成的UI头文件:

#include "notepad.h" "notepad.h"
#include "ui_notepad.h" "ui_notepad.h"

以下行定义了Notepad构造函数:

Notepad::Notepad((QWidget *parent) :) :

以下行调用QMainWindow构造函数,该构造函数是Notepad类的基类:

    QMainWindow(parent)parent),
下面的行创建UI类实例并将其分配给ui成员:
 ui((new UiUi::Notepad))

以下行设置了UI:

    ui->setupUi((this);

在析构函数中,我们删除ui:

Notepad::~Notepad()()
{{
    delete ui;;
}}

为了使文本编辑字段占据整个屏幕,我们添加setCentralWidget到主窗口。

Notepad::Notepad((QWidget *parent) :
    ) :
    QMainWindow(parent)parent),
    ui((new UiUi::Notepad))
{{
    ui->setupUi((this);
    
    this->setCentralWidget(ui(ui->textEdit););

    connect(ui(ui->actionNew, &QAction::triggered, this, &Notepad::newDocument););
    connect(ui(ui->actionOpen, &QAction::triggered, this, &Notepad::open););
    connect(ui(ui->actionSave, &QAction::triggered, this, &Notepad::save););
    connect(ui(ui->actionSave_as, &QAction::triggered, this, &Notepad::saveAs););
    connect(ui(ui->actionPrint, &QAction::triggered, this, &Notepad::print););
    connect(ui(ui->actionExit, &QAction::triggered, this, &Notepad::exit););
    connect(ui(ui->actionCopy, &QAction::triggered, this, &Notepad::copy););
    connect(ui(ui->actionCut, &QAction::triggered, this, &Notepad::cut););
    connect(ui(ui->actionPaste, &QAction::triggered, this, &Notepad::paste););
    connect(ui(ui->actionUndo, &QAction::triggered, this, &Notepad::undo););
    connect(ui(ui->actionRedo, &QAction::triggered, this, &Notepad::redo););
    connect(ui(ui->actionFont, &QAction::triggered, this, &Notepad::selectFont););
    connect(ui(ui->actionBold, &QAction::triggered, this, &Notepad::setFontBold););
    connect(ui(ui->actionUnderline, &QAction::triggered, this, &Notepad::setFontUnderline););
    connect(ui(ui->actionItalic, &QAction::triggered, this, &Notepad::setFontItalic););
    connect(ui(ui->actionAbout, &QAction::triggered, this, &Notepad::about););

// Disable menu actions for unavailable features
#if !defined(QT_PRINTSUPPORT_LIB) || !QT_CONFIG(printer)
    ui->actionPrint->setEnabled((false);

#endif

#if !QT_CONFIG(clipboard)
    ui->actionCut->setEnabled((false);
    ui->actionCopy->setEnabled((false);
    ui->actionPaste->setEnabled((false);

#endif
}}

慧都高端UI界面开发

标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@capbkgr.cn

文章转载自:

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP