您的位置:首页 > 编程语言 > Qt开发

QT 新手练习 模仿系统自带 记事本

2011-06-11 15:23 295 查看
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
class QAction;
class QTextEdit;
class QLabel;
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
//rewrite event handle
protected:
void closeEvent(QCloseEvent *);
private:
//state flag
int changed;
//file menu
QAction *newAt;
QAction *openAt;
QAction *saveAt;
QAction *sAsAt;
QAction *quitAt;
QAction *undoAt;
//edit menu
QAction *findAt;
QAction *rPeAt;
//help menu
QAction *abtAt;
//work space
QTextEdit *textEdit;
//QString
QString fileName;
//QLabel
QLabel *numOfWord;
//function
private slots:
void open();
void newFile();
void save();
void saveAs();
void change();
void pressOk_have();
void pressOk_new();
void findNowNext(QString,bool);
void findNowPre(QString,bool);
void findText();
void setUndo(bool);
void repNow(QString,QString,bool);
void repAllNow(QString,QString,bool);
void repText();
};

#endif // MAINWINDOW_H

#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
class QLabel;
class QLineEdit;
class QCheckBox;
class QPushButton;
class QRadioButton;

class FindDialog:public QDialog
{
Q_OBJECT
public:
enum state{NEXT,PRE};
FindDialog(QWidget *parent=0);
private:
QLabel *findLabel;
QLineEdit *inText;
QCheckBox *checkBox;
QPushButton *next,
*cancel;
QRadioButton *up,
*down;
state uod;

void setupWidget();
void setConns();
signals:
void findNext(QString,bool b=0);
void findPrevious(QString,bool b=0);
private slots:
void sendsgl();
void setUp(bool);
};

#endif // FINDDIALOG_H

#ifndef REPLDIALOG_H
#define REPLDIALOG_H
#include <QDialog>
class QLabel;
class QPushButton;
class QCheckBox;
class QLineEdit;
class ReplDialog:public QDialog
{
Q_OBJECT
private:
QLabel *scLabel,
*reLabel;
QLineEdit *scEdit,
*reEdit;
QPushButton *repButton,
*repAllButton,
*cancelButton;
QCheckBox *sensCheckBox;
//Function
void setupWidget();
void setConns();
public:
ReplDialog(QWidget *parent=0);
signals:
void sigRep(QString,QString,bool);
void sigAll(QString,QString,bool);
private slots:
void sendRep();
void sendAll();
};

#endif // REPLDIALOG_H

#include "mainwindow.h"
#include <QtGui>
#include "finddialog.h"
#include "repldialog.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),changed(0)
{
fileName=tr("无标题");
this->setWindowTitle(fileName+"-Notepad+");
//QAction memory allocate
//file menu
newAt=new QAction(this);
openAt=new QAction(this);
saveAt=new QAction(this);
sAsAt=new QAction(this);
quitAt=new QAction(this);
//edit menu
undoAt=new QAction(this);
findAt=new QAction(this);
rPeAt=new QAction(this);
//help menu
abtAt=new QAction(this);
//text edit
textEdit=new QTextEdit;
//statustip
numOfWord=new QLabel(this);
numOfWord->setText(tr("您已经输入0个字符(包括换行回车等)"));

//QAction title set
newAt->setText(tr("新建"));
newAt->setShortcut(QKeySequence::New);
openAt->setText(tr("打开"));
openAt->setShortcut(QKeySequence::Open);
saveAt->setText(tr("保存"));
saveAt->setShortcut(QKeySequence::Save);
sAsAt->setText(tr("另存为"));
sAsAt->setShortcut(QKeySequence::SaveAs);
quitAt->setText(tr("退出"));
quitAt->setShortcut(QKeySequence::Quit);
findAt->setText(tr("查找"));
findAt->setShortcut(QKeySequence::Find);
rPeAt->setText(tr("替换"));
rPeAt->setShortcut(QKeySequence::Replace);
abtAt->setText(tr("关于"));
abtAt->setShortcut(QKeySequence::HelpContents);
undoAt->setText(tr("撤销"));
//Action enable set
undoAt->setEnabled(false);
findAt->setEnabled(false);

//text config
textEdit->setFont(QFont("宋体",10));

//布局

//file
QMenu *file=this->menuBar()->addMenu(tr("文件(&F)"));
file->addAction(newAt);
file->addAction(openAt);
file->addAction(saveAt);
file->addAction(sAsAt);
file->addAction(quitAt);
//edit
QMenu *edit=this->menuBar()->addMenu(tr("编辑(&E)"));
edit->addAction(undoAt);
edit->addAction(findAt);
edit->addAction(rPeAt);
//help
QMenu *help=this->menuBar()->addMenu(tr("帮助(&H)"));
help->addAction(abtAt);
//center
this->setCentralWidget(textEdit);

//statustip
this->statusBar()->addPermanentWidget(numOfWord);

//connections
connect(openAt,SIGNAL(triggered()),this,SLOT(open()));
connect(textEdit,SIGNAL(textChanged()),this,SLOT(change()));
connect(saveAt,SIGNAL(triggered()),this,SLOT(save()));
connect(newAt,SIGNAL(triggered()),this,SLOT(newFile()));
connect(sAsAt,SIGNAL(triggered()),this,SLOT(saveAs()));
connect(undoAt,SIGNAL(triggered()),textEdit,SLOT(undo()));
connect(findAt,SIGNAL(triggered()),this,SLOT(findText()));
connect(textEdit,SIGNAL(undoAvailable(bool)),this,SLOT(setUndo(bool)));
connect(rPeAt,SIGNAL(triggered()),this,SLOT(repText()));

//window size
this->resize(800,600);
}

MainWindow::~MainWindow()
{

}

//Event handle
void MainWindow::closeEvent(QCloseEvent *e)
{
if(changed==1)
{
QMessageBox msg(this);
msg.setText(tr("文件已改变"));
msg.setInformativeText(tr("要将修改保存到")+fileName+"?");
msg.setIcon(QMessageBox::Question);
msg.setStandardButtons(QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);
msg.setDefaultButton(QMessageBox::Save);
int ret = msg.exec();
switch(ret)
{
case QMessageBox::Save:
if(fileName!=tr("无标题"))
{
pressOk_have();break;
}
else
{
pressOk_new();break;
}
case QMessageBox::No:
e->accept();break;
case QMessageBox::Cancel:
e->ignore();break;
}
}
else
e->accept();
}
//function
void MainWindow::open()
{
fileName = QFileDialog::getOpenFileName(this, tr("打开文件"),
"/home",
tr("文本文档 (*.txt *.doc)"));
if(!fileName.isEmpty())
{
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text))
{
QTextStream toText(&file);
toText.setCodec("UNICODE");
textEdit->setPlainText(toText.readAll());
}
this->setWindowTitle(fileName+"-Notepad+");
changed=0;
}
}
void MainWindow::save()
{
if(fileName==tr("无标题"))
{
fileName = QFileDialog::getSaveFileName(this, tr("保存文件"),
"untitled.txt",
tr("文本文件 (*.txt)"));
QString content=textEdit->toPlainText();
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Text))
{
QTextStream save(&file);
save<<content;
this->setWindowTitle(fileName+"-Notepad+");
changed=0;
}
}
else
{
QString content=textEdit->toPlainText();
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Text))
{
QTextStream save(&file);
save<<content;
this->setWindowTitle(fileName+"-Notepad+");
changed=0;
}
}
}
void MainWindow::change()
{
if(textEdit->toPlainText().isEmpty())
{
findAt->setEnabled(false);
}
else
{
findAt->setEnabled(true);
}
numOfWord->setText(tr("您已经输入")+QString::number(textEdit->toPlainText().length())+tr("个字符(包括换行回车等)"));
this->setWindowTitle(fileName+"(*)"+"-Notepad+");
changed=1;
}
void MainWindow::pressOk_have()
{
QString content=textEdit->toPlainText();
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Text))
{
QTextStream save(&file);
save<<content;
this->setWindowTitle(fileName+"-Notepad+");
changed=0;
}
}
void MainWindow::pressOk_new()
{
fileName = QFileDialog::getSaveFileName(this, tr("保存文件"),
"untitled.txt",
tr("文本文件 (*.txt)"));
QString content=textEdit->toPlainText();
QFile file(fileName);
if(file.open(QFile::WriteOnly|QFile::Text))
{
QTextStream save(&file);
save<<content;
this->setWindowTitle(fileName+"-Notepad+");
changed=0;
}
}
void MainWindow::newFile()
{
if(changed==1)
{
QMessageBox msg(this);
msg.setText(tr("文件已改变"));
msg.setInformativeText(tr("要将修改保存到")+fileName+"?");
msg.setIcon(QMessageBox::Question);
msg.setStandardButtons(QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);
msg.setDefaultButton(QMessageBox::Save);
int ret = msg.exec();
switch(ret)
{
case QMessageBox::Save:
if(fileName!=tr("无标题"))
{
pressOk_have();
textEdit->clear();
fileName=tr("无标题");
this->setWindowTitle(fileName+"-Notepad+");
break;
}
else
{
pressOk_new();
textEdit->clear();
fileName=tr("无标题");
this->setWindowTitle(fileName+"-Notepad+");
break;
}
case QMessageBox::No:
textEdit->clear();
fileName=tr("无标题");
this->setWindowTitle(fileName+"-Notepad+");
break;
case QMessageBox::Cancel:
break;
}
}
else
{
textEdit->clear();
fileName=tr("无标题");
this->setWindowTitle(fileName+"-Notepad+");
}
changed=0;
}
void MainWindow::saveAs()
{
pressOk_new();
}
void MainWindow::findText()
{
FindDialog findit(this);
connect(&findit,SIGNAL(findNext(QString,bool)),this,SLOT(findNowNext(QString,bool)));
connect(&findit,SIGNAL(findPrevious(QString,bool)),this,SLOT(findNowPre(QString,bool)));
findit.exec();
}
void MainWindow::findNowPre(QString str,bool b)
{
switch(b)
{
case false:
if(textEdit->find(str,QTextDocument::FindBackward))
{}
else
{
QMessageBox::warning(this,tr("无法找到"),tr("已到开头或结尾,找不到")+str+"!");
}
break;
case true:
if(textEdit->find(str,QTextDocument::FindBackward|QTextDocument::FindCaseSensitively))
{}
else
{
QMessageBox::warning(this,tr("无法找到"),tr("已到开头或结尾,找不到")+str+"!");
}
break;
}
}
void MainWindow::findNowNext(QString str,bool b)
{
switch(b)
{
case false:
if(textEdit->find(str))
{}
else
{
QMessageBox::warning(this,tr("无法找到"),tr("已到开头或结尾,找不到")+str+"!");
}
break;
case true:
if(textEdit->find(str,QTextDocument::FindCaseSensitively))
{}
else
{
QMessageBox::warning(this,tr("无法找到"),tr("已到开头或结尾,找不到")+str+"!");
}
break;
}
}
void MainWindow::setUndo(bool b)
{
if(b==true)
{
undoAt->setEnabled(true);
}
else
{
undoAt->setEnabled(false);
}
}
void MainWindow::repText()
{
ReplDialog rep(this);
connect(&rep,SIGNAL(sigRep(QString,QString,bool)),this,SLOT(repNow(QString,QString,bool)));
connect(&rep,SIGNAL(sigAll(QString,QString,bool)),this,SLOT(repAllNow(QString,QString,bool)));
rep.exec();
}
void MainWindow::repNow(QString s1, QString s2, bool b)
{
QTextCursor textCur=textEdit->textCursor();
switch(b)
{
case false:
if(textEdit->find(s1))
{
textCur=textEdit->textCursor();
textCur.insertText(s2);
}
else
{
QMessageBox::warning(this,tr("提示"),tr("找不到需要替换的内容,可能光标在要查找的内容之后!"),QMessageBox::Ok);
}
break;
case true:
if(textEdit->find(s1,QTextDocument::FindCaseSensitively))
{
textCur=textEdit->textCursor();
textCur.insertText(s2);
}
else
{
QMessageBox::warning(this,tr("提示"),tr("找不到需要替换的内容,可能光标在要查找的内容之后!"),QMessageBox::Ok);
}
break;
}
}
void MainWindow::repAllNow(QString s1, QString s2, bool b)
{
qDebug()<<"Run";
QTextCursor textSt;
QTextCursor textCur=textEdit->textCursor();
textCur.movePosition(QTextCursor::Start);
textEdit->setTextCursor(textCur);
switch(b)
{
case false:
while(textEdit->find(s1))
{
textSt=textEdit->textCursor();
textSt.insertText(s2);
}
break;
case true:
while(textEdit->find(s1,QTextDocument::FindCaseSensitively))
{
textSt=textEdit->textCursor();
textSt.insertText(s2);
}
break;
}
}

#include "finddialog.h"
#include <QtGui>
FindDialog::FindDialog(QWidget *parent):QDialog(parent)
{
uod=NEXT;
setupWidget();
setConns();
}
void FindDialog::setupWidget()
{
findLabel=new QLabel(tr("查找内容:"),this);
inText=new QLineEdit(this);
checkBox=new QCheckBox(tr("大小写敏感"),this);
next=new QPushButton(tr("查找下一个"),this);
cancel=new QPushButton(tr("取消"),this);
up=new QRadioButton(tr("向前"),this);
down=new QRadioButton(tr("向后"),this);
down->setDown(true);
QHBoxLayout *topLayout=new QHBoxLayout;
QHBoxLayout *botLayout=new QHBoxLayout;
QVBoxLayout *rightLayout=new QVBoxLayout;
QVBoxLayout *leftLayout=new QVBoxLayout;
QHBoxLayout *mainLayout=new QHBoxLayout;
QHBoxLayout *cenLayout=new QHBoxLayout;
QGroupBox *upOrDown=new QGroupBox(tr("方向"),this);
cenLayout->addWidget(up);
cenLayout->addWidget(down);
upOrDown->setLayout(cenLayout);
topLayout->addWidget(findLabel);
topLayout->addWidget(inText);
botLayout->addWidget(checkBox);
botLayout->addWidget(upOrDown);
rightLayout->addWidget(next);
rightLayout->addWidget(cancel);
leftLayout->addLayout(topLayout);
leftLayout->addLayout(botLayout);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
this->setLayout(mainLayout);
}
void FindDialog::setConns()
{
connect(cancel,SIGNAL(clicked()),this,SLOT(close()));
connect(next,SIGNAL(clicked()),this,SLOT(sendsgl()));
connect(up,SIGNAL(toggled(bool)),this,SLOT(setUp(bool)));
}
void FindDialog::sendsgl()
{
if(uod==NEXT)
{
emit findNext(this->inText->text(),checkBox->isChecked());
qDebug()<<"send"<<checkBox->isChecked()<<uod;
}
else
{
emit findPrevious(this->inText->text(),checkBox->isChecked());
qDebug()<<"send"<<checkBox->isChecked()<<uod;
}
}
void FindDialog::setUp(bool n)
{
if(n)
{
uod=PRE;
}
else
{
uod=NEXT;
}
}

#include "repldialog.h"
#include <QtGui>
ReplDialog::ReplDialog(QWidget *parent):QDialog(parent)
{
setupWidget();
setConns();
}
void ReplDialog::setupWidget()
{
scLabel=new QLabel(tr("查找内容:"),this);
reLabel=new QLabel(tr("替换为  :"),this);
scEdit=new QLineEdit(this);
reEdit=new QLineEdit(this);
repButton=new QPushButton(tr("替换"),this);
repAllButton=new QPushButton(tr("替换全部"),this);
cancelButton=new QPushButton(tr("取消"),this);
sensCheckBox=new QCheckBox(tr("大小写敏感"),this);
QHBoxLayout *topLayout=new QHBoxLayout;
QHBoxLayout *midLayout=new QHBoxLayout;
QVBoxLayout *leftLayout=new QVBoxLayout;
QVBoxLayout *rightLayout=new QVBoxLayout;
QHBoxLayout *mainLayout=new QHBoxLayout;
topLayout->addWidget(scLabel);
topLayout->addWidget(scEdit);
midLayout->addWidget(reLabel);
midLayout->addWidget(reEdit);
leftLayout->addLayout(topLayout);
leftLayout->addLayout(midLayout);
leftLayout->addWidget(sensCheckBox);
rightLayout->addWidget(repButton);
rightLayout->addWidget(repAllButton);
rightLayout->addWidget(cancelButton);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
this->setLayout(mainLayout);
}
void ReplDialog::setConns()
{
connect(cancelButton,SIGNAL(clicked()),this,SLOT(close()));
connect(repButton,SIGNAL(clicked()),this,SLOT(sendRep()));
connect(repAllButton,SIGNAL(clicked()),this,SLOT(sendAll()));
}
//SLOTS
void ReplDialog::sendRep()
{
emit sigRep(scEdit->text(),reEdit->text(),sensCheckBox->isChecked());
qDebug()<<sensCheckBox->isChecked();
}
void ReplDialog::sendAll()
{
emit sigAll(scEdit->text(),reEdit->text(),sensCheckBox->isChecked());
}
用了QTextEdit

写的很烂 凑合着看看

主要写了打开 保存 另存为 查找 替换

其它都没写

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: