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
}}
如果你对我们的产品感兴趣或者有任何疑问,欢迎前往慧都网获取更多资料