Qt学习(二)------实例mynotepad制作
2016-06-24 17:05
507 查看
一、Qt学习
最近在看高老师的视频教程,自己跟着做了一个txt编辑器,功能很简单,能实现编辑,保存,撤销,复制粘贴等基础功能;关于那边有一些特效二、关键代码
1、mainwindow.cpp#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); this->setWindowTitle("Untitled.txt-------------------yanqiang"); this->setWindowIcon(QIcon(":new/icon/txt")); //File Menu QObject::connect(ui->newAction,SIGNAL(triggered()),this ,SLOT(newFileSlot())); QObject::connect(ui->openAction,SIGNAL(triggered()),this,SLOT(openFileSlot())); QObject::connect(ui->saveAction,SIGNAL(triggered()),this,SLOT(saveFileSlot())); QObject::connect(ui->exitAction,SIGNAL(triggered()),this,SLOT(close())); //Edit Menu QObject::connect(ui->undoAction,SIGNAL(triggered()),ui->textEdit,SLOT(undo())); QObject::connect(ui->redoAction,SIGNAL(triggered()),ui->textEdit,SLOT(redo())); QObject::connect(ui->copyAction,SIGNAL(triggered()),ui->textEdit,SLOT(copy())); QObject::connect(ui->cutAction,SIGNAL(triggered()),ui->textEdit,SLOT(cut())); QObject::connect(ui->pasteAction,SIGNAL(triggered()),ui->textEdit,SLOT(paste())); QObject::connect(ui->selectAllAction,SIGNAL(triggered()),ui->textEdit,SLOT(selectAll())); QObject::connect(ui->fontAction,SIGNAL(triggered()),this,SLOT(setFontSlot())); QObject::connect(ui->colorAction,SIGNAL(triggered()),this,SLOT(setColorSlot())); QObject::connect(ui->datetimeAction,SIGNAL(triggered()),this,SLOT(currentDateTimeSlot())); //Help Menu QObject::connect(ui->aboutQtAction,SIGNAL(triggered()),qApp,SLOT(aboutQt())); QObject::connect(ui->aboutWebAction,SIGNAL(triggered()),this,SLOT(aboutWebSiteSlot())); QObject::connect(ui->aboutSoftWareAction,SIGNAL(triggered()),this,SLOT(aboutSoftWareSlot())); } void MainWindow::closeEvent(QCloseEvent *event){ //忽略事件 //event->ignore(); //event->accept(); if(ui->textEdit->document()->isModified()){ QMessageBox msgBox; //显示的提示信息 msgBox.setText("文本已经变更"); msgBox.setInformativeText("您是否要保存文件"); msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); msgBox.setDefaultButton(QMessageBox::Save); int ret = msgBox.exec(); switch (ret) { case QMessageBox::Save: // Save was clicked this->saveFileSlot(); break; case QMessageBox::Discard: // Don't Save was clicked event->accept(); break; case QMessageBox::Cancel: // Cancel was clicked event->ignore(); break; default: // should never be reached break; } } else{ event->accept(); } } MainWindow::~MainWindow() { delete ui; } void MainWindow::newFileSlot(){ //Content is modified if(ui->textEdit->document()->isModified()) { qDebug()<<"File is modified"; } else{ //qDebug()<<"File is not modified"; ui->textEdit->clear(); this->setWindowTitle("Untitled.txt------------yanqinag"); } } void MainWindow::openFileSlot(){ //Get File Name QString fileName=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath()); //qDebug()<<"fileName is:"<<fileName; if(fileName.isEmpty()){ QMessageBox::information(this,"Error Message","Select A File"); return; } QFile *file=new QFile; file->setFileName(fileName); bool ok=file->open(QIODevice::ReadOnly); if(ok){ //File With QTextStream QTextStream in(file); ui->textEdit->setText(in.readAll());//get content from file file->close(); delete file; } else { QMessageBox::information(this,"Error Message","Fail To Open File"+file->errorString()); return; } } //save file void MainWindow::saveFileSlot(){ if(saveFileName.isEmpty()){ this->saveAsFileSlot(); } else{ QFile *file=new QFile; file->setFileName(saveFileName); bool ok=file->open(QIODevice::WriteOnly); if(ok){ QTextStream out(file); out<<ui->textEdit->toPlainText();//这里去除textEdit的纯文本 file->close(); delete file; } } } //save as void MainWindow::saveAsFileSlot(){ //使用getSaveFileName方法 QString saveFileName=QFileDialog::getSaveFileName(this,"Save File",QDir::currentPath()); if(saveFileName.isEmpty()){ QMessageBox::information(this,"Error Message","Select A File"); return; } QFile *file=new QFile; file->setFileName(saveFileName); bool ok=file->open(QIODevice::WriteOnly); if(ok){ QTextStream out(file); out<<ui->textEdit->toPlainText();//这里去除textEdit的纯文本 file->close(); this->setWindowTitle(saveFileName+"-----------------notepad"); delete file; } else{ QMessageBox::information(this,"Error Message","Save File Error"); return; } } void MainWindow::setFontSlot(){ //get selected font /* bool ok; QFont font = QFontDialog::getFont( &ok, QFont("Helvetica [Cronyx]", 10), this); if (ok) { // the user clicked OK and font is set to the font the user selected } else { // the user canceled the dialog; font is set to the initial // value, in this case Helvetica [Cronyx], 10 } */ bool ok; QFont font=QFontDialog::getFont(&ok,this); if(ok){ ui->textEdit->setFont(font); } else{ QMessageBox::information(this,"Error Message","Error Set Font"); return; } } void MainWindow::setColorSlot(){ /* const QColorDialog::ColorDialogOptions options = QFlag(colorDialogOptionsWidget->value()); const QColor color = QColorDialog::getColor(Qt::green, this, "Select Color", options); if (color.isValid()) { colorLabel->setText(color.name()); colorLabel->setPalette(QPalette(color)); colorLabel->setAutoFillBackground(true); } */ QColor color=QColorDialog::getColor(Qt::red,this); if(color.isValid()){ ui->textEdit->setTextColor(color); } else{ QMessageBox::information(this,"Error Message","Color is invalid"); return; } } void MainWindow::currentDateTimeSlot(){ QDateTime current=QDateTime::currentDateTime(); QString time=current.toString("yyyy-M-dd hh:mm:ss"); ui->textEdit->append(time); } void MainWindow::aboutWebSiteSlot(){ QDesktopServices::openUrl(QUrl("http://wwww.baidu.com")); } void MainWindow::aboutSoftWareSlot(){ //创建about对象 about *dialog=new about; dialog->show();//非模态对话框 //dialog.exec() modal dailog }
2、main.cpp
#include "mainwindow.h" #include <QApplication> #include <QPixmap> #include <QSplashScreen> //延时 void sleep(unsigned int msec) { QTime dieTime = QTime::currentTime().addMSecs(msec); while( QTime::currentTime() < dieTime ) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); } int main(int argc, char *argv[]) { QApplication app(argc, argv); //屏幕启动画面 QPixmap pixmap("screen.png"); QSplashScreen splash(pixmap); splash.show(); sleep(5000); // for(long index=0;index<=4590500000;index++); MainWindow w; w.show(); splash.finish(&w); return app.exec(); }
3、about.cpp
#include "about.h" #include "ui_about.h" about::about(QWidget *parent) : QDialog(parent), ui(new Ui::about) { ui->setupUi(this); this->setWindowIcon(QIcon(":new/icon/txt")); /* QLabel label; QMovie *movie = new QMovie("animations/fire.gif"); label.setMovie(movie); movie->start(); */ this->movie=new QMovie("mynotepad.gif"); ui->movieLabel->setMovie(movie); movie->start(); QObject::connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(stopMovieSlot())); QObject::connect(ui->startButton,SIGNAL(clicked()),this,SLOT(startMovieSlot())); } about::~about() { delete ui; } void about::startMovieSlot(){ this->movie->start();//开始动画播放 } void about::stopMovieSlot(){ this->movie->stop();//停止动画播放 }
三、运行效果图
四、下面是源码下载链接
http://download.csdn.net/detail/yanqianglifei/9558722相关文章推荐
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- Qt 5.6更新至RC版,最终版本近在咫尺
- 交换机升级排障实例
- sql2008启动代理未将对象应用到实例解决方案
- Ajax教程实例详解
- 初识JQuery 实例一(first)
- JQuery入门基础小实例(1)
- Jquery具体实例介绍AJAX何时用,AJAX应该在什么地方用
- 一个小助手批处理实例代码
- Qt定时器和随机数详解
- XStream使用方法总结附实例代码
- asp.net得到本机数据库实例的两种方法代码
- C++多继承同名隐藏实例详细介绍
- PHP入门学习的几个不错的实例代码
- JSP发送邮件实例
- Qt实现图片移动实例(图文教程)
- C#中的IEnumerable简介及简单实现实例
- jQuery Ajax 实例详解 ($.ajax、$.post、$.get)
- PHP中的流(streams)浅析