轉帖|使用教程|編輯:鮑佳佳|2020-07-29 10:28:36.860|閱讀 371 次
概述:此為一個系列的關于qt小部件的入門教程,在本主題中,我們通過使用C ++和Qt Widgets模塊實現一個簡單的記事本應用程序來教授基本的Qt知識。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Qt是目前最先進、最完整的跨平臺C++開發工具。它不僅完全實現了一次編寫,所有平臺無差別運行,更提供了幾乎所有開發過程中需要用到的工具。如今,Qt已被運用于超過70個行業、數千家企業,支持數百萬設備及應用。
向導為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 }}
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: