QT练习项目
程序员文章站
2024-01-10 09:42:10
...
QT练习项目之文本编辑器
mianwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDebug>
#include <QLabel>
#include <QPalette>
#include <QLineEdit>
#include <QCheckBox>
#include <QDateTime>
#include <QPushButton>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#include <QMainWindow>
#include <QRadioButton>
#include <QTextDocument>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void on_actionNew_triggered();
void on_actionSave_triggered();
void on_actionSaveAs_triggered();
void on_actionDel_triggered();
void on_actionFind_triggered();
void on_actionFindNext_triggered();
void on_actionSwap_triggered();
void on_actionTranto_triggered();
void on_actionSelectAll_triggered();
void on_actionTime_triggered();
void on_actionFont_triggered();
private:
Ui::MainWindow *ui;
private:
QString filename;
QString filename2;
QString curfile;
bool cursaveflag;
QString findtext;
QLineEdit *findEdit;
QPushButton *findBtn;
QRadioButton *findForward;
QCheckBox *findcasesensitive;
QRadioButton *findBackforward;
QTextDocument::FindFlags option;
QLabel *swapLabel;
QLabel *swapFindLabel;
QLineEdit *swapLineEdit;
QLineEdit *swapFindLineEdit;
QPushButton *swapFindBtn;
QPushButton *swapBtn;
QPushButton *swapAllBtn;
int linenum;
QLabel *trantoLabel;
QLineEdit *trantoLineEdit;
QPushButton *trantoBtn;
public:
void LoadFile(QString filename);
void SaveFile(QString filename);
void SaveAsFile(QString filename);
public slots:
void findText();
void swapfindText();
void swapText();
void swapAllText();
void trantoText();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextBlock>
#include <QFontDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPalette palette = ui->textEdit->palette();
palette.setColor(QPalette::Highlight,palette.color(QPalette::Active,QPalette::Highlight));
ui->textEdit->setPalette(palette);
cursaveflag = false;
ui->verticalLayout->setMargin(0);
connect(ui->textEdit,SIGNAL(copyAvailable(bool)),ui->actionCopy,SLOT(setEnabled(bool)));
connect(ui->textEdit,SIGNAL(copyAvailable(bool)),ui->actionCut,SLOT(setEnabled(bool)));
connect(ui->textEdit,SIGNAL(copyAvailable(bool)),ui->actionDel,SLOT(setEnabled(bool)));
connect(ui->actionCopy,SIGNAL(triggered(bool)),ui->textEdit,SLOT(copy()));
connect(ui->actionPaste,SIGNAL(triggered(bool)),ui->
textEdit,SLOT(paste()));
connect(ui->actionCut,SIGNAL(triggered(bool)),ui->textEdit,SLOT(cut()));
connect(ui->actionExit,SIGNAL(triggered(bool)),this,SLOT(close()));
connect(ui->actionAbout,SIGNAL(triggered(bool)),this,SLOT(QApplication::aboutQt()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::LoadFile(QString filename)
{
QFile file(filename);
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream textstream(&file);
while(!textstream.atEnd())
ui->textEdit->append(textstream.readLine());
}
}
void MainWindow::SaveFile(QString filename)
{
QFile file(filename);
if(file.open((QFile::WriteOnly | QFile::Text)))
{
QTextStream textstream(&file);
textstream<<ui->textEdit->toPlainText();
cursaveflag = true;
curfile = QFileInfo(filename).canonicalFilePath();
}
}
void MainWindow::findText()
{
QPalette palette = ui->textEdit->palette();
palette.setColor(QPalette::Highlight,palette.color(QPalette::Active,QPalette::Highlight));
ui->textEdit->setPalette(palette);
option = 0;
findtext = findEdit->text();
if(findcasesensitive->isChecked())
option |= QTextDocument::FindCaseSensitively ;
if(findBackforward->isChecked())
option |= QTextDocument::FindBackward;
if(!ui->textEdit->find(findtext,option))
QMessageBox::warning(this,tr("查找"),tr("找不到 %1").arg(findtext));
}
void MainWindow::swapfindText()
{
QPalette palette = ui->textEdit->palette();
palette.setColor(QPalette::Highlight,palette.color(QPalette::Active,QPalette::Highlight));
ui->textEdit->setPalette(palette);
findtext = swapFindLineEdit->text();
if(!ui->textEdit->find(findtext,QTextDocument::FindBackward))
QMessageBox::warning(this,tr("查找"),tr("找不到 %1").arg(findtext));
}
void MainWindow::swapText()
{
QTextCursor cursor = ui->textEdit->textCursor();
if(cursor.hasSelection())
cursor.insertText(swapLineEdit->text());
// qDebug()<<cursor.blockNumber()<<","<<cursor.columnNumber()<<endl;
}
void MainWindow::swapAllText()
{
QTextCursor cursor;
QTextCursor cursor1 = ui->textEdit->textCursor();
while(ui->textEdit->find(swapFindLineEdit->text(),QTextDocument::FindBackward))
{
cursor = ui->textEdit->textCursor();
if(cursor.hasSelection())
cursor.insertText(swapLineEdit->text());
}
ui->textEdit->setTextCursor(cursor1);
}
void MainWindow::trantoText()
{
bool ok;
QTextCursor cursor = ui->textEdit->textCursor();
int number = trantoLineEdit->text().toInt(&ok);
if(number > linenum)
QMessageBox::warning(this,tr("转到"),tr("超过了总行数%1").arg(linenum));
int pos = ui->textEdit->document()->findBlockByLineNumber(number).position();
cursor.setPosition(pos);
ui->textEdit->setTextCursor(cursor);
}
void MainWindow::on_actionOpen_triggered()
{
filename = QFileDialog::getOpenFileName(this);
if(!filename.isEmpty())
{
if(ui->textEdit->document()->isEmpty())
LoadFile(filename);
else
{
MainWindow *newWin = new MainWindow;
newWin->show();
newWin->LoadFile(filename);
}
}
}
void MainWindow::on_actionNew_triggered()
{
MainWindow *newWin = new MainWindow;
newWin->show();
}
void MainWindow::on_actionSave_triggered()
{
if(cursaveflag)
SaveFile(curfile);
else
on_actionSaveAs_triggered();
}
void MainWindow::on_actionSaveAs_triggered()
{
filename2 = QFileDialog::getSaveFileName(this,tr("另存为"),curfile,"c++ file(*.cpp);;c file(*.c);;txt file(*.txt)");
if(!filename2.isEmpty())
SaveFile(filename2);
}
void MainWindow::on_actionDel_triggered()
{
QTextCursor cursor = ui->textEdit->textCursor();
if(!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.deleteChar();
}
void MainWindow::on_actionFind_triggered()
{
QDialog *dialog = new QDialog(this);
dialog->setMaximumSize(100,80);
findEdit = new QLineEdit(dialog);
findcasesensitive = new QCheckBox(tr("区分大小写"));
findBackforward = new QRadioButton(tr("向上查找"));
findForward = new QRadioButton(tr("向下查找"));
findForward->setChecked(true);
QLabel *dirLabel = new QLabel(tr("方向:"));
findBtn = new QPushButton(tr("查找下一个"),dialog);
QGridLayout *layout = new QGridLayout(dialog);
layout->addWidget(findEdit,0,0,1,2);
layout->addWidget(findBtn,0,2);
layout->addWidget(dirLabel,1,1);
layout->addWidget(findcasesensitive,2,0);
layout->addWidget(findBackforward,2,1);
layout->addWidget(findForward,2,2);
dialog->show();
connect(findBtn,SIGNAL(clicked(bool)),this,SLOT(findText()));
}
void MainWindow::on_actionFindNext_triggered()
{
if(findtext.isEmpty())
return;
if(!ui->textEdit->find(findtext,option))
QMessageBox::warning(this,tr("查找"),tr("找不到 %1").arg(findtext));
}
void MainWindow::on_actionSwap_triggered()
{
QDialog *dialog = new QDialog(this);
swapFindLabel = new QLabel(tr("查找内容:"),dialog);
swapFindLineEdit = new QLineEdit(dialog);
swapLabel = new QLabel(tr("替换为:"),dialog);
swapLineEdit = new QLineEdit(dialog);
swapFindBtn = new QPushButton(tr("查找下一个"),dialog);
swapBtn = new QPushButton(tr("替换"),dialog);
swapAllBtn = new QPushButton(tr("替换全部"),dialog);
QGridLayout *layout = new QGridLayout(dialog);
layout->addWidget(swapFindLabel,0,0);
layout->addWidget(swapFindLineEdit,0,1);
layout->addWidget(swapFindBtn,0,2);
layout->addWidget(swapLabel,1,0);
layout->addWidget(swapLineEdit,1,1);
layout->addWidget(swapBtn,1,2);
layout->addWidget(swapAllBtn,3,2);
dialog->show();
connect(swapFindBtn,SIGNAL(clicked(bool)),this,SLOT(swapfindText()));
connect(swapBtn,SIGNAL(clicked(bool)),this,SLOT(swapText()));
connect(swapAllBtn,SIGNAL(clicked(bool)),this,SLOT(swapAllText()));
}
void MainWindow::on_actionTranto_triggered()
{
QDialog *trantodialog = new QDialog(this);
linenum = ui->textEdit->document()->lineCount();
trantoLabel = new QLabel(tr("行号:"),trantodialog);
trantoLineEdit = new QLineEdit(trantodialog);
trantoLineEdit->setText(tr("%1").arg(linenum));
trantoBtn = new QPushButton(tr("转到"),trantodialog);
QGridLayout *layout = new QGridLayout(trantodialog);
layout->addWidget(trantoLabel,0,0);
layout->addWidget(trantoLineEdit,0,1);
layout->addWidget(trantoBtn,0,2);
trantodialog->show();
connect(trantoBtn,SIGNAL(clicked(bool)),this,SLOT(trantoText()));
connect(trantoBtn,SIGNAL(released()),trantodialog,SLOT(close()));
}
void MainWindow::on_actionSelectAll_triggered()
{
ui->textEdit->selectAll();
}
void MainWindow::on_actionTime_triggered()
{
QDateTime *date = new QDateTime(QDateTime::currentDateTime());
QTextCursor cursor = ui->textEdit->textCursor();
cursor.insertText(date->toString());
}
void MainWindow::on_actionFont_triggered()
{
bool ok;
QFont f = QFontDialog::getFont(&ok);
QTextCharFormat format;
format.setFont(f);
if(ok)
{
QTextCursor cursor = ui->textEdit->textCursor();
if(!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
ui->textEdit->mergeCurrentCharFormat(format);
}
}
上一篇: ansible快速入门