Qt5開發(fā)及實(shí)例(第2版)-第5章Qt課件_第1頁
Qt5開發(fā)及實(shí)例(第2版)-第5章Qt課件_第2頁
Qt5開發(fā)及實(shí)例(第2版)-第5章Qt課件_第3頁
Qt5開發(fā)及實(shí)例(第2版)-第5章Qt課件_第4頁
Qt5開發(fā)及實(shí)例(第2版)-第5章Qt課件_第5頁
已閱讀5頁,還剩53頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、第5章 Qt5主窗體Qt 5主窗體構(gòu)成5.15.25.35.45.5Qt 5文件操作功能Qt 5圖像坐標(biāo)變換Qt 5文本編輯功能Qt 5排版功能5.1 Qt 5主窗體構(gòu)成5.1.1 基本元素QMainWindow是一個(gè)為用戶提供主窗口程序的類,包含一個(gè)菜單欄(menu bar)、多個(gè)工具欄(tool bars)、多個(gè)錨接部件(dock widgets)、一個(gè)狀態(tài)欄(status bar)及一個(gè)中心部件(central widget),是許多應(yīng)用程序的基礎(chǔ),如文本編輯器、圖片編輯器等。本章將對(duì)此進(jìn)行詳細(xì)介紹。其界面布局如圖5.1所示。5.1.1 基本元素1菜單欄菜單是一系列命令的列表。為了實(shí)現(xiàn)菜

2、單、工具欄按鈕、鍵盤快捷方式等命令的一致性,Qt使用動(dòng)作(Action)來表示這些命令。Qt的菜單就是由一系列的QAction動(dòng)作對(duì)象構(gòu)成的列表,而菜單欄則是包容菜單的面板,它位于主窗口頂部、主窗口標(biāo)題欄的下面。一個(gè)主窗口最多只有一個(gè)菜單欄。2狀態(tài)欄狀態(tài)欄通常顯示GUI應(yīng)用程序的一些狀態(tài)信息,它位于主窗口的最底部。用戶可以在狀態(tài)欄上添加、使用Qt窗口部件。一個(gè)主窗口最多只有一個(gè)狀態(tài)欄。5.1.1 基本元素3工具欄工具欄是由一系列的類似于按鈕的動(dòng)作排列而成的面板,它通常由一些經(jīng)常使用的命令(動(dòng)作)組成。工具欄位于菜單欄的下面、狀態(tài)欄的上面,可以??吭谥鞔翱诘纳?、下、左、右四個(gè)方向上。一個(gè)主窗口可

3、以包含多個(gè)工具欄。4錨接部件錨接部件作為一個(gè)容器使用,以包容其他窗口部件來實(shí)現(xiàn)某些功能。例如,Qt設(shè)計(jì)器的屬性編輯器、對(duì)象監(jiān)視器等都是由錨接部件包容其他的Qt窗口部件來實(shí)現(xiàn)的。它位于工具欄區(qū)的內(nèi)部,可以作為一個(gè)窗口自由地浮動(dòng)在主窗口上面,也可以像工具欄一樣停靠在主窗口的上、下、左、右四個(gè)方向上,一個(gè)主窗口可以包含多個(gè)錨接部件。5中心部件中心部件處在錨接部件區(qū)的內(nèi)部、主窗口的中心,一個(gè)主窗口只有一個(gè)中心部件。5.1.2 【綜合實(shí)例】:文本編輯器(1)新建Qt Widgets Application(詳見1.3.1節(jié)),項(xiàng)目名稱為“ImageProcessor”,基類選擇“QMainWindow”

4、,類名命名為“ImgProcessor”,取消“創(chuàng)建界面”復(fù)選框的選中狀態(tài)。單擊“下一步”按鈕,最后單擊“完成”按鈕,完成該項(xiàng)目工程的建立。(2)添加該工程的提供主要顯示文本編輯框函數(shù)所在的文件,在“ImageProcessor”項(xiàng)目名上單擊鼠標(biāo)右鍵,在彈出的快捷菜單中選擇“添加新文件.”菜單項(xiàng),在彈出的對(duì)話框中選擇“C+ Class”選項(xiàng),單擊“Choose.”按鈕,彈出對(duì)話框,在“Base class”后面的下拉列表框中選擇基類名“QWidget”,在“Class name”后面的文本框中輸入類的名稱“ShowWidget”。(3)單擊“下一步”按鈕,單擊“完成”按鈕,添加“showwid

5、get.h”頭文件和“showwidget.cpp”源文件。5.1.2 【綜合實(shí)例】:文本編輯器(4)打開“showwidget.h”頭文件,具體代碼如下:#include #include #include #include class ShowWidget : public QWidget Q_OBJECTpublic: explicit ShowWidget(QWidget *parent = 0); QImage img; QLabel *imageLabel; QTextEdit *text;signals:public slots:;5.1.2 【綜合實(shí)例】:文本編輯器(5)打開“

6、showwidget.cpp”文件,添加如下代碼:#include showwidget.h#include ShowWidget:ShowWidget(QWidget *parent):QWidget(parent) imageLabel =new QLabel; imageLabel-setScaledContents(true); text =new QTextEdit; QHBoxLayout *mainLayout =new QHBoxLayout(this); mainLayout-addWidget(imageLabel); mainLayout-addWidget(text);

7、5.1.2 【綜合實(shí)例】:文本編輯器(6)主函數(shù)ImgProcessor類聲明中createActions()函數(shù)用于創(chuàng)建所有的動(dòng)作、createMenus()函數(shù)用于創(chuàng)建菜單、createToolBars()函數(shù)用于創(chuàng)建工具欄;接著聲明實(shí)現(xiàn)主窗口所需的各個(gè)元素,包括菜單、工具欄及各個(gè)動(dòng)作等;最后聲明用到的槽函數(shù),打開“imgprocessor.h”文件,添加代碼。5.1.2 【綜合實(shí)例】:文本編輯器(7)下面是主窗口構(gòu)造函數(shù)部分的內(nèi)容,構(gòu)造函數(shù)主要實(shí)現(xiàn)窗體的初始化,打開“imgprocessor.cpp”文件,添加如下代碼:ImgProcessor:ImgProcessor(QWidget

8、*parent) : QMainWindow(parent) setWindowTitle(tr(Easy Word);/設(shè)置窗體標(biāo)題 showWidget =new ShowWidget(this);/(a) setCentralWidget(showWidget); /* 創(chuàng)建動(dòng)作、菜單、工具欄的函數(shù) */ createActions(); createMenus(); createToolBars(); if(img.load(image.png) /在imageLabel對(duì)象中放置圖像 showWidget-imageLabel-setPixmap(QPixmap:fromImage(

9、img); 5.1.3 菜單與工具欄的實(shí)現(xiàn)1動(dòng)作(Action)的實(shí)現(xiàn)以下是實(shí)現(xiàn)基本文件操作的動(dòng)作(Action)的代碼。以下是實(shí)現(xiàn)打印文本和圖像、圖像縮放、旋轉(zhuǎn)和鏡像的動(dòng)作(Action)的代碼。2菜單(Menus)的實(shí)現(xiàn)在實(shí)現(xiàn)了各個(gè)動(dòng)作之后,需要將它們通過菜單、工具欄或快捷鍵的方式體現(xiàn)出來,以下是菜單的實(shí)現(xiàn)函數(shù)createMenus()代碼。3工具欄(ToolBars)的實(shí)現(xiàn)接下來實(shí)現(xiàn)相對(duì)應(yīng)的工具欄createToolBars(),主窗口的工具欄上可以有多個(gè)工具條,通常采用一個(gè)菜單對(duì)應(yīng)一個(gè)工具條的方式,也可根據(jù)需要進(jìn)行工具條的劃分。工具條是一個(gè)可移動(dòng)的窗口,它可停靠的區(qū)域由QToolBar

10、的allowAreas決定,包括Qt:LeftToolBarArea、Qt:RightToolBarArea、Qt:TopToolBarArea、Qt:BottomToolBarArea和Qt:AllToolBarAreas。默認(rèn)為Qt:AllToolBarAreas,啟動(dòng)后默認(rèn)出現(xiàn)于主窗口的頂部??赏ㄟ^調(diào)用setAllowAreas()函數(shù)來指定工具條可停靠的區(qū)域,例如:fileTool-setAllowedAreas(Qt:TopToolBarArea|Qt:LeftToolBarArea);此函數(shù)限定文件工具條只可出現(xiàn)在主窗口的頂部或左側(cè)。工具條也可通過調(diào)用setMovable()函數(shù)設(shè)

11、定可移動(dòng)性,例如:fileTool-setMovable(false);指定文件工具條不可移動(dòng),只出現(xiàn)于主窗口的頂部。5.1.3 菜單與工具欄的實(shí)現(xiàn)5.1.3 菜單與工具欄的實(shí)現(xiàn)將程序中用到的圖片保存到該工程下的debug文件夾中,運(yùn)行程序,結(jié)果如圖5.3所示。5.2 Qt 5文件操作功能5.2.1 新建文件下面將介紹如何實(shí)現(xiàn)新建一個(gè)空白文件的功能。(1)打開“imgprocessor.h”頭文件,添加“protected slots:”變量:protected slots: void ShowNewFile();(2)在createActions()函數(shù)的“新建動(dòng)作”最后添加事件關(guān)聯(lián):con

12、nect(NewFileAction,SIGNAL(triggered(),this,SLOT(ShowNewFile();(3)實(shí)現(xiàn)新建文件功能的函數(shù)ShowNewFile()如下:void ImgProcessor:ShowNewFile() ImgProcessor *newImgProcessor =new ImgProcessor; newImgProcessor-show();5.2.1 新建文件(4)運(yùn)行程序,單擊“文件”“新建”命令或單擊工具欄上的 按鈕,彈出新的文件編輯窗口,如圖5.4所示。5.2.2 打開文件(1)在“imgprocessor.h”頭文件中添加“protec

13、ted slots:”變量:void ShowOpenFile();(2)在createActions()函數(shù)的“打開動(dòng)作”最后添加事件關(guān)聯(lián):connect(openFileAction,SIGNAL(triggered(),this,SLOT(ShowOpenFile();(3)實(shí)現(xiàn)打開文件功能的函數(shù)ShowOpenFile()如下:void ImgProcessor:ShowOpenFile() fileName =QFileDialog:getOpenFileName(this); if(!fileName.isEmpty() if(showWidget-text-document()-

14、isEmpty() loadFile(fileName); else ImgProcessor *newImgProcessor =new ImgProcessor; newImgProcessor-show(); newImgProcessor-loadFile(fileName); 5.2.2 打開文件其中,loadFile()函數(shù)的實(shí)現(xiàn)如下,該函數(shù)利用QFile和QTextStream完成具體讀取文件內(nèi)容的工作:void ImgProcessor:loadFile(QString filename) printf(file name:%sn,filename.data(); QFile

15、file(filename); if(file.open(QIODevice:ReadOnly|QIODevice:Text) QTextStream textStream(&file); while(!textStream.atEnd() showWidget-text-append(textStream.readLine(); printf(read linen); printf(endn); 5.2.2 打開文件在此僅詳細(xì)說明標(biāo)準(zhǔn)文件對(duì)話框QFileDialog的getOpenFileName()靜態(tài)函數(shù)各個(gè)參數(shù)的作用,其他文件對(duì)話框類中相關(guān)的靜態(tài)函數(shù)的參數(shù)有與其類似之處。QString

16、 QFileDialog:getOpenFileName(QWidget* parent=0,/定義標(biāo)準(zhǔn)文件對(duì)話框的父窗口const QString & caption=QString(),/定義標(biāo)準(zhǔn)文件對(duì)話框的標(biāo)題名const QString & dir=QString(),/(a) const QString & filter=QString(),/(b)QString * selectedFilter=0,/用戶選擇的過濾器通過此參數(shù)返回Options options=0);5.2.2 打開文件(4)在該源文件的開始部分添加如下頭文件:#include #include #include

17、 (5)運(yùn)行程序,單擊“文件”“打開”命令或單擊工具欄上的 按鈕,彈出“打開”對(duì)話框,如圖5.5(a)所示。選擇某個(gè)文件,單擊“打開”按鈕,文本編輯框中將顯示出該文件的內(nèi)容,如圖5.5(b)所示。5.2.3 打印文件1文本打印打印文本在文本編輯工作中經(jīng)常使用,下面將介紹如何實(shí)現(xiàn)文本打印功能。實(shí)現(xiàn)后的效果如圖5.6所示。5.2.3 打印文件具體實(shí)現(xiàn)步驟如下。(1)在頭文件中添加“protected slots:”變量:void ShowPrintText();(2)在createActions()函數(shù)的“打印文本動(dòng)作”最后添加事件關(guān)聯(lián):connect(PrintTextAction,SIGNAL

18、(triggered(),this,SLOT(ShowPrintText();(3)實(shí)現(xiàn)打印文本功能的函數(shù)ShowPrintText ()如下:void ImgProcessor:ShowPrintText() QPrinter printer;/新建一個(gè)QPrinter對(duì)象 QPrintDialog printDialog(&printer,this);/(a) if(printDialog.exec()/(b) /獲得QTextEdit對(duì)象的文檔 QTextDocument *doc =showWidget-text-document(); doc-print(&printer);/打印

19、5.2.3 打印文件(4)在該源文件的開始部分添加如下頭文件:#include #include (5)運(yùn)行程序,單擊“文件”“打印文本”命令或工具欄上的 按鈕,彈出標(biāo)準(zhǔn)打印對(duì)話框,如圖5.6所示。5.2.3 打印文件2圖像打印(1)在頭文件中添加“protected slots:”變量:void ShowPrintImage();(2)在createActions()函數(shù)的最后添加事件關(guān)聯(lián):connect(PrintImageAction,SIGNAL(triggered(),this,SLOT(ShowPrintImage();5.2.3 打印文件(3)實(shí)現(xiàn)打印圖像功能的函數(shù)ShowPri

20、ntImage ()如下:void ImgProcessor:ShowPrintImage() QPrinter printer;/新建一個(gè)QPrinter對(duì)象 QPrintDialog printDialog(&printer,this);/(a) if(printDialog.exec()/(b) QPainter painter(&printer);/(c) QRect rect =painter.viewport();/獲得QPainter對(duì)象的視圖矩形區(qū)域 QSize size = img.size();/獲得圖像的大小 /* 按照?qǐng)D形的比例大小重新設(shè)置視圖矩形區(qū)域 */ size.

21、scale(rect.size(),Qt:KeepAspectRatio); painter.setViewport(rect.x(),rect.y(),size.width(),size.height(); painter.setWindow(img.rect();/設(shè)置QPainter窗口大小為圖像的大小 painter.drawImage(0,0,img);/打印圖像 5.2.3 打印文件(4)在該源文件的開始部分添加如下頭文件:#include (5)運(yùn)行程序,單擊“文件”“打印圖像”命令或單擊工具欄上的 按鈕,彈出標(biāo)準(zhǔn)打印對(duì)話框,顯示效果如圖5.6所示。5.3 Qt 5圖像坐標(biāo)變換5

22、.3.1 縮放功能下面將介紹如何實(shí)現(xiàn)縮放功能,具體步驟如下。(1)在頭文件中添加“protected slots:”變量:void ShowZoomIn();(2)在createActions()函數(shù)的最后添加事件關(guān)聯(lián):connect(zoomInAction,SIGNAL(triggered(),this,SLOT(ShowZoomIn();(3)實(shí)現(xiàn)圖形放大功能的函數(shù)ShowZoomIn()如下:void ImgProcessor:ShowZoomIn() if(img.isNull()/有效性判斷 return; QMatrix martix;/聲明一個(gè)QMatrix類的實(shí)例 marti

23、x.scale(2,2);/(a) img = img.transformed(martix); /重新設(shè)置顯示圖形 showWidget-imageLabel-setPixmap(QPixmap:fromImage(img);5.3.1 縮放功能(4)在頭文件中添加“protected slots:”變量:void ShowZoomOut();(5)在createActions()函數(shù)的最后添加事件關(guān)聯(lián):connect(zoomOutAction,SIGNAL(triggered(),this,SLOT(ShowZoomOut();(6)實(shí)現(xiàn)圖形縮小功能的函數(shù)ShowZoomOut()如下:

24、void ImgProcessor:ShowZoomOut() if(img.isNull() return; QMatrix matrix; matrix.scale(0.5,0.5);/(a) img = img.transformed(matrix); showWidget-imageLabel-setPixmap(QPixmap:fromImage(img);5.3.1 縮放功能(7)運(yùn)行程序,單擊“編輯”“放大”命令或單擊工具欄上的 按鈕,界面效果如圖5.7所示。5.3.2 旋轉(zhuǎn)功能(1)在頭文件中添加“protected slots:”變量:void ShowRotate90();

25、(2)在createActions()函數(shù)的最后添加事件關(guān)聯(lián):connect(rotate90Action,SIGNAL(triggered(),this,SLOT(ShowRotate90();(3)ShowRotate90()函數(shù)的具體實(shí)現(xiàn)代碼如下:void ImgProcessor:ShowRotate90() if(img.isNull() return; QMatrix matrix; matrix.rotate(90); img = img.transformed(matrix); showWidget-imageLabel-setPixmap(QPixmap:fromImage(

26、img);5.3.2 旋轉(zhuǎn)功能(4)在頭文件中添加“protected slots:”變量:void ShowRotate180();void ShowRotate270();(5)在createActions()函數(shù)的最后添加事件關(guān)聯(lián):connect(rotate180Action,SIGNAL(triggered(),this,SLOT(ShowRotate180();connect(rotate270Action,SIGNAL(triggered(),this,SLOT(ShowRotate270();5.3.2 旋轉(zhuǎn)功能(6)ShowRotate180()、ShowRotate270(

27、)函數(shù)的具體實(shí)現(xiàn)代碼如下:void ImgProcessor:ShowRotate180() if(img.isNull() return; QMatrix matrix; matrix.rotate(180); img = img.transformed(matrix); showWidget-imageLabel-setPixmap(QPixmap:fromImage(img);void ImgProcessor:ShowRotate270() if(img.isNull() return; QMatrix matrix; matrix.rotate(270); img = img.tra

28、nsformed(matrix); showWidget-imageLabel-setPixmap(QPixmap:fromImage(img);5.3.2 旋轉(zhuǎn)功能(7)運(yùn)行程序,單擊“旋轉(zhuǎn)”“旋轉(zhuǎn)90”命令或單擊工具欄上的 按鈕,圖像旋轉(zhuǎn)效果如圖5.8所示。5.3.3 鏡像功能(1)在頭文件中添加“protected slots:”變量:void ShowMirrorVertical();void ShowMirrorHorizontal();(2)在createActions()函數(shù)的最后添加事件關(guān)聯(lián):connect(mirrorVerticalAction,SIGNAL(trigger

29、ed(),this,SLOT(ShowMirror Vertical();connect(mirrorHorizontalAction,SIGNAL(triggered(),this,SLOT(ShowMirrorHorizontal();5.3.3 鏡像功能(3)ShowMirrorVertical ()、ShowMirrorHorizontal ()函數(shù)的具體實(shí)現(xiàn)代碼如下:void ImgProcessor:ShowMirrorVertical() if(img.isNull() return; img=img.mirrored(false,true); showWidget-imageL

30、abel-setPixmap(QPixmap:fromImage(img);void ImgProcessor:ShowMirrorHorizontal() if(img.isNull() return; img=img.mirrored(true,false); showWidget-imageLabel-setPixmap(QPixmap:fromImage(img);5.3.3 鏡像功能(4)此時(shí)運(yùn)行程序,單擊“鏡像”“橫向鏡像”命令,蝴蝶翅膀底部的陰影從右邊移到左邊,顯示效果如圖5.9所示。5.4 Qt 5文本編輯功能各類之間的劃分與關(guān)系如圖5.10所示。5.4 Qt 5文本編輯功能實(shí)

31、現(xiàn)文本編輯的具體操作步驟如下。(1)在頭文件中添加“private:”變量:QLabel *fontLabel1; /字體設(shè)置項(xiàng)QFontComboBox *fontComboBox;QLabel *fontLabel2;QComboBox *sizeComboBox;QToolButton *boldBtn;QToolButton *italicBtn;QToolButton *underlineBtn;QToolButton *colorBtn;QToolBar *fontToolBar; /字體工具欄5.4 Qt 5文本編輯功能(2)在頭文件中添加“protected slots:”變量

32、:void ShowFontComboBox(QString comboStr);void ShowSizeSpinBox(QString spinValue);void ShowBoldBtn();void ShowItalicBtn();void ShowUnderlineBtn();void ShowColorBtn();void ShowCurrentFormatChanged(const QTextCharFormat &fmt);(3)在相對(duì)應(yīng)的構(gòu)造函數(shù)中,在語句“setCentralWidget(showWidget);”與語句“createActions();”之間添加代碼。5

33、.4 Qt 5文本編輯功能(4)在該構(gòu)造函數(shù)的最后部分添加相關(guān)的事件關(guān)聯(lián):connect(fontComboBox,SIGNAL(activated(QString), this,SLOT(ShowFontComboBox(QString);connect(sizeComboBox,SIGNAL(activated(QString), this,SLOT(ShowSizeSpinBox(QString);connect(boldBtn,SIGNAL(clicked(),this,SLOT(ShowBoldBtn();connect(italicBtn,SIGNAL(clicked(),this

34、,SLOT(ShowItalicBtn();connect(underlineBtn,SIGNAL(clicked(),this,SLOT(ShowUnderlineBtn();connect(colorBtn,SIGNAL(clicked(),this,SLOT(ShowColorBtn();connect(showWidget-text,SIGNAL(currentCharFormatChanged(QtextChar Format&),this,SLOT(ShowCurrentFormatChanged(QTextCharFormat&);5.4 Qt 5文本編輯功能(5)在相對(duì)應(yīng)的工具

35、欄createToolBars()函數(shù)中添加如下代碼:/字體工具條fontToolBar =addToolBar(Font);fontToolBar-addWidget(fontLabel1);fontToolBar-addWidget(fontComboBox);fontToolBar-addWidget(fontLabel2);fontToolBar-addWidget(sizeComboBox);fontToolBar-addSeparator();fontToolBar-addWidget(boldBtn);fontToolBar-addWidget(italicBtn);fontTo

36、olBar-addWidget(underlineBtn);fontToolBar-addSeparator();fontToolBar-addWidget(colorBtn);5.4.1 設(shè)置字體完成設(shè)置選定文字字體的函數(shù)ShowFontComboBox()代碼如下:void ImgProcessor:ShowFontComboBox(QString comboStr)/設(shè)置字體 QTextCharFormat fmt; /創(chuàng)建一個(gè)QTextCharFormat對(duì)象 fmt.setFontFamily(comboStr); /選擇的字體名稱設(shè)置給QTextCharFormat對(duì)象 merge

37、Format(fmt); /將新的格式應(yīng)用到光標(biāo)選區(qū)內(nèi)的字符前面介紹過,所有對(duì)于QTextDocument進(jìn)行的修改都通過QTextCursor類來完成,具體代碼如下:void ImgProcessor:mergeFormat(QTextCharFormat format) QTextCursor cursor =showWidget-text-textCursor();/獲得編輯框中的光標(biāo) if(!cursor.hasSelection()/(a) cursor.select(QTextCursor:WordUnderCursor); cursor.mergeCharFormat(forma

38、t);/(b) showWidget-text-mergeCurrentCharFormat(format);/(c)5.4.2 設(shè)置字號(hào)設(shè)置選定文字字號(hào)大小的ShowSizeSpinBox()函數(shù)代碼如下:void ImgProcessor:ShowSizeSpinBox(QString spinValue)/設(shè)置字號(hào) QTextCharFormat fmt; fmt.setFontPointSize(spinValue.toFloat(); showWidget-text-mergeCurrentCharFormat(fmt);5.4.3 設(shè)置文字加粗設(shè)置選定文字為加粗顯示的ShowBol

39、dBtn()函數(shù)代碼如下:void ImgProcessor:ShowBoldBtn() /設(shè)置文字顯示加粗 QTextCharFormat fmt; fmt.setFontWeight(boldBtn-isChecked()?QFont:Bold:QFont: Normal); showWidget-text-mergeCurrentCharFormat(fmt);5.4.4 設(shè)置字體傾斜設(shè)置選定文字為斜體顯示的ShowItalicBtn()函數(shù)代碼如下:void ImgProcessor:ShowItalicBtn()/設(shè)置文字顯示斜體 QTextCharFormat fmt; fmt.s

40、etFontItalic(italicBtn-isChecked(); showWidget-text-mergeCurrentCharFormat(fmt);5.4.5 文字加下畫線在選定文字下方加下畫線的ShowUnderlineBtn()函數(shù)代碼如下:void ImgProcessor:ShowUnderlineBtn()/設(shè)置文字加下畫線 QTextCharFormat fmt; fmt.setFontUnderline(underlineBtn-isChecked(); showWidget-text-mergeCurrentCharFormat(fmt);5.4.6 設(shè)置文字顏色設(shè)

41、置選定文字顏色的ShowColorBtn()函數(shù)代碼如下:void ImgProcessor:ShowColorBtn()/設(shè)置文字顏色QColor color=QColorDialog:getColor(Qt:red,this);/(a)if(color.isValid() QTextCharFormat fmt; fmt.setForeground(color); showWidget-text-mergeCurrentCharFormat(fmt); 在頭文件中添加聲明:#include #include 5.4.6 設(shè)置文字顏色標(biāo)準(zhǔn)顏色對(duì)話框QColorDialog類的使用:QColo

42、r getColor( const QColor& initial=Qt:white, QWidget* parent=0); 第1個(gè)參數(shù)指定了選中的顏色,默認(rèn)為白色。5.4.7 設(shè)置字符格式當(dāng)光標(biāo)所在處的字符格式發(fā)生變化時(shí)調(diào)用此槽函數(shù),函數(shù)根據(jù)新的字符格式將工具欄上各個(gè)格式控件的顯示更新。void ImgProcessor:ShowCurrentFormatChanged(const QTextCharFormat &fmt)fontComboBox-setCurrentIndex(fontComboBox-findText(fmt .fontFamily(); sizeComboBox-s

43、etCurrentIndex(sizeComboBox-findText( QString:number(fmt.fontPointSize(); boldBtn-setChecked(fmt.font().bold(); italicBtn-setChecked(fmt.fontItalic(); underlineBtn-setChecked(fmt.fontUnderline();5.5 Qt 5排版功能具體實(shí)現(xiàn)步驟如下。(1)在頭文件中添加“private:”變量:QLabel *listLabel; /排序設(shè)置項(xiàng)QComboBox *listComboBox;QActionGroup

44、 *actGrp;QAction *leftAction;QAction *rightAction;QAction *centerAction;QAction *justifyAction;QToolBar *listToolBar; /排序工具欄(2)在頭文件中添加“protected slots:”變量:void ShowList(int);void ShowAlignment(QAction *act);void ShowCursorPositionChanged();5.5 Qt 5排版功能(3)在相對(duì)應(yīng)的構(gòu)造函數(shù)中,在語句“setCentralWidget(showWidget);”

45、與語句“createActions();”之間添加如下代碼:/排序listLabel =new QLabel(tr(排序); listComboBox =new QComboBox;listComboBox-addItem(Standard);listComboBox-addItem(QTextListFormat:ListDisc);listComboBox-addItem(QTextListFormat:ListCircle);listComboBox-addItem(QTextListFormat:ListSquare);listComboBox-addItem(QTextListFor

46、mat:ListDecimal);listComboBox-addItem(QTextListFormat:ListLowerAlpha);listComboBox-addItem(QTextListFormat:ListUpperAlpha);listComboBox-addItem(QTextListFormat:ListLowerRoman);listComboBox-addItem(QTextListFormat:ListUpperRoman);5.5 Qt 5排版功能(4)在構(gòu)造函數(shù)的最后部分添加相關(guān)的事件關(guān)聯(lián):connect(listComboBox,SIGNAL(activate

47、d(int),this,SLOT(ShowList(int);connect(showWidget-text-document(),SIGNAL(undoAvailable(bool), redoAction,SLOT(setEnabled(bool);connect(showWidget-text-document(),SIGNAL(redoAvailable(bool), redoAction,SLOT(setEnabled(bool);connect(showWidget-text,SIGNAL(cursorPositionChanged(), this,SLOT(ShowCursorP

48、ositionChanged();5.5 Qt 5排版功能(5)在相對(duì)應(yīng)的工具欄createActions()函數(shù)中添加如下代碼:/排序:左對(duì)齊、右對(duì)齊、居中和兩端對(duì)齊actGrp =new QActionGroup(this);leftAction =new QAction(QIcon(left.png),左對(duì)齊,actGrp);leftAction-setCheckable(true);rightAction =new QAction(QIcon(right.png),右對(duì)齊,actGrp);rightAction-setCheckable(true);centerAction =new

49、QAction(QIcon(center.png),居中,actGrp);centerAction-setCheckable(true);justifyAction =new QAction(QIcon(justify.png),兩端對(duì)齊,actGrp);justifyAction-setCheckable(true);connect(actGrp,SIGNAL(triggered(QAction*),this,SLOT(ShowAlignment (QAction*);5.5 Qt 5排版功能(6)在相對(duì)應(yīng)的工具欄createToolBars()函數(shù)中添加如下代碼:/排序工具條listToolBar =addToolBar(list);listToolBar-addWidget(listLabel);listToolBar-addWidget(listComboBox);listToolBar-addSeparator(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論