Qt是一種跨平台的原生開發架構,面向所有主要的作業系統,包括Linux、Windows、Mac、Symbian和Maemo。本文介紹的是在Windows下用qt編程實現的簡易記事本,有興趣學習qt編程的可以看看。本人也是剛接觸qt編程,覺得qt相對其他語言來說學起來還是比較容易的。 這是我用Qt Creator實現的簡易記事本:QQ迷你版 本網聊天軟體詳見Qt編程—我的QQ(區域網路可用)—含原始碼 上面介面是用Qt Designer實現的 是不是在actionNew(建立)前面有出現小表徵圖,在往工具列裡移動,就出現上述效果了。 這隻是實現了一部分功能,大家可以看一下 mainwindow.h檔案
<span style="font-size:14px;">#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QFileDialog>#include <QTextStream>#include <QMessageBox>#include <QDialog>#include <QLineEdit>#include <QPushButton>#include <QVBoxLayout>#include <QHBoxLayout>namespace Ui { class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void changeTitle(const QString &filename); void openfile(const QString &filename); void savefile(const QString &filename);private: Ui::MainWindow *ui; QString curFile; QLineEdit *find_textLineEdit;private slots: void show_findText(); void on_action_2_triggered();//尋找 void on_plan_textEdit_copyAvailable(bool b); void on_plan_textEdit_textChanged(); void on_action_U_triggered();//撤銷 void on_action_P_triggered();//粘貼 void on_action_C_triggered();//複製 void on_action_T_triggered();//剪下 void on_actionExit_triggered();//退出 bool on_actionSave_triggered();//儲存 bool on_actionSave_As_triggered();//另存新檔 void on_actionOpen_triggered();//開啟 void on_actionNew_triggered();//建立};#endif // MAINWINDOW_H</span>
main.h檔案
<span style="font-size:14px;">#include <QtGui/QApplication>#include <QTextCodec>#include "mainwindow.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312")); QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312")); MainWindow w; w.show(); return a.exec();}</span>
mainwindow.cpp
<span style="font-size:14px;">#include "mainwindow.h"#include "ui_mainwindow.h"#include<QDebug>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); this->changeTitle(""); this->setCentralWidget(ui->plan_textEdit); //設定action的屬性 ui->action_T->setEnabled(false); ui->action_C->setEnabled(false); //設定介面表徵圖 this->setWindowIcon(QIcon(":/images/editor.png"));}MainWindow::~MainWindow(){ delete ui;}void MainWindow::savefile(const QString &filename){ QFile file(filename); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){ QMessageBox::warning(this, "Save", tr("Save , error ?") .arg(filename) .arg(file.errorString())); return ; } QTextStream out(&file); out << ui->plan_textEdit->toPlainText(); file.close();}void MainWindow::openfile(const QString &filename){ QFile file(filename); if(file.open(QIODevice::ReadOnly | QIODevice::Text)){ QMessageBox::warning(this, "Open", tr("Save , error ?") .arg(filename) .arg(file.errorString())); return ; } QTextStream in(&file); QString s=in.readAll(); ui->plan_textEdit->setPlainText(s);}void MainWindow::changeTitle(const QString &filename){ qDebug()<<"***"<<filename; if(filename.isEmpty()){ this->curFile = ""; this->setWindowTitle(tr("無標題.txt[*]--記事本")); } else{ this->curFile = filename; QFileInfo fi(filename); QString name=fi.fileName(); this->setWindowTitle(tr("%1[*]--記事本").arg(name)); qDebug()<<"===="<<name; } this->setWindowModified(false); ui->plan_textEdit->document()->setModified(false);}void MainWindow::on_actionNew_triggered()//建立{ QTextDocument *document=ui->plan_textEdit->document(); if(!document->isModified()){ ui->plan_textEdit->setPlainText(""); this->changeTitle(""); return ; } int ret=QMessageBox::question(this, "Save?", "disSave, Save", QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); switch(ret) { case QMessageBox::Cancel: break; case QMessageBox::Discard: ui->plan_textEdit->setPlainText(""); this->changeTitle(""); break; case QMessageBox::Save: if(!on_actionSave_triggered()) return ; ui->plan_textEdit->setPlainText(""); this->changeTitle(""); break; }}void MainWindow::on_actionOpen_triggered()//開啟{ QTextDocument *document=ui->plan_textEdit->document(); if(document->isModified()){ int ret=QMessageBox::question(this, "Save?", "disSave, Save", QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); if(ret==QMessageBox::Cancel){ return ; } else if(ret==QMessageBox::Save){ } } //沒修改 正常儲存 放棄儲存 QString filename=QFileDialog::getOpenFileName(this); if(filename.isEmpty()) return ; this->openfile(filename); this->changeTitle(filename);}bool MainWindow::on_actionSave_As_triggered()//另存新檔{ QString fileName=QFileDialog::getSaveFileName(this,tr("Save File")); if(fileName.isEmpty()) return false; this->savefile(fileName); this->changeTitle(fileName); return true;}bool MainWindow::on_actionSave_triggered()//儲存{ if(this->curFile.isEmpty()) // qDebug()<<"+++"; return on_actionSave_As_triggered(); else { savefile(this->curFile); this->setWindowModified(false); } return true;}void MainWindow::on_actionExit_triggered()//退出{ this->close();}void MainWindow::on_action_T_triggered()//剪下{ this->ui->plan_textEdit->cut();}void MainWindow::on_action_C_triggered()//複製{ this->ui->plan_textEdit->copy();}void MainWindow::on_action_P_triggered()//粘貼{ this->ui->plan_textEdit->paste();}void MainWindow::on_action_U_triggered()//撤銷{ this->ui->plan_textEdit->undo();}void MainWindow::on_plan_textEdit_textChanged(){ //qDebug()<<"niaho"; this->setWindowModified(true);}void MainWindow::on_plan_textEdit_copyAvailable(bool b){ ui->action_T->setEnabled(b); ui->action_C->setEnabled(b);}//這部分是編碼實現的,可以看看與介面設計實現的有什麼不一樣void MainWindow::on_action_2_triggered()//尋找{ QDialog *findDlg=new QDialog(this); findDlg->setWindowTitle(tr("Find!")); find_textLineEdit=new QLineEdit(findDlg); QPushButton *find_Bth=new QPushButton(tr("尋找下一個"),findDlg); QHBoxLayout *layout=new QHBoxLayout(findDlg); layout->addWidget(find_textLineEdit); layout->addWidget(find_Bth); findDlg->show(); connect(find_Bth,SIGNAL(clicked()),this,SLOT(show_findText()));}void MainWindow::show_findText(){ QString findText=find_textLineEdit->text(); if(!ui->plan_textEdit->find(findText,QTextDocument::FindBackward)) { QMessageBox::warning(this,tr("尋找"),tr("找不到 %1").arg(findText)); }}</span>
其實還有一部分功能沒實現,有興趣的話可以關注我下一篇文章。