qt creator 版本6.8.0 MinGW 64bit
由此模块可以扩展成设计一个qt文本编辑器。
界面如下
部分功能展示如下
新建文件
打开文件
mdi模式、级联模式和平铺模式
界面和程序构建过程。
1.如图所需.cpp和.h文件
2.mainwindow.ui和tformdoc.ui界面布局如下
不懂什么是Action如何建立和使用的请看我的另一篇文章。
tformdoc.h和tformdoc.cpp代码如下
cpp
#ifndef TFORMDOC_H
#define TFORMDOC_H
#include <QWidget>
namespace Ui {
class TFormDoc;
}
class TFormDoc : public QWidget
{
Q_OBJECT
public:
explicit TFormDoc(QWidget *parent = nullptr);
~TFormDoc();
void loadFromFile(QString &aFileName);
QString currentFileName();
bool isFileOpen();
void saveToFile();
void textEditFont();
void textCut();
void textCopy();
void textPaste();
private:
Ui::TFormDoc *ui;
QString m_fileName;
bool m_fileOpened=false;
};
#endif // TFORMDOC_H
/******************************.cpp********************/
#include "tformdoc.h"
#include "ui_tformdoc.h"
#include<QFile>
#include<QFileInfo>
#include<QFontDialog>
TFormDoc::TFormDoc(QWidget *parent)
: QWidget(parent)
, ui(new Ui::TFormDoc)
{
ui->setupUi(this);
setWindowTitle("New Doc[*]");
setAttribute(Qt::WA_DeleteOnClose);
connect(ui->plainTextEdit,&QPlainTextEdit::modificationChanged,this,&TFormDoc::setWindowModified);
}
TFormDoc::~TFormDoc()
{
delete ui;
}
void TFormDoc::loadFromFile(QString &aFileName)
{
QFile aFile(aFileName);
if(aFile.open(QIODevice::ReadOnly|QIODevice::Text))
{
QTextStream aStream(&aFile);
ui->plainTextEdit->clear();
while(!aStream.atEnd())
{
ui->plainTextEdit->appendPlainText(aStream.readLine());
}
aFile.close();
QFileInfo fileInfo(aFileName);
QString str=fileInfo.fileName();
setWindowTitle(str+"[*]");
m_fileName=str;
m_fileOpened=true;
}
}
QString TFormDoc::currentFileName()
{
return m_fileName;
}
bool TFormDoc::isFileOpen()
{
return m_fileOpened;
}
void TFormDoc::saveToFile()
{
setWindowModified(false);
}
void TFormDoc::textEditFont()
{
QFont font;
font=ui->plainTextEdit->font();
bool ok=false;
font= QFontDialog::getFont(&ok,font);
if(ok)
{
ui->plainTextEdit->setFont(font);
}
}
void TFormDoc::textCut()
{
ui->plainTextEdit->cut();
}
void TFormDoc::textCopy()
{
ui->plainTextEdit->copy();
}
void TFormDoc::textPaste()
{
ui->plainTextEdit->paste();
}
mainwindow.h和mainwindow.cpp代码如下
cpp
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QMdiSubWindow;
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_actDocNew_triggered();
void on_actOpen_triggered();
void on_actFont_triggered();
void on_actCut_triggered();
void on_actCopy_triggered();
void on_actPaster_triggered();
void on_actionViewMode_triggered(bool checked);
void on_actTile_triggered();
void on_actCascade_triggered();
void on_actCloseAll_triggered();
void on_actDocSave_triggered();
void on_mdiArea_subWindowActivated(QMdiSubWindow *arg1);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
/***************************************************.cpp*******************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tformdoc.h"
#include<QMdiSubWindow>
#include<QDir>
#include<QFileDialog>
#include<QMdiSubWindow>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
setCentralWidget(ui->mdiArea);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actDocNew_triggered()
{
TFormDoc *tformDoc=new TFormDoc();
ui->mdiArea->addSubWindow(tformDoc);
tformDoc->show();
ui->actCut->setEnabled(true);
ui->actCopy->setEnabled(true);
ui->actPaster->setEnabled(true);
ui->actFont->setEnabled(true);
}
void MainWindow::on_actOpen_triggered()
{
bool newNeed;
TFormDoc *tformDoc;
if(ui->mdiArea->subWindowList().size()>0)
{
tformDoc=(TFormDoc*)ui->mdiArea->activeSubWindow()->widget();
newNeed=tformDoc->isFileOpen();
}
else
{
newNeed=true;
}
QString curPath=QDir::currentPath();
QString aFileName=QFileDialog::getOpenFileName(this,"打开一个文件",curPath,"c程序文件(*.h *.cpp);;文本文件(*.txt);;所有文件(*.*)");
if(aFileName.isEmpty())
return ;
if(newNeed)
{
tformDoc=new TFormDoc(this);
ui->mdiArea->addSubWindow(tformDoc);
}
tformDoc->loadFromFile(aFileName);
tformDoc->show();
ui->actCopy->setEnabled(true);
ui->actPaster->setEnabled(true);
ui->actCut->setEnabled(true);
ui->actFont->setEnabled(true);
}
void MainWindow::on_actFont_triggered()
{
TFormDoc *tformDoc=(TFormDoc*)ui->mdiArea->activeSubWindow()->widget();
tformDoc->textEditFont();
}
void MainWindow::on_actCut_triggered()
{
TFormDoc *tformDoc=(TFormDoc*)ui->mdiArea->activeSubWindow()->widget();
tformDoc->textCut();
}
void MainWindow::on_actCopy_triggered()
{
TFormDoc *tformDoc=(TFormDoc*)ui->mdiArea->activeSubWindow()->widget();
tformDoc->textCopy();
}
void MainWindow::on_actPaster_triggered()
{
TFormDoc *tformDoc=(TFormDoc*)ui->mdiArea->activeSubWindow()->widget();
tformDoc->textPaste();
}
void MainWindow::on_actionViewMode_triggered(bool checked)
{
if(checked)
{
ui->mdiArea->setViewMode(QMdiArea::TabbedView);
}
else
{
ui->mdiArea->setViewMode(QMdiArea::SubWindowView);
}
ui->mdiArea->setTabsClosable(checked);
ui->actCascade->setEnabled(!checked);
ui->actTile->setEnabled(!checked);
}
void MainWindow::on_actTile_triggered()
{
ui->mdiArea->tileSubWindows();
}
void MainWindow::on_actCascade_triggered()
{
ui->mdiArea->cascadeSubWindows();
}
void MainWindow::on_actCloseAll_triggered()
{
ui->mdiArea->closeAllSubWindows();
}
void MainWindow::on_actDocSave_triggered()
{
TFormDoc *tformDoc=(TFormDoc*)ui->mdiArea->activeSubWindow()->widget();
tformDoc->saveToFile();
}
void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)
{
Q_UNUSED(arg1);
if(ui->mdiArea->subWindowList().size()==0)
{
ui->actCopy->setEnabled(false);
ui->actPaster->setEnabled(false);
ui->actCut->setEnabled(false);
ui->actFont->setEnabled(false);
}
}
欢迎大家一起交流。