当前位置:网站首页>Text editor of QT project practice ---------- episode 11
Text editor of QT project practice ---------- episode 11
2022-06-25 01:00:00 【PureヾChan】
In previous blogs, the function of text editor has been almost realized , The functions of words and paragraphs are almost completed , But is it still ignoring the realization of color and bullet function ?
Next , Is to realize the color function and bullet function .
towards mainWindow.h Add function :
public:
void textColor();Function implementation :
void MainWindow::textColor()
{
if(activateChildWnd()){
QColor color=QColorDialog::getColor(activateChildWnd()->textColor(),this);// The first of the two parameters is to specify the color
if(!color.isValid()) return;
QTextCharFormat fmt;// Define a format object
fmt.setForeground(color);// Set the color
activateChildWnd()->setFormatOnSelectedWord(fmt);
QPixmap pix(16,16);// Choose a color , By the way, change the icon
pix.fill(color);
ui->colorAction->setIcon(pix);
}
}First of all, if you want to change the color, you must first judge whether it is an active sub window , Tugu is the use of QColorDialog Class getColor Function call palette , It will return the color selected by the user . Then judge whether the color is effective , If it is invalid, it directly returns .
Then define a text object , Temporarily setting the color format , When invoking the active child window setFormatOnSelectedWord Function to finally set the color format .Qpixmap It's a bitmap , Define this so that we can directly switch to the corresponding color on the bitmap and display it in the text box .
towards mainWindow.h Add slot function :‘
private slots:
void on_colorAction_triggered();Slot function implementation :
void MainWindow::on_colorAction_triggered()
{
textColor();
}Realize bullet application :
towards ChildWnd.h Add function :
public:
void setParaSyle(int pStyle);// Change bullets Function implementation :
void ChildWnd::setParaSyle(int pStyle)
{
QTextCursor tcursor=textCursor();// Get a copy of the cursor
QTextListFormat::Style sname;
if(pStyle!=0){
switch (pStyle) {
case 1:
sname=QTextListFormat::ListDisc;// Solid round
break;
case 2:
sname=QTextListFormat::ListCircle;// The hollow circle 、
break;
case 3:
sname=QTextListFormat::ListSquare;// square
break;
case 4:
sname=QTextListFormat::ListDecimal;// Decimal numbers
break;
case 5:
sname=QTextListFormat::ListLowerAlpha;// Small Aladdin symbol
break;
case 6:
sname=QTextListFormat::ListUpperAlpha;// Capital Aladdin symbol
break;
case 7:
sname=QTextListFormat::ListLowerRoman;// Lowercase Roman characters
break;
case 8:
sname=QTextListFormat::ListUpperRoman;// Capital Roman characters
break;
default:
sname=QTextListFormat::ListDisc;// Solid round
break;
}
tcursor.beginEditBlock();
QTextBlockFormat tBlockFmt=tcursor.blockFormat();// Return text block format
QTextListFormat tListFmt;
if(tcursor.currentList()){
tListFmt=tcursor.currentList()->format();// Returns the current linked list format
}else{
tListFmt.setIndent(tBlockFmt.indent()+1);// Set indent
tBlockFmt.setIndent(0);
tcursor.setBlockFormat(tBlockFmt);
}
tListFmt.setStyle(sname);// Set bullets
tcursor.createList(tListFmt);
tcursor.endEditBlock();
}else{
QTextBlockFormat tbfmt;
tbfmt.setObjectIndex(-1);// Set the selected sequence number , That is, the corresponding enumeration value , Of course -1 non-existent
tcursor.mergeBlockFormat(tbfmt);// After setting up , And then merge it into the block format
}
}The parameter passed in represents the sequence number of the selected bullet , So let's define a QTextCursor Text cursor object tCursor Get a copy of the cursor textCursor(), Let me define one more QTextListFormat Class object , It is used to save the enumeration value of bullet format each time using the sequence number , This will be used later to set the bullet of the entire text box .
Then specify the start edit block of the text cursor , So let's define one QTextBlockFormat Object to get the block format of the current text cursor , Let me define one more QTextListFormat List format object , Then judge whether the current cursor has a linked list . If there is , Save the format of the current linked list , If not, set the indent of the linked list format , Set the format indent of the block format to 0, Finally, format the block .
Then use sname Saved bullet enumeration values , Then set the final format of the linked list format , utilize tListFmt Create a linked list in the saved linked list format . In the above case, the parameter passed in is not 0 The operation of , If the parameters passed in do not meet the requirements , Then set a nonexistent enumeration value to the format .
stay mainWindow.h Add functions and slot functions :
public:
void paraStyle(int Style);
private slots:
void on_comboBox_activated(int index);Function implementation :
void MainWindow::paraStyle(int Style)
{
if(activateChildWnd()){
activateChildWnd()->setParaSyle(Style);
}
}
void MainWindow::on_comboBox_activated(int index)
{
paraStyle(index);
}
Slot function call paraStyle(int style) function , This function calls the modified bullet function of the subclass to add bullets .
Program running effect ;

Source code :
ChildWnd.h
#ifndef CHILDWND_H
#define CHILDWND_H
#include<QTextEdit>
// The idea is to create a sub window editing area in the main window , Is to call the constructor of another class , So that is ChildWnd This class
class ChildWnd : public QTextEdit
{
Q_OBJECT
public:
ChildWnd();
QString m_Currentpath; // The path of the current document
void newDoc(); // New document
QString GetCurDocName();// Extract the document name from the file path
bool loadDoc(const QString& docName);
void setCurDoc(const QString& docName);// Set the current document function
bool saveDoc();// Save the document
bool saveAsDoc();// Save as document
bool saveDocOpt(QString &docName);// Actually perform the save operation
void setFormatOnSelectedWord(const QTextCharFormat& fmt);
void setAlignofDocumnetText(int AlignType);
void setParaSyle(int pStyle);// Change bullets
protected:
void closeEvent(QCloseEvent *event);// Turn off event handling
private:
bool promptSave();// Try to save
private slots:
void docBeModified(); // The corresponding slot function when modifying the document
private:
bool m_bSaved; // Whether the document is saved
};
#endif // CHILDWND_H
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include"childwnd.h"
#include<QSignalMapper>
#include<QMdiSubWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void InitMainWindow();// Initialize the window function
void DocNew();
void DocOpen();
void docSave();
void docSaveAs();
void docUndo();// revoke
void docRedo();// rewrite
void docCut();// shear
void docCopy();// Copy
void docPaste();// Paste
void textBold();// In bold
void textItalic();// Oblique cutting
void textUnderline();// Underline
void textFamliy(const QString& fmly);// Set text style
void textSize(const QString& font);// Set text font size
void textColor();
void paraStyle(int Style);
private slots:
void on_newAction_triggered();
void RefreshMenu();// Refresh window menu slot function
void addSubWindowListMenu();// Refresh window information function
void on_closeAction_triggered();
void on_closeAllAction_triggered();
void on_titleAction_triggered();
void on_cascadeAction_triggered();
void on_nextAction_triggered();
void on_previousAction_triggered();
void setActivSubWindow(QWidget*wnd);
void on_openAction_triggered();
void on_saveAction_triggered();
void on_saveAsAction_triggered();
void on_undoAction_triggered();
void on_redoAction_triggered();
void on_cutAction_triggered();
void on_copyAction_triggered();
void on_pasteAction_triggered();
void on_boldAction_triggered();
void on_italicAction_triggered();
void on_underlineAction_triggered();
void on_fontComboBox_activated(const QString &arg1);
void on_FontSize_comboBox_activated(const QString &arg1);
void on_letfAlignAction_triggered();
void on_centerAlignAction_triggered();
void on_rightAlignAction_triggered();
void on_justifyAction_triggered();
void on_colorAction_triggered();
void on_comboBox_activated(int index);
protected:
void closeEvent(QCloseEvent *event);
private:
void formatEnable();
ChildWnd* activateChildWnd();// obtain mdieara Active child window of
// Find window functions
QMdiSubWindow* FindChildWnd(const QString& docName);
private:
Ui::MainWindow *ui;
QSignalMapper* m_singnalmapper;// Create a signal mapper
};
#endif // MAINWINDOW_H
ChildWnd.cpp
#include "childwnd.h"
#include<QFileInfo>
#include<QFileDialog>
#include<QTextDocumentWriter>
#include<QMessageBox>
#include<QCloseEvent>
#include<QTextBlockFormat>
#include<QTextListFormat>
#include<QtWidgets>
ChildWnd::ChildWnd()
{
// Destroy the instance object of the class when the child window is closed , Reach recycling
setAttribute(Qt::WA_DeleteOnClose);
m_bSaved=false;// The original edit state is unchanged
}
void ChildWnd::newDoc()
{
static int wndSeqNum=1;
m_Currentpath=QString("WPS file %1").arg(wndSeqNum++);// Each time you create a document, the name is different ,, utilize wndSeqNum To achieve the goal
// Set the title of the form , Add... After document changes “*” Number identification
setWindowTitle(m_Currentpath+"[*]"+"-MyWPS");
connect(document(),SIGNAL(contentsChanged()),
this,SLOT(docBeModified()));
}
QString ChildWnd::GetCurDocName()
{
return QFileInfo(m_Currentpath).fileName();//fileINfo Class can be called from a file filename operation , Returns the file name extracted from the file path
}
bool ChildWnd::loadDoc(const QString &docName)
{
// First determine whether the loaded document name is empty
if(!docName.isEmpty()){
QFile file(docName);// Get all the information of the file through the file name
if(!file.exists()) return false;// File does not exist return false
if(!file.open(QFile::ReadOnly)) return false;// If the read-only mode fails to open, it returns false
QByteArray text=file.readAll();
if(Qt::mightBeRichText(text)){
setHtml(text);// If it is rich text, it is set to HTml
}else{
setPlainText(text);// If the rich text is not set to simple string format
}
setCurDoc(docName);
connect(document(),SIGNAL(contentsChanged()),
this,SLOT(docBeModified()));
return true;
}
}
void ChildWnd::setCurDoc(const QString &docName)
{
m_Currentpath=QFileInfo(docName).canonicalFilePath();// It will put the path name of the file “.” All removed
m_bSaved=true;
document()->setModified(false);// Set the document modification property to false
setWindowModified(false); // The window does not display the change logo
setWindowTitle(GetCurDocName()+"[*]");// Set sub window title
}
bool ChildWnd::saveDoc()
{
if(m_bSaved) return saveDocOpt(m_Currentpath);// Save directly after saving
else saveAsDoc();// Save as
}
bool ChildWnd::saveAsDoc()
{
// Save as, of course, provides a file window to prompt for saving
QString docName=QFileDialog::getSaveFileName(this,
" Save as ",
m_Currentpath,
"HTML file (*.htm *html);;"
" All the files (*.*)");
if(docName.isEmpty()) return false;
else return saveDocOpt(docName);
}
bool ChildWnd::saveDocOpt(QString &docName)
{
if(!(docName.endsWith(".htm",Qt::CaseInsensitive)||
docName.endsWith(".html",Qt::CaseInsensitive))){
// If it does not end in the specified format , Add specified format
docName+=".html";
}
QTextDocumentWriter writer(docName);
bool isSuccess=writer.write(this->document());//write Method returns the write state
if(isSuccess) setCurDoc(docName);// Survey setting window status function
return isSuccess;
}
void ChildWnd::setFormatOnSelectedWord(const QTextCharFormat &fmt)
{
QTextCursor tCursor=textCursor();
// Uncheck to select the text setting under the cursor
if(!tCursor.hasSelection()) tCursor.select(QTextCursor::WordUnderCursor);
// Selected , Merge text formatting
tCursor.mergeCharFormat(fmt);// Merge the text selected by the cursor with the selected format
mergeCurrentCharFormat(fmt);// Set the format of the subsequent text editor to the cursor selected text
}
void ChildWnd::setAlignofDocumnetText(int AlignType)
{
switch (AlignType) {
case 1:
setAlignment(Qt::AlignLeft|Qt::AlignAbsolute);
break;
case 2:
setAlignment(Qt::AlignRight|Qt::AlignAbsolute);
break;
case 3:
setAlignment(Qt::AlignCenter);
break;
case 4:
setAlignment(Qt::AlignJustify);
break;
default:
break;
}
}
void ChildWnd::setParaSyle(int pStyle)
{
QTextCursor tcursor=textCursor();// Get a copy of the cursor
QTextListFormat::Style sname;
if(pStyle!=0){
switch (pStyle) {
case 1:
sname=QTextListFormat::ListDisc;// Solid round
break;
case 2:
sname=QTextListFormat::ListCircle;// The hollow circle 、
break;
case 3:
sname=QTextListFormat::ListSquare;// square
break;
case 4:
sname=QTextListFormat::ListDecimal;// Decimal numbers
break;
case 5:
sname=QTextListFormat::ListLowerAlpha;// Small Aladdin symbol
break;
case 6:
sname=QTextListFormat::ListUpperAlpha;// Capital Aladdin symbol
break;
case 7:
sname=QTextListFormat::ListLowerRoman;// Lowercase Roman characters
break;
case 8:
sname=QTextListFormat::ListUpperRoman;// Capital Roman characters
break;
default:
sname=QTextListFormat::ListDisc;// Solid round
break;
}
tcursor.beginEditBlock();
QTextBlockFormat tBlockFmt=tcursor.blockFormat();// Return text block format
QTextListFormat tListFmt;
if(tcursor.currentList()){
tListFmt=tcursor.currentList()->format();// Returns the current linked list format
}else{
tListFmt.setIndent(tBlockFmt.indent()+1);// Set indent
tBlockFmt.setIndent(0);
tcursor.setBlockFormat(tBlockFmt);
}
tListFmt.setStyle(sname);// Set bullets
tcursor.createList(tListFmt);
tcursor.endEditBlock();
}else{
QTextBlockFormat tbfmt;
tbfmt.setObjectIndex(-1);// Set the selected sequence number , That is, the corresponding enumeration value , Of course -1 non-existent
tcursor.mergeBlockFormat(tbfmt);// After setting up , And then merge it into the block format
}
}
void ChildWnd::closeEvent(QCloseEvent *event)
{
if(promptSave()) event->accept();// Accept the event
else event->ignore();// Ignore Events
}
bool ChildWnd::promptSave()
{
// The document has not been modified
if(!document()->isModified()) return true;
QMessageBox::StandardButton result;
result=QMessageBox::warning(this,
QString(" System prompt "),
QString(" file %1 The modified , Save ?")
.arg(GetCurDocName()),
QMessageBox::Yes|
QMessageBox::Discard|
QMessageBox::No);
if(result==QMessageBox::Yes){
return saveDoc();
}else if(result==QMessageBox::No){
return false;
}
return true;// Select ignore to return true
}
void ChildWnd::docBeModified()
{
setWindowModified(document()->isModified());
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"childwnd.h"
#include<QDebug>
#include<QMdiSubWindow>
#include<QList>
#include<QCloseEvent>
#include<QFileDialog>
#include<QColorDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
InitMainWindow();
}
MainWindow::~MainWindow()
{
delete ui;
}
// Initialize the window function
void MainWindow::InitMainWindow()
{
// Initialize the character list
QFontDatabase fontdb;
foreach (int fontsize, fontdb.standardSizes()) {
//standarSizes The function returns the standard character list . Here is the size of each standard character
// Pass to fontsize
ui->FontSize_comboBox->addItem(QString::number(fontsize));// Pass the characters obtained each time to the component list
}
QFont defFont;// Program default font ( Including font size and font style )
QString sFontSize;
int defFontSize;// The default font size of the current application
int defFontindex;// Serial number of the current font size table
defFont=QApplication::font();// Get the font style of the current program
defFontSize=defFont.pointSize();// Returns the current font size
sFontSize=QString::number(defFontSize);// Convert the integer font size to a convenient list of string type to find the sequence number
defFontindex=ui->FontSize_comboBox->findText(sFontSize);// Return the sequence number corresponding to the default font size
ui->FontSize_comboBox->setCurrentIndex(defFontindex);// Set the font size and serial number under the default program
// Add scroll bar
ui->mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);// Set the vertical scroll bar , Property to display when needed
ui->mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);// Set the horizontal scroll bar , Property to display when needed
// Note: when setting the scroll bar, you will encounter a small bug, When you want to set the scroll bar, you should leave a little space for the horizontal scroll bar , Otherwise it can't show
RefreshMenu();// When initializing, call to initialize the toolbar first 、
// When there are active child windows , Transmit signal refresh menu
connect(ui->mdiArea,&QMdiArea::subWindowActivated,this,&MainWindow::RefreshMenu);
addSubWindowListMenu();
// Here we need another slot function corresponding to , When the window is clicked, new window contents are added
connect(ui->menu_W,&QMenu::aboutToShow,this,&MainWindow::addSubWindowListMenu);
// Initialize the signal mapper
m_singnalmapper=new QSignalMapper(this);
connect(m_singnalmapper,SIGNAL(mapped(QWidget*)),
this,SLOT(setActivSubWindow(QWidget*)));
// We know that there can only be one alignment , So when we create the main form, we can only select one of the four alignments
QActionGroup* QAlignGroup=new QActionGroup(this);
QAlignGroup->addAction(ui->letfAlignAction);
QAlignGroup->addAction(ui->rightAlignAction);
QAlignGroup->addAction(ui->centerAlignAction);
QAlignGroup->addAction(ui->justifyAction);
}
void MainWindow::DocNew()
{
ChildWnd* childwnd=new ChildWnd;
ui->mdiArea->addSubWindow(childwnd);// Add the new sub window to the editing area
// Assign and cut at the same time Action Establishing a connection
connect(childwnd,SIGNAL(copyAvailable(bool)),ui->cutAction,SLOT(setEnabled(bool)));
connect(childwnd,SIGNAL(copyAvailable(bool)),ui->copyAction,SLOT(setEnabled(bool)));
childwnd->newDoc();
childwnd->show();
formatEnable();// Set the toolbar to available after creating a new window
}
void MainWindow::DocOpen()
{
QString docName=QFileDialog::getOpenFileName(this,
" Open file ",
"",
" text file (*.txt);;"
"HTML file (*.html *.htm);;"
" All the files (*.*)");
if(!docName.isEmpty()){
QMdiSubWindow* existWnd=FindChildWnd(docName);
if(existWnd){
// If you find this sub window
ui->mdiArea->setActiveSubWindow(existWnd);// Make this window active
return;
}
ChildWnd* childWnd=new ChildWnd;
ui->mdiArea->addSubWindow(childWnd);
connect(childWnd,SIGNAL(copyAvailable(bool)),
ui->cutAction,SLOT(setEnabled(bool)));//copyAvailable It is triggered when the document is opened and the text is selected
connect(childWnd,SIGNAL(copyAvailable(bool)),
ui->copyAction,SLOT(setEnabled(bool)));
if(childWnd->loadDoc(docName)){
statusBar()->showMessage(" The file is open ",3000);
childWnd->show();
formatEnable();
}else{
childWnd->close();
}
}
}
void MainWindow::docSave()
{
if(activateChildWnd()&&activateChildWnd()->saveDoc()){
statusBar()->showMessage(" Saved successfully ",3000);
}
}
void MainWindow::docSaveAs()
{
if(activateChildWnd()&&activateChildWnd()->saveAsDoc()){
statusBar()->showMessage(" Saved successfully ",3000);
}
}
void MainWindow::docUndo()
{
if(activateChildWnd()){
activateChildWnd()->undo();
}
}
void MainWindow::docRedo()
{
if(activateChildWnd()){
activateChildWnd()->redo();
}
}
void MainWindow::docCut()
{
if(activateChildWnd()){
activateChildWnd()->cut();
}
}
void MainWindow::docCopy()
{
if(activateChildWnd()){
activateChildWnd()->copy();
}
}
void MainWindow::docPaste()
{
if(activateChildWnd()){
activateChildWnd()->paste();
}
}
void MainWindow::textBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(ui->boldAction->isChecked()?QFont::Bold:QFont::Normal);
if(activateChildWnd()){
activateChildWnd()->setFormatOnSelectedWord(fmt);
}
}
void MainWindow::textItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(ui->italicAction->isChecked());
// Determine as active child window
if(activateChildWnd()){
activateChildWnd()->setFormatOnSelectedWord(fmt);
}
}
void MainWindow::textUnderline()
{
QTextCharFormat fmt;
fmt.setFontUnderline(ui->underlineAction->isChecked());
// Determine whether it is an active child window
if(activateChildWnd()){
activateChildWnd()->setFormatOnSelectedWord(fmt);// Set the format according to the selected format
}
}
void MainWindow::textFamliy(const QString& fmly)
{
QTextCharFormat fmt;
fmt.setFontFamily(fmly);// Set text font style
if(activateChildWnd()){
activateChildWnd()->setFormatOnSelectedWord(fmt);
}
}
void MainWindow::textSize(const QString& font)
{
qreal fontsize=font.toFloat();
if(font.toFloat()>0){
QTextCharFormat fmt;
fmt.setFontPointSize(fontsize);// Set format font size
if(activateChildWnd()){
activateChildWnd()->setFormatOnSelectedWord(fmt);// Set the font format of the text window
}
}
}
void MainWindow::textColor()
{
if(activateChildWnd()){
QColor color=QColorDialog::getColor(activateChildWnd()->textColor(),this);// The first of the two parameters is to specify the color
if(!color.isValid()) return;
QTextCharFormat fmt;// Define a format object
fmt.setForeground(color);// Set the color
activateChildWnd()->setFormatOnSelectedWord(fmt);
QPixmap pix(16,16);// Choose a color , By the way, change the icon
pix.fill(color);
ui->colorAction->setIcon(pix);
}
}
void MainWindow::paraStyle(int Style)
{
if(activateChildWnd()){
activateChildWnd()->setParaSyle(Style);
}
}
void MainWindow::formatEnable()
{
ui->boldAction->setEnabled(true);// Set the functions of the toolbar to available
ui->italicAction->setEnabled(true);
ui->underlineAction->setEnabled(true);
ui->letfAlignAction->setEnabled(true);
ui->centerAlignAction->setEnabled(true);
ui->rightAlignAction->setEnabled(true);
ui->justifyAction->setEnabled(true);
ui->colorAction->setEnabled(true);
//qDebug()<<12%1<<endl;
}
ChildWnd *MainWindow::activateChildWnd()
{
QMdiSubWindow*actWnd=ui->mdiArea->activeSubWindow();// Get the active window of the document editor
if(actWnd)
return qobject_cast<ChildWnd*>(actWnd->widget());// If it is an active window, convert the window to the target window and return
else
return 0;
}
QMdiSubWindow *MainWindow::FindChildWnd(const QString &docName)
{
QString strFile=QFileInfo(docName).canonicalFilePath();
foreach (QMdiSubWindow* subWnd, ui->mdiArea->subWindowList()) {
ChildWnd* childWnd=qobject_cast<ChildWnd*>(subWnd->widget());// Convert each child window to ChildWnd type
if(childWnd->m_Currentpath==strFile)return subWnd;
}
return 0;
}
void MainWindow::on_newAction_triggered()
{
DocNew();// Click the create action to create a new document
}
void MainWindow::RefreshMenu()
{
bool hasChild;
hasChild=(activateChildWnd()!=0);
ui->saveAction->setEnabled(hasChild);
ui->printAction->setEnabled(hasChild);
ui->saveAsAction->setEnabled(hasChild);
ui->printPreviewAction->setEnabled(hasChild);
ui->pasteAction->setEnabled(hasChild);
ui->closeAction->setEnabled(hasChild);
ui->closeAllAction->setEnabled(hasChild);
ui->titleAction->setEnabled(hasChild);
ui->cascadeAction->setEnabled(hasChild);
ui->nextAction->setEnabled(hasChild);
ui->previousAction->setEnabled(hasChild);
// Besides the above document operations, there are also text operations
bool hasSelect=(activateChildWnd() && activateChildWnd()->textCursor().hasSelection());//textcursor The function returns information about the selected text , This function is used to check whether the text is selected
ui->copyAction->setEnabled(hasSelect);
ui->cutAction->setEnabled(hasSelect);
ui->boldAction->setEnabled(hasSelect);
ui->italicAction->setEnabled(hasSelect);
ui->underlineAction->setEnabled(hasSelect);
ui->letfAlignAction->setEnabled(hasSelect);
ui->centerAlignAction->setEnabled(hasSelect);
ui->rightAlignAction->setEnabled(hasSelect);
ui->colorAction->setEnabled(hasSelect);
ui->justifyAction->setEnabled(hasSelect);
}
void MainWindow::addSubWindowListMenu()
{
// Because every time you click hi Execute this function , If you don't clear it first, the menu bar will become longer and longer
ui->menu_W->clear();
ui->menu_W->addAction(ui->closeAction);
ui->menu_W->addAction(ui->closeAllAction);
ui->menu_W->addSeparator();
ui->menu_W->addAction(ui->titleAction);
ui->menu_W->addAction(ui->cascadeAction);
ui->menu_W->addSeparator();
ui->menu_W->addAction(ui->nextAction);
ui->menu_W->addAction(ui->previousAction);
// This is the corresponding function when clicking on the window , Then we have to judge whether there is an active sub window
QList<QMdiSubWindow*> wnds=ui->mdiArea->subWindowList();// This function will mdiArea The window in the is returned in the form of a linked list
if(!wnds.isEmpty()){
ui->menu_W->addSeparator();// If there is an active child window, add a split line
}
for(int i=0;i<wnds.size();++i){
ChildWnd* childwnd=qobject_cast<ChildWnd*>(wnds.at(i)->widget());// Convert the elements in the linked list into components , Then the type is converted to ChildWnds
QString menuItem_text;
menuItem_text=QString("%1 %2")
.arg(i+1)
.arg(childwnd->GetCurDocName());
QAction* menuItem_act=ui->menu_W->addAction(menuItem_text);// Add the name of the window to menu_w Inside
menuItem_act->setCheckable(true);
menuItem_act->setChecked(childwnd==activateChildWnd());// Check whether it is an active sub window ,activateChildWnd Function returns whether the window is an active child window
// Add each one to the Action Add signal
connect(menuItem_act,SIGNAL(triggered(bool)),
m_singnalmapper,SLOT(map()));// every last action The menu will trigger when it is clicked triggered Signal execution map() Slot function
// In this case, it will be implemented map() function , But how to distinguish them ?
m_singnalmapper->setMapping(menuItem_act,wnds.at(i));// By setting different Action The difference between , Use serial numbers to distinguish
}
formatEnable();
}
void MainWindow::on_closeAction_triggered()
{
ui->mdiArea->closeActiveSubWindow();// Close the active child window
}
void MainWindow::on_closeAllAction_triggered()
{
ui->mdiArea->closeAllSubWindows();// Close all active child windows
}
void MainWindow::on_titleAction_triggered()
{
ui->mdiArea->tileSubWindows();// tile
}
void MainWindow::on_cascadeAction_triggered()
{
ui->mdiArea->cascadeSubWindows();//cengdie
}
void MainWindow::on_nextAction_triggered()
{
ui->mdiArea->activateNextSubWindow();// Next window
}
void MainWindow::on_previousAction_triggered()
{
ui->mdiArea->activatePreviousSubWindow();// The previous window
}
void MainWindow::setActivSubWindow(QWidget*wnd)
{
// First, determine whether it is an active child window
if(!wnd){
return;
}else{
// Set this window in the text editing area as the active sub window
ui->mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow*>(wnd));
// Setting the active sub window is passed into a text editing area mdiSubWindow Of , So type conversion is used here
}
}
void MainWindow::closeEvent(QCloseEvent *event){
ui->mdiArea->closeAllSubWindows();// When the window is closed, of course, the child window must also be closed
if(ui->mdiArea->currentSubWindow())
event->ignore();// Ignore this event
else
event->accept();// Accept this event
}
void MainWindow::on_openAction_triggered()
{
DocOpen();// Open document
}
void MainWindow::on_saveAction_triggered()
{
docSave();// preservation
}
void MainWindow::on_saveAsAction_triggered()
{
docSaveAs();// Save as
}
void MainWindow::on_undoAction_triggered()
{
docUndo();
}
void MainWindow::on_redoAction_triggered()
{
docRedo();
}
void MainWindow::on_cutAction_triggered()
{
docCut();
}
void MainWindow::on_copyAction_triggered()
{
docCopy();
}
void MainWindow::on_pasteAction_triggered()
{
docPaste();
}
void MainWindow::on_boldAction_triggered()
{
textBold();
}
void MainWindow::on_italicAction_triggered()
{
textItalic();
}
void MainWindow::on_underlineAction_triggered()
{
textUnderline();
}
void MainWindow::on_fontComboBox_activated(const QString &arg1)
{
textFamliy(arg1);
}
void MainWindow::on_FontSize_comboBox_activated(const QString &arg1)
{
textSize(arg1);
}
void MainWindow::on_letfAlignAction_triggered()
{
if(activateChildWnd()){
activateChildWnd()->setAlignofDocumnetText(1);
}
}
void MainWindow::on_centerAlignAction_triggered()
{
if(activateChildWnd()){
activateChildWnd()->setAlignofDocumnetText(3);
}
}
void MainWindow::on_rightAlignAction_triggered()
{
if(activateChildWnd()){
activateChildWnd()->setAlignofDocumnetText(2);
}
}
void MainWindow::on_justifyAction_triggered()
{
if(activateChildWnd()){
activateChildWnd()->setAlignofDocumnetText(4);
}
}
void MainWindow::on_colorAction_triggered()
{
textColor();
}
void MainWindow::on_comboBox_activated(int index)
{
paraStyle(index);
}
ui I won't release the file ... Finally, it will be integrated into a compressed package
边栏推荐
- Garbage collection of C closure
- Hyperledger Fabric 2. X dynamic update smart contract
- Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...
- 移动安全工具-jarsigner
- QT(35)-操作EXCEL-QXlsx-QAxObject
- December 6, 2019 what happens after the browser enters a URL
- 108页(4万字)未来公寓智能化设计平台项目方案建议书2022版
- Thermodynamic diagram display correlation matrix
- Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download
- I 刷题 I — 复制带随机指针的链表
猜你喜欢

Custom animation (simulated win10 loading animation) - Optimization
![[microservices sentinel] sentinel quick start | building an image | starting the console](/img/88/a01c8120f6117f1b8e4463cf6f685f.png)
[microservices sentinel] sentinel quick start | building an image | starting the console

热力图展示相关矩阵
[distributed system design profile (2)] kV raft

Punch smart spirit 1. The brand is attractive. What is the strength of the product?

【微服务|Sentinel】簇点链路|微服务集群环境搭建

2021-11-07

Binder mechanism and Aidl communication example

百公里加速仅5.92秒,威兰达高性能版以高能产品实力领跑

Single blind box removal, social blind box and friend blind box program source code
随机推荐
Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022
2022年全国最新消防设施操作员(高级消防设施操作员)模拟题及答案
[microservices sentinel] cluster link | microservices cluster environment construction
December 6, 2019 what happens after the browser enters a URL
D omit parameter name
WordPress add photo album function [advanced custom fields Pro custom fields plug-in series tutorial]
Punch smart spirit 1. The brand is attractive. What is the strength of the product?
Hot fix sophix multi-channel patch solution
Infotnews | is the development of domestic NFT limited to digital collections?
【微服务|Sentinel】实时监控|RT|吞吐量|并发数|QPS
Apk decompiled method (not confused)
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
placeholder
Custom animation (simulated win10 loading animation) - Optimization
Registration method of native method in JNI
EVM简略
Applet opening traffic master
@mysql
C# 闭包的垃圾回收
Use of file class filenamefilter & filefilter in io