首页 > 代码库 > qt文本编辑器

qt文本编辑器

示例代码:

mainwindow.cpp

#include "mainwindow.h"#include "ui_mainwindow.h"#include <QtGui>MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),    ui(new Ui::MainWindow){    ui->setupUi(this);    isSaved =false;    curFile = tr("未命名.txt");    setWindowTitle(curFile);    init_statusBar();}MainWindow::~MainWindow(){    delete ui;}void MainWindow::do_file_New(){    do_file_SaveOrNot();    isSaved = false;    curFile = tr("未命名.txt");    setWindowTitle(curFile);    ui->textEdit->clear();//清空文本编辑器    ui->textEdit->setVisible(true);//文本编辑器可见}void MainWindow::do_file_SaveOrNot(){    if(ui->textEdit->document()->isModified()) //如果文件被更改过,弹出保存对话框    {        QMessageBox box;        box.setWindowTitle(tr("警告"));        box.setIcon(QMessageBox::Warning);        box.setText(curFile+tr("尚未保存,是否保存?"));        box.setStandardButtons(QMessageBox::Yes|QMessageBox::No);        if(box.exec() == QMessageBox::Yes)            do_file_Save();    }}void MainWindow::do_file_Save(){    if(isSaved)    {        saveFile(curFile);    }    else    {        do_file_SaveAs();    }}void MainWindow::do_file_SaveAs(){  //  QString fileName = QFileDialog::getSaveFileName(this,tr("另存为"),curFile);      QString fileName = QFileDialog::getSaveFileName(this,tr("另存为"),curFile);    if(!fileName.isEmpty())    {        saveFile(fileName);    }}bool MainWindow::saveFile(const QString &fileName){    QFile file(fileName);    if(!file.open(QFile::WriteOnly|QFile::Text))    {        QMessageBox::warning(this,tr("保存文件"),        tr("无法保存文件 %1:\n %2").arg(fileName)        .arg(file.errorString()));        return false;    }    QTextStream out(&file);    out<<ui->textEdit->toPlainText();    isSaved = true;    curFile = QFileInfo(fileName).canonicalFilePath();    setWindowTitle(curFile);    return true;}void MainWindow::do_file_Open(){    do_file_SaveOrNot();    QString fileName = QFileDialog::getOpenFileName(this);    if(!fileName.isEmpty())    {        do_file_Load(fileName);    }    ui->textEdit->setVisible(true);//文本编辑器可见}bool MainWindow::do_file_Load(const QString &fileName){    QFile file(fileName);    if(!file.open(QFile::ReadOnly|QFile::Text))    {        QMessageBox::warning(this,tr("读取文件"),tr("无法读取文件 %1:\n%2").arg(fileName).arg(file.errorString()));        return false;    }    QTextStream in(&file);    ui->textEdit->setText(in.readAll());    curFile = QFileInfo(fileName).canonicalFilePath();    setWindowTitle(curFile);    return true;}void MainWindow::on_action_N_triggered(){    do_file_New();}void MainWindow::on_action_S_triggered(){    do_file_Save();}void MainWindow::on_action_2_triggered(){    do_file_SaveAs();}void MainWindow::on_action_O_triggered(){    do_file_Open();}void MainWindow::on_action_C_triggered(){    do_file_SaveOrNot();    ui->textEdit->setVisible(false);}void MainWindow::on_action_X_triggered(){    on_action_C_triggered();    qApp->quit();}void MainWindow::on_action_Z_triggered(){    ui->textEdit->undo();}void MainWindow::on_action_X_2_triggered(){    ui->textEdit->cut();}void MainWindow::on_action_C_2_triggered(){    ui->textEdit->copy();}void MainWindow::on_action_V_triggered(){    ui->textEdit->paste();}void MainWindow::on_action_F_triggered(){    QDialog *findDlg = new QDialog(this);    findDlg ->setWindowTitle(tr("查找"));    find_textLineEdit = new QLineEdit(findDlg);    QPushButton *find_Btn = new QPushButton(tr("查找下一个"),findDlg);    QVBoxLayout *layout = new  QVBoxLayout(findDlg);    layout ->addWidget(find_textLineEdit);    layout->addWidget(find_Btn);    findDlg ->show();    //显示对话框    connect(find_Btn,SIGNAL(clicked()),this,SLOT(show_findText()));}void MainWindow::show_findText(){    QString findText = find_textLineEdit->text();    ui->textEdit->find(findText,QTextDocument::FindBackward);    if(!ui->textEdit->find(findText,QTextDocument::FindBackward))    {        QMessageBox::warning(this,tr("查找"),tr("找不到 %1").arg(findText));    }}void MainWindow::init_statusBar(){    QStatusBar*bar = ui->statusBar;    first_statusLabel = new QLabel;    first_statusLabel -> setMinimumSize(150,20);    first_statusLabel -> setFrameShape(QFrame::WinPanel);    first_statusLabel -> setFrameShadow(QFrame::Sunken);    second_statusLabel = new QLabel;    second_statusLabel->setMinimumSize(150,20);    second_statusLabel ->setFrameShape(QFrame::WinPanel);    second_statusLabel->setFrameShadow(QFrame::Sunken);    bar ->addWidget(first_statusLabel);    bar->addWidget(second_statusLabel);    first_statusLabel->setText(tr("欢迎使用文本编辑器"));    second_statusLabel->setText("yafeilinux 制作!");}