【C/C++学院】(19)QT版:记事本
利用主窗口类实现,QT版:记事本。
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QCloseEvent> #include <QTextEdit> #include <QMenu> #include <QMenuBar> #include <QAction> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void openfile(); void newfile(); void savefile(); void exitfile(); void copyfile(); void cutfile(); void pastefile(); private: void createMenus(); void createActions(); void closeEvent(QCloseEvent *event); QTextEdit *text; QMenu *fileMenu; QMenu *editMenu; QAction *openFileAction; QAction *newFileAction; QAction *saveFileAction; QAction *exitFileAction; QAction *copyFileAction; QAction *cutFileAction; QAction *pasteFileAction; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include <QMessageBox> #include <QFileDialog> #include <QTextStream> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { setWindowTitle("记事本"); text = new QTextEdit(this); this->setCentralWidget(text); text->setFontPointSize(20);//设置默认字体大小 createActions(); createMenus(); } MainWindow::~MainWindow() { } void MainWindow::openfile() { QString filename = QFileDialog::getOpenFileName(this, tr("打开文件")); if (filename.isEmpty())//如果没有选择要打开的文件名,函数退出 { return ; } QFile data(filename); if (data.open(QFile::ReadOnly))//以只读方式打开文件 { text->clear();//首先将text空间内容清空 QTextStream stream(&data); while (!stream.atEnd())//循环读取文件按,直到文件尾结束循环 { text->append(stream.readLine());//在text控件内容尾部追加从文件中读取的内容 } data.close();//读完文件后关闭文件 }else { QMessageBox::critical(this, tr("错误"), data.errorString()); } } void MainWindow::newfile() { text->clear(); } void MainWindow::savefile() { QString filename = QFileDialog::getSaveFileName(this, tr("保存文件")); if (filename.isEmpty())//如果没有选择文件,函数退出 { return ; } QFile data(filename); if (data.open(QFile::WriteOnly | QFile::Truncate))//以写方式打开文件,如果关键存在,则是追加模式 { QTextStream stream(&data); stream<<text->toPlainText();//将text控件内容转化为QString后,写入文件 data.close(); } else { QMessageBox::critical(this, tr("错误"), data.errorString()); } } void MainWindow::exitfile() { close(); //exit(0);//直接退出 } void MainWindow::copyfile() { text->copy(); } void MainWindow::cutfile() { text->cut(); } void MainWindow::pastefile() { text->paste(); } void MainWindow::createMenus() { fileMenu = menuBar()->addMenu(tr("文件"));//建立文件菜单 fileMenu->addAction(openFileAction); fileMenu->addAction(newFileAction); fileMenu->addAction(saveFileAction); fileMenu->addSeparator();//加入一个分隔符 fileMenu->addAction(exitFileAction); editMenu = menuBar()->addMenu(tr("编辑")); editMenu->addAction(copyFileAction); editMenu->addAction(cutFileAction); editMenu->addAction(pasteFileAction); } void MainWindow::createActions() { openFileAction = new QAction(tr("打开"), this); openFileAction->setShortcut(tr("Ctrl+o")); connect(openFileAction, SIGNAL(triggered()), this, SLOT(openfile())); newFileAction = new QAction(tr("新建"), this); newFileAction->setShortcut(tr("Ctrl+N")); connect(newFileAction, SIGNAL(triggered()), this, SLOT(newfile())); saveFileAction = new QAction(tr("保存"), this); saveFileAction->setShortcut(tr("Ctrl+s")); connect(saveFileAction, SIGNAL(triggered()), this, SLOT(savefile())); exitFileAction = new QAction(tr("退出"), this); exitFileAction->setShortcut(tr("Ctrl+q")); connect(exitFileAction, SIGNAL(triggered()), this, SLOT(exitfile())); copyFileAction = new QAction(tr("复制"), this); copyFileAction->setShortcut(tr("Ctrl+c")); connect(copyFileAction, SIGNAL(triggered()), this, SLOT(copyfile())); cutFileAction = new QAction(tr("剪切"), this); cutFileAction->setShortcut(tr("Ctrl+x")); connect(cutFileAction, SIGNAL(triggered()), this, SLOT(cutfile())); pasteFileAction = new QAction(tr("粘贴"), this); pasteFileAction->setShortcut(tr("Ctrl+v")); connect(pasteFileAction, SIGNAL(triggered()), this, SLOT(pastefile())); } void MainWindow::closeEvent(QCloseEvent *event) { QMessageBox::StandardButton button; button = QMessageBox::question(this, tr("退出程序"), QString(tr("是否退出")), QMessageBox::Yes | QMessageBox::No); if (button == QMessageBox::Yes) { event->accept();//接受退出信号,程序退出 }else { event->ignore();//忽略退出信息,程序继续运行 } }
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.resize(600, 400); w.show(); return a.exec(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。