1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/qtguide-qtguide

Клонировать/Скачать
ch11-05.htm 120 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
winland Отправлено 16.06.2025 16:44 2b2d4b7
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>ch11-05</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="thumbnailviewer.css" type="text/css">
<script src="thumbnailviewer.js" type="text/javascript">
/***********************************************
* Image Thumbnail Viewer Script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
</script> </head>
<body>
<div class="os1">11.5 通用对话框:QMessageBox、QWizard</div>
<br>
本节介绍通用对话框:QMessageBox、QWizard,并通过示例程序展示消息对话框和向导对话框的各自使用方法。 <br>
<br>
<div class="os2">11.5.1 QMessageBox</div>
<br>
QMessageBox 消息框一般作为模态对话框,告知用户信息、提供选项并接收用户反馈。消息框同时提供三种文本显示:primary
text(基本文本,必须有的,显示情况信息)、informative text(信息文本,向用户解释更多情况或提出问题)、detailed
text(详细文本,可选的,根据用户需要显示更多数据信息),这些文本都是用于提供信息给用户理解消息框提出的问题,用户理解后作出相应的选择。消
息框还提供图标和一系 列标准按钮,程序员根据需要设置图标和按
钮,用户点击按钮后,消息框返回用户点击的按钮数值。消息框可以使用自定义对象的方式,也可以直接调用定制好的静态函数弹窗。我们先介绍消息框的普通成员函数和其
主要属性,然后介绍静态函数。<br>
(1)普通成员函数<br>
QMessageBox 基类是 QDialog 对话框类,QMessageBox 构造函数如下:<br>
<div class="code">QMessageBox(QWidget * parent = 0)<br>
QMessageBox(Icon icon, const QString &amp; title, const QString &amp;
text, StandardButtons buttons = NoButton, QWidget * parent = 0,
Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)</div>
第一个是默认构造函数,只有 parent&nbsp; 父窗口参数。第二个是带有详细参数的构造函数,参数 icon 是消息框显示的图标;title
是标题栏文本;text 是基本文本,显示展示给用户的信息内容;buttons 是消息框显示的按钮,可以通过 |
位或操作指定多个按钮,构造函数里的NoButton 是指不设置按钮,默认用 Ok 按钮;parent 是父窗口参数; f
是窗口标志位,Qt::Dialog 代表是对话框标志,窗口右上角没有最大化、最小化按钮,Qt::MSWindowsFixedSizeDialogHint
是指 Windows 的固定尺寸对话框的边框风格。<br>
<br>
针对消息框的按钮使用,有多种添加、设置和访问函数。<br>
首先是添加按钮函数:<br>
<div class="code">void&nbsp;&nbsp; &nbsp;addButton(QAbstractButton * button,
ButtonRole role)<br>
QPushButton *&nbsp;&nbsp;&nbsp; addButton(const QString &amp; text,
ButtonRole role)<br>
QPushButton *&nbsp;&nbsp;&nbsp; addButton(StandardButton button)</div>
第一个添加按钮函数,参数为自定义的按钮对象指针,role 是按钮的角色类型。<br>
第二个添加按钮函数,根据文本新建按钮添加到消息框,并返回新建的按钮对象指针。<br>
第三个添加按钮函数,根据标志位数值,添加标准按钮,并返回新建的按钮对象指针。<br>
按钮角色是枚举类型,具体如下表:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 220px;" align="center"><b>QMessageBox::ButtonRole</b></td>
<td style="width: 100px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>QMessageBox::InvalidRole</td>
<td> -1</td>
<td> 不可用角色,该按钮不可用。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::AcceptRole</td>
<td> 0</td>
<td> 接收角色,点击该按钮导致对话框被接受,例如 OK 按钮。 </td>
</tr>
<tr>
<td>QMessageBox::RejectRole</td>
<td> 1 </td>
<td> 拒绝角色,点击该按钮导致对话框被拒绝,例如 Cancel 按钮。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::DestructiveRole</td>
<td> 2 </td>
<td> 放弃角色,点击该按钮导致放弃变更并关闭对话框,例如 放弃变更、不保存就退出。 </td>
</tr>
<tr>
<td>QMessageBox::ActionRole</td>
<td> 3 </td>
<td> 动作角色,点击该按钮导致对话框元素做变动动作。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::HelpRole</td>
<td> 4</td>
<td> 帮助角色,点击该按钮请求帮助。 </td>
</tr>
<tr>
<td>QMessageBox::YesRole</td>
<td> 5</td>
<td> 同意角色,表示 Yes 意思的按钮。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::NoRole</td>
<td> 6</td>
<td> 否定角色,表示 No 意思的按钮。 </td>
</tr>
<tr>
<td>QMessageBox::ApplyRole</td>
<td> 8</td>
<td> 应用角色,应用当前的变更操作。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::ResetRole</td>
<td> 7</td>
<td> 重置角色,将对话框元素重置为默认值。 </td>
</tr>
</tbody>
</table>
<br>
点击不同角色的按钮,对话框的动作不同,并且影响对话框返回值,要根据实际需要配置按钮角色。<br>
消息框定义了很多标准按钮,可以通过 | 位或运算符设置多个标准按钮,标准按钮的标志位如下表所示:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 220px;" align="center"><b>QMessageBox::StandardButtons</b></td>
<td style="width: 100px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>QMessageBox::Ok</td>
<td> 0x00000400 </td>
<td> OK 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Open</td>
<td> 0x00002000 </td>
<td> Open 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::Save</td>
<td> 0x00000800 </td>
<td> Save 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Cancel</td>
<td> 0x00400000 </td>
<td> Cancel 按钮,定义为 RejectRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::Close</td>
<td> 0x00200000 </td>
<td> Close 按钮,定义为 RejectRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Discard</td>
<td> 0x00800000 </td>
<td> Discard 或 Don't Save 按钮,依赖系统平台,定义为 DestructiveRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::Apply</td>
<td> 0x02000000 </td>
<td> Apply 按钮,定义为 ApplyRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Reset</td>
<td> 0x04000000 </td>
<td> Reset 按钮,定义为 ResetRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::RestoreDefaults</td>
<td> 0x08000000 </td>
<td> Restore Defaults 按钮,定义为 ResetRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Help</td>
<td> 0x01000000 </td>
<td> Help 按钮,定义为 HelpRole 角色。</td>
</tr>
<tr>
<td>QMessageBox::SaveAll</td>
<td> 0x00001000 </td>
<td> Save All 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Yes</td>
<td> 0x00004000 </td>
<td> Yes 按钮,定义为 YesRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::YesToAll</td>
<td> 0x00008000 </td>
<td> Yes to All 按钮,定义为 YesRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::No</td>
<td> 0x00010000 </td>
<td> No 按钮,定义为 NoRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::NoToAll</td>
<td> 0x00020000 </td>
<td> No to All 按钮,定义为 NoRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Abort</td>
<td> 0x00040000 </td>
<td> Abort 按钮,定义为 RejectRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::Retry</td>
<td> 0x00080000 </td>
<td> Retry 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr class="d1">
<td>QMessageBox::Ignore</td>
<td> 0x00100000 </td>
<td> Ignore 按钮,定义为 AcceptRole 角色。 </td>
</tr>
<tr>
<td>QMessageBox::NoButton</td>
<td> 0x00000000 </td>
<td> 不可用按钮 </td>
</tr>
</tbody>
</table>
<br>
根据标志位,可以一次设置多个标准按钮,如下函数:<br>
<div class="code">void&nbsp;&nbsp; &nbsp;setStandardButtons(StandardButtons
buttons)&nbsp;&nbsp; //设置标准按钮<br>
StandardButtons&nbsp;&nbsp;&nbsp; standardButtons()
const&nbsp;&nbsp;&nbsp; //获取设置好的标准按钮标志位<br>
StandardButton&nbsp;&nbsp;&nbsp; standardButton(QAbstractButton * button)
const&nbsp; //根据按钮对象指针获取按钮对应的标准按钮标志位</div>
setStandardButtons() 函数根据参数里标志位一次设置多个按钮,standardButtons() 则返回设置好的按钮标志位。<br>
standardButton() 是根据参数里的按钮对象指针,返回该按钮对应的标准按钮标志位,如果对象不是标准按钮,返回
QMessageBox::NoButton&nbsp;<br>
注意不要调用 msgBox.setStandardButtons(QMessageBox::NoButton) ,消息框显示时会没有任何按钮,消息框可能
出现关不掉的情况,程序界面会卡住无法操作,一般默认用 QMessageBox::Ok 按钮即可。<br>
<br>
按钮角色可以读取,如下函数:<br>
<div class="code">ButtonRole&nbsp;&nbsp;&nbsp; buttonRole(QAbstractButton *
button) const<br>
</div>
按钮角色得在添加按钮 addButton() 时指定,目前没有直接修改按钮角色的函数。<br>
从消息框卸载按钮使用如下函数:<br>
<div class="code">void&nbsp;&nbsp;&nbsp; removeButton(QAbstractButton *
button)<br>
</div>
注意 removeButton() 函数只是将消息框的按钮卸下,并不会删除该按钮对象。<br>
如果需要修改按钮角色,可以先卸载按钮,再用新的角色添加给消息框。<br>
获取消息框已设置的所有按钮对象列表,使用如下函数:<br>
<div class="code">QList&lt;QAbstractButton *&gt;&nbsp;&nbsp;&nbsp; buttons()
const</div>
<br>
当消息框有多个按钮时,可以指定默认按钮(默认对应 Enter 键)、退出按钮(对应 Esc/Escape 键),如下函数:<br>
<div class="code">void&nbsp;&nbsp; &nbsp;setDefaultButton(QPushButton *
button)&nbsp;&nbsp; //根据按钮指针设置默认按钮<br>
void&nbsp;&nbsp;&nbsp; setDefaultButton(StandardButton button)
//根据标准按钮标志位设 置默认按钮<br>
QPushButton *&nbsp;&nbsp;&nbsp; defaultButton() const &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; //获取默认按钮对象指针<br>
void&nbsp;&nbsp;&nbsp; setEscapeButton(QAbstractButton *
button)&nbsp;&nbsp; //根据按钮指针设置退出按钮<br>
void&nbsp;&nbsp;&nbsp; setEscapeButton(StandardButton
button)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //根据标准按钮标志位设置退出按钮<br>
QAbstractButton *&nbsp;&nbsp;&nbsp; escapeButton()
const&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//获取退出按钮对象指针</div>
注意检查参数和返回值的有效性,避免操作空指针。<br>
<br>
根据用户的操作,可以获取用户点击的按钮对象,或者返回 0 (NULL):<br>
<div class="code">QAbstractButton *&nbsp;&nbsp;&nbsp; clickedButton() const<br>
</div>
用户如果点击了按钮,就返回点击的按钮对象;如果没有设置退出键时用户按下键盘 Esc 键,返回 0 。<br>
在未调用 exec() 显示消息框时, clickedButton() 也返回 0 。<br>
消息框还可以添加一个复选框,比如用于设置是否在文件关闭前自动保存修改内容,注意复选框对象要自己新建,消息框默认并没有复选框。设置和获取复选框 对象函数如
下:<br>
<div class="code">QCheckBox *&nbsp;&nbsp;&nbsp; checkBox()
const&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //获取复选框对象指针,默认为 NULL<br>
void&nbsp;&nbsp;&nbsp; setCheckBox(QCheckBox * cb)&nbsp;&nbsp; //手动新建复选框对象
cb,添加给消息框<br>
</div>
&nbsp; checkBox() 默认是 NULL,消息框自己不会创建复选框,必须自己手动新建一个复选框对象,用setCheckBox( cb )
函数设置复选框对象。<br>
一个典型的消息框对象使用如下代码示例:<br>
<div class="code">&nbsp;&nbsp;&nbsp; QMessageBox msgBox;<br>
&nbsp;&nbsp;&nbsp; msgBox.setText(tr("文本已经修改。"));&nbsp;&nbsp; //基本文本设置<br>
&nbsp;&nbsp;&nbsp; msgBox.setInformativeText(tr("是否保存修改后的文本?"));&nbsp;
//信息文本设置<br>
&nbsp;&nbsp;&nbsp;
msgBox.setDetailedText(tr("勾选上面复选框,设置总是自动保存修改的文本。"));&nbsp; //详细文本设置<br>
&nbsp;&nbsp;&nbsp; msgBox.setStandardButtons(QMessageBox::Save |
QMessageBox::Discard | QMessageBox::Cancel);<br>
&nbsp;&nbsp;&nbsp; msgBox.setDefaultButton(QMessageBox::Save);<br>
&nbsp;&nbsp;&nbsp; //补充复选框<br>
&nbsp;&nbsp;&nbsp; QCheckBox *pCheckBox = new
QCheckBox(tr("关闭前总是自动保存修改文本。"));<br>
&nbsp;&nbsp;&nbsp; msgBox.setCheckBox(pCheckBox);<br>
&nbsp;&nbsp;&nbsp; int ret = msgBox.exec();<br>
&nbsp;&nbsp;&nbsp; if(NULL != msgBox.checkBox())<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool bAlwaysSave =
msgBox.checkBox()-&gt;isChecked();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(bAlwaysSave)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
qDebug()&lt;&lt;tr("关闭前总是自动保存修改文本。");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
qDebug()&lt;&lt;tr("关闭前不自动保存修改文本。");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
</div>
显示效果如下图所示:<br>
<center> <img src="images/ch11/ch11-05-01.png" alt="msgboxshow1"></center>
消息框第一行显示的是基本文本,第二行是信息文本,当设置了详细文本后,消息框自动添加“Show Details”按钮,点击该按钮就会展开下方的文本框显示详
细文本。<br>
添加复选框之后,复选框显示在第三行,可以访问复选框对象,获取用户是否选中了复选框,以进行后续操作。<br>
消息框的三种文本访问和设置函数如下:<br>
<div class="code">QString&nbsp;&nbsp;&nbsp; text() const&nbsp;&nbsp; //获取基本文
<br>
void&nbsp;&nbsp;&nbsp; setText(const QString &amp; text)&nbsp; //设置基本文本<br>
QString&nbsp;&nbsp;&nbsp; informativeText() const&nbsp;&nbsp; //获取信息文本<br>
void&nbsp;&nbsp;&nbsp; setInformativeText(const QString &amp;
text)&nbsp;&nbsp;&nbsp; //设置信息文本<br>
QString&nbsp;&nbsp;&nbsp; detailedText() const&nbsp; &nbsp; &nbsp;&nbsp;
//获取详细文本<br>
void&nbsp;&nbsp;&nbsp; setDetailedText(const QString &amp;
text)&nbsp;&nbsp;&nbsp; //设置详细文本<br>
</div>
设置详细文本后,消息框自动添加“Show Details”按钮,如果没有设置详细文本,消息框就没有“Show Details”按钮。<br>
消息框文本可以设置文本格式,如下函数:<br>
<div class="code">Qt::TextFormat&nbsp;&nbsp;&nbsp; textFormat()
const&nbsp;&nbsp;&nbsp; //获取文本格式<br>
void&nbsp;&nbsp;&nbsp; setTextFormat(Qt::TextFormat format)&nbsp;&nbsp;
//设置文本格式<br>
</div>
Qt::TextFormat 枚举类型内容比较简单,三种取值如下表所示:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 220px;" align="center"><b>Qt::TextFormat 枚举常量</b></td>
<td style="width: 140px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>Qt::PlainText</td>
<td>&nbsp;0</td>
<td> 设置的文本理解为纯文本,不解析 HTML 语法。 </td>
</tr>
<tr class="d1">
<td>Qt::RichText</td>
<td>&nbsp;1 </td>
<td> 设置的文本理解为丰富文本,按照 HTML 语法解析呈现内容。 </td>
</tr>
<tr>
<td>Qt::AutoText</td>
<td>&nbsp;2</td>
<td> 自动判断文本类型,如果 Qt::mightBeRichText() 函数返回 true,当做丰富文本呈现,否则都当成纯文本。</td>
</tr>
</tbody>
</table>
<br>
默认文本格式为 Qt::AutoText ,自动判断设置文本类型。<br>
注意 setTextFormat() 函数只影响基本文本,不影响信息文本和详细文本。<br>
信息文本默认总是自动文本格式 Qt::AutoText,详细文本默认总是纯文本格式 Qt::PlainText。<br>
<br>
消息框的文本还可以设置文本交互操作的标志位,如下函数:<br>
<div class="code">Qt::TextInteractionFlags&nbsp;&nbsp;&nbsp;
textInteractionFlags() const&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
//获取文本交互标志位,默认值依赖 style 风格<br>
void&nbsp;&nbsp;&nbsp; setTextInteractionFlags(Qt::TextInteractionFlags
flags)&nbsp; //设置文本交互标志位<br>
</div>
&nbsp; Qt::TextInteractionFlags 标志位定义文本的交互性,比如是否可以复制,是否可以编辑,如下表所示:<br>
- setTextInteractionFlags() 只影响基本文本,不影响信息文本和详细文本。<br>
消息框除了显示文本,还可以选择图标配合显示,表示消息的类别,比如普通消息、警告消息、严重错误消息等类型。<br>
图标支持使用 QMessageBox 自带图标或者自定义图标,如下函数:<br>
<div class="code">void&nbsp;&nbsp;&nbsp; setIcon(Icon)&nbsp;&nbsp;&nbsp;
//设置 QMessageBox 自带的图标<br>
Icon&nbsp;&nbsp;&nbsp; icon() const&nbsp;&nbsp;&nbsp;&nbsp; //获取所用的
QMessageBox 自带图标<br>
void&nbsp;&nbsp;&nbsp; setIconPixmap(const QPixmap &amp;
pixmap)&nbsp;&nbsp; //设置自定义图片作为图标<br>
QPixmap&nbsp;&nbsp;&nbsp; iconPixmap() const&nbsp;&nbsp; //获取设置的自定义图标的图片<br>
</div>
默认情况下, iconPixmap() 是空的 null ,没有自定义图片文件。如果需要用自定义图片,根据图片文件名构建 QPixmap
对象,设置给消息框即可。<br>
icon() 默认情况下返回 0,就是 QMessageBox::NoIcon ,QMessageBox 自带的图标如下表所示:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 220px;" align="center"><b>QMessageBox::​Icon 枚举类型</b></td>
<td style="width: 120px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
<td align="center"><b> 图标示例</b></td>
</tr>
<tr>
<td>QMessageBox::NoIcon</td>
<td>&nbsp;0</td>
<td> 消息框没有任何图标。 </td>
<td></td>
</tr>
<tr class="d1">
<td> QMessageBox::Question</td>
<td>&nbsp;4 </td>
<td> 询问图标,说明提示的消息是向用户询问。 </td>
<td> <img src="images/ch11/iconquestion.png" alt="iconquestion"> </td>
</tr>
<tr>
<td>QMessageBox::Information</td>
<td>&nbsp;1</td>
<td> 信息图标,说明提示的消息是告诉用户信息。</td>
<td> <img src="images/ch11/iconinformation.png" alt="iconinformation">
</td>
</tr>
<tr class="d1">
<td>QMessageBox::Warning</td>
<td>&nbsp;2 </td>
<td> 警告图标,说明提示的消息是警告信息,但目前程序能够处理。 </td>
<td> <img src="images/ch11/iconwarning.png" alt="iconwarning"> </td>
</tr>
<tr>
<td>QMessageBox::Critical</td>
<td>&nbsp;3</td>
<td> 严重错误图标,说明程序发生了严重错误,很可能崩溃。</td>
<td> <img src="images/ch11/iconcritical.png" alt="iconcritical"> </td>
</tr>
</tbody>
</table>
<br>
上表图标示例仅供参考,实际运行时图标依赖系统平台的风格,不同操作系统显示图标不一样。<br>
消息框还可以修改窗口标题栏文本,设置模态或非模态显示,如下函数设置:<br>
<div class="code">void&nbsp;&nbsp;&nbsp; setWindowTitle(const QString &amp;
title)&nbsp;&nbsp; //设置消息框标题栏文本<br>
QString&nbsp;&nbsp;&nbsp; windowTitle()
const&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //获取消息框标题栏文本<br>
void&nbsp;&nbsp;&nbsp; setWindowModality(Qt::WindowModality
windowModality)&nbsp;&nbsp; //设置消息框显示模态类型<br>
Qt::WindowModality&nbsp;&nbsp;&nbsp; windowModality() const&nbsp;&nbsp;
//获取消息框的显示模态类型<br>
</div>
窗口的显示模态类型见 11.1.1 节QWidget 类的介绍,主要就是非模态 Qt::NonModal、Qt::WindowModal
窗口级模态、Qt::ApplicationModal 应用级模态三种。<br>
消息框还有一个特别的 open() 函数,可以指定接受信号的对象和槽函数:<br>
<div class="code">void&nbsp;&nbsp;&nbsp; open(QObject * receiver, const char
* member)<br>
</div>
该函数打开消息框,并将 finished() 或 buttonClicked() 信号关联到参数里对象槽函数,如果槽函数有一个指针参数,那么关联
buttonClicked() 信号,否则关联 finished() 信号到槽函数。<br>
注意消息窗口关闭后,自动解除信号和 open() 参数里对象的槽函数关联。<br>
<br>
消息框类本级的槽函数只有 1 个,如下函数:<br>
<div class="code">virtual int&nbsp;&nbsp;&nbsp; exec()&nbsp;&nbsp; //模态显示消息框<br>
</div>
消息框类本级的信号也只有 1 个,如下信号:<br>
<div class="code">void&nbsp;&nbsp;&nbsp; buttonClicked(QAbstractButton *
button)&nbsp; //点击按钮信号,参数里是被点击的按钮对象<br>
</div>
<br>
消息框主要从基类 QDialog 和 QWidget 继承各种信号和槽函数,详见 11.1 节和 11.2 节内容。<br>
<br>
(2)静态成员函数<br>
消息框可以通过定义对象、调用普通成员函数方式使用,也可以通过最简单的静态成员函数来使用。<br>
静态成员函数仅需一句代码即可完成调用,非常便捷。<br>
针对询问、信息、警告、严重错误四种情况,对应的静态成员函数如下:<br>
<div class="code">StandardButton&nbsp;&nbsp;&nbsp; question(QWidget *
parent, const QString &amp; title, const QString &amp; text,
StandardButtons buttons = StandardButtons( Yes | No ), StandardButton
defaultButton = NoButton)<br>
StandardButton&nbsp;&nbsp;&nbsp; information(QWidget * parent, const
QString &amp; title, const QString &amp; text, StandardButtons buttons =
Ok, StandardButton defaultButton = NoButton)<br>
StandardButton&nbsp;&nbsp;&nbsp; warning(QWidget * parent, const QString
&amp; title, const QString &amp; text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)<br>
StandardButton&nbsp;&nbsp;&nbsp; critical(QWidget * parent, const QString
&amp; title, const QString &amp; text, StandardButtons buttons = Ok,
StandardButton defaultButton = NoButton)<br>
</div>
上面静态函数的参数都很类似,title 是标题文本,text 是基本文本,buttons 是设置的标准按钮,询问时为 Yes 和 No
按钮,其他情况默认为 Ok 按钮,defaultButton 默认 NoButton;返回值就是用户点击的标准按钮数值。<br>
静态函数调用非常简单,如下示例:<br>
<div class="code">int ret = QMessageBox::warning(this, tr("My Application"),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
tr("The document has been modified.\n"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
"Do you want to save your changes?"),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
QMessageBox::Save | QMessageBox::Discard<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
| QMessageBox::Cancel,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
QMessageBox::Save);<br>
</div>
窗口标题文本 "My Application",基本文本 "The document has been modified.\nDo you want
to save your changes?" ,显示三个按钮 Save 、Discard、Cancel,默认焦点是 Save 按钮。<br>
<br>
另外,消息框还提供了两个用于显示本程序信息和 Qt 版本信息的静态函数:<br>
<div class="code">void&nbsp;&nbsp;&nbsp; about(QWidget * parent, const
QString &amp; title, const QString &amp; text)&nbsp; <br>
void&nbsp;&nbsp;&nbsp; aboutQt(QWidget * parent, const QString &amp; title
= QString())<br>
</div>
about() 函数用于显示本程序的信息,title 是标题栏文本,text 一般为自定义的本程序介绍文本,比如 "某某软件 1.0
版本,版权归某某所有。" <br>
aboutQt() 函数显示关于所用 Qt 版本的信息,title 是窗口标题文本,窗口内的消息文本是 Qt 自行设置的,包括 Qt
版本信息、许可证信息等等。<br>
<br>
QMessageBox 类的内容介绍到这,下面我们通过一个简单文本编辑器示例熟悉消息框的使用。<br>
我们打开 QtCreator,新建一个 Qt Widgets Application 项目,在新建项目的向导里填写:<br>
①项目名称 simpletextedit,创建路径 D:\QtProjects\ch11,点击下一步;<br>
②套件选择里面选择全部套件,点击下一步;<br>
③基类选择 QWidget,注意修改主窗口类名为 WidgetSimpleTextEdit,然后点击下一步;<br>
④项目管理不修改,点击完成。<br>
由于使用 Qt 通用对话框,就不再需要额外新建自己的子窗口类和UI文件。<br>
我们打开 widgetsimpletextedit.ui 界面文件,拖入控件:<br>
<center> <img src="images/ch11/ch11-05-02.png" alt="ui1" width="800"></center>
第一行控件是“文件名”标签,单行编辑器 lineEditFileName,“浏览”按钮 pushButtonBrowser。<br>
第二行是纯文本编辑器 plainTextEdit,编辑器右侧还有 5 个按钮,<br>
“打开文件”按钮 pushButtonOpenFile,“保存文件”按钮 pushButtonSaveFile,“关闭文件”按钮
pushButtonCloseFile,“关于本程序”按钮 pushButtonAboutThis,“关于Qt”按钮
pushButtonAboutQt。<br>
第一行控件按照水平布局器排列,第二行先将右边 5 个按钮按照垂直布局器排列,再与纯文本编辑器一起按水平布局器排列。窗口整体按照垂直布局器排列,窗口大小
640 * 480 。<br>
<br>
完成界面布局后,依次右击节目 6 个按钮,为按钮添加&nbsp; <strong>clicked()</strong> 信号的槽函数。<br>
然后右击纯文本编辑器 plainTextEdit ,为纯文本编辑器添加<b> textChanged() </b>信号的槽函数。<br>
槽函数添加完成后,我们保存并关闭 ui 文件,回到代码编辑界面。<br>
我们首先编辑头文件 widgetsimpletextedit.h 内容:<br>
<div class="code"><span style=" color:#000080;">#ifndef</span><span style=" color:#c0c0c0;">
</span>WIDGETSIMPLETEXTEDIT_H
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#define</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">WIDGETSIMPLETEXTEDIT_H</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;QWidget&gt;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;QFile&gt;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;QFileDialog&gt;</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">namespace</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Ui</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">class</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">WidgetSimpleTextEdit</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">class</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">WidgetSimpleTextEdit</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">:</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">public</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QWidget</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">Q_OBJECT</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">public</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">explicit</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QWidget</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span>parent<span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">0</span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">~</span><span style=" font-style:italic; color:#000000;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">private</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">slots</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonBrowser_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonOpenFile_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonSaveFile_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonCloseFile_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonAboutThis_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_pushButtonAboutQt_clicked</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">on_plainTextEdit_textChanged</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">private</span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Ui</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//文件对象</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QFile</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存文本是否被修改的状态</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">bool</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//根据编辑框内容写入文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">};</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#endif</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">//</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">WIDGETSIMPLETEXTEDIT_H</span></pre>
</div>
我们添加了 QFile 和 QFileDialog 的头文件引用。<br>在类内容,6 个按钮的槽函数和纯文本编辑器的槽函数是通过界面编辑操作添加的。<br>
然后手动添加了成员文件对象 m_file,标志位 m_bTextIsChanged 表示文本是否被修改。<br>
添加了函数 SaveTextFile() ,用于将编辑器内容写入到文件中。<br><br>
接下来我们分段编辑源文件 widgetsimpletextedit.cpp 内容,首先是头文件包含和构造函数:<br>
<div class="code"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">"widgetsimpletextedit.h"</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">"ui_widgetsimpletextedit.h"</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;QDebug&gt;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;QMessageBox&gt;</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#800080;">WidgetSimpleTextEdit</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">WidgetSimpleTextEdit</span><span style=" color:#000000;">(</span><span
style=" color:#800080;">QWidget</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">*</span><span style=" color:#000000;">parent</span><span
style=" color:#000000;">)</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QWidget</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">parent</span><span
style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Ui</span><span style=" color:#000000;">::</span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setupUi</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">);</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存文本是否被修改的状态</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">false</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
添加了 QDebug 和 QMessageBox 头文件包含。<br>
在构造函数里面添加了初始化代码,m_bTextIsChanged 默认为 false。<br>
然后是析构函数代码:<br>
<div class="code"><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::~</span><span style=" font-style:italic; color:#000000;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">()</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断文本修改状态</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//提示用户保存修改后的文本</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">question</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件询问"</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"是否保存文件编辑的内容?"</span><span
style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#000000;">|</span><span
style=" color:#800080;">QMessageBox</span><span style=" color:#000000;">::</span><span
style=" color:#800080;">No</span><span style=" color:#000000;">,</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">==</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存一下文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//关闭文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">close</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//清空编辑框</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">plainTextEdit</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">clear</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//删除界面</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">delete</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre></div>
在析构函数里,我们添加了对 m_bTextIsChanged 标志位的检查,如果为 true,说明文本修改了但未保存,<br>弹出消息框提示用户是否需要保存文件编辑内容,如果用户选择 Yes 按钮,那么调用 SaveTextFile() 保存文件。<br>然后关闭文件,清空纯文本编辑器,最后删除界面。<br><br>
接下来是“浏览”按钮对应槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//浏览文件</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonBrowser_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//文件名</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QFileDialog</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">getOpenFileName</span><span style=" color:#000000;">(</span><span
style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"选择文本文件"</span><span
style=" color:#000000;">),</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">""</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"Text</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">files</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">(*.txt);;All</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">files</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">(*)"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strFileName</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">isEmpty</span><span
style=" color:#000000;">())</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//没有文件名</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//正常文件名</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">lineEditFileName</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strFileName</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
该函数先定义字符串对象 strFileName,然后调用 QFileDialog::getOpenFileName() 获取要打开的文件名,<br>然后判断文件名是否为空,如果为空就不处理返回;<br>文件名不为空,就将获取的文件名显示到文件名编辑器里。<br><br>
接下来是“打开文件”按钮对应槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//打开文件</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonOpenFile_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//获取文件名</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800000;">ui</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">lineEditFileName</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">text</span><span style=" color:#000000;">().</span><span
style=" color:#000000;">trimmed</span><span style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断文件名是否为空</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">isEmpty</span><span
style=" color:#000000;">()</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">warning</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"文件名检查"</span><span style=" color:#000000;">),</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"文件名为空,请先浏览选择文本文件。"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断文本修改状态</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//提示用户保存修改后的文本</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">question</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件询问"</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"是否保存上一个文件编辑的内容?"</span><span
style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#000000;">|</span><span
style=" color:#800080;">QMessageBox</span><span style=" color:#000000;">::</span><span
style=" color:#800080;">No</span><span style=" color:#000000;">,</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Yes</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">==</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存一下文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//关闭旧文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">close</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//清空编辑框</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">plainTextEdit</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">clear</span><span
style=" color:#000000;">();</span></pre>
<pre style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//尝试打开文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setFileName</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFileName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">bool</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">bRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">open</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QIODevice</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">ReadWrite</span><span
style=" color:#000000;">|</span><span style=" color:#800080;">QIODevice</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Text</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断是否打开</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">!</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">bRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//没有打开,报错</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">warning</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"文件打开报错"</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"指定文件无法打开,请检查文件是否存在,是否有读写权限。"</span><span
style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//没有文件,没有编辑</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">false</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//修改保存按钮文本,不带</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">*</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">pushButtonSaveFile</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//文件成功打开</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QByteArray</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">baData</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800000;">m_file</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">readAll</span><span style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//转为</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">QString</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strText</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QString</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">fromLocal8Bit</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">baData</span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//显示到文本编辑器</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">plainTextEdit</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setPlainText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strText</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//修改标记位,新文件,还没有编辑</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">false</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//修改保存按钮文本,不带</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">*</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">pushButtonSaveFile</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre></div>
该函数先从单行编辑器获取文件名,并判断文件名是否为空,如果为空,弹出消息框提示用户文件名为空 并返回;<br>如果文件名非空,继续后面操作。<br>检查 m_bTextIsChanged 标志位是否为真,如果为真,说明上一个文件修改了但没有保存,<br>弹出消息框询问用户,是否保存上一个文件编辑内容,如果用户点击 Yes 就保存上一个文件。<br>然后关闭上一个文件,清空纯文本编辑器。<br>设置新文件名给文件对象,然后尝试以 QIODevice::ReadWrite|QIODevice::Text&nbsp; 模式打开文本文件,<br>如果打开失败,就重置 m_bTextIsChanged 为 false,并重置“保存文件”按钮文本,然后返回。<br>如果打开文件成功,将文件所有内容读取到 baData,然后转为 QString 类型,存到 strText 。<br>将 strText 设置给纯文本编辑器显示,并重置 m_bTextIsChanged 为 false,并重置“保存文件”按钮文本。<br><br>这里说明一下,当文本发生修改时,保存文件按钮显示“保存文件*”字样(多个 * 提示),其他时候显示“保存文件”。<br><br>
接下来我们编写实际写入文件内容的 SaveTextFile() 函数:<br>
<div class="code"><span style=" color:#008000;">//根据编辑框内容写入文件</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//获取文本</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strText</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800000;">ui</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">plainTextEdit</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">toPlainText</span><span style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//转为本地字节数组</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QByteArray</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">baData</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strText</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toLocal8Bit</span><span style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">isOpen</span><span
style=" color:#000000;">()</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//文件是打开的</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">resize</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">baData</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">size</span><span style=" color:#000000;">());</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//设置文件大小</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">reset</span><span
style=" color:#000000;">();</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//光标移动到开头位置</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">write</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">baData</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//修改标记位</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">false</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//修改保存按钮文本,不带</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">*</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">pushButtonSaveFile</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre></div>
该函数先从纯文本编辑器获取纯文本,存到 strText,然后转为本地字节数组 baData。<br>判断文件对象是否正常打开状态,如果是打开的,重新设置文件大小为 baData 数组的大小,<br>然后移动读写光标到文件开头,写入 baData 数组。<br>然后重置 m_bTextIsChanged 为 false,并重置“保存文件”按钮文本。<br><br>
接下来是“保存文件”按钮对应槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//保存文件</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonSaveFile_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断标志位</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存文件内容</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
该函数比较简单,判断 m_bTextIsChanged 标志位,为真时调用 SaveTextFile() 保存文件;如果标志位为假,就不处理。<br><br>
接下来是“关闭文件”按钮对应槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//关闭文件</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonCloseFile_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断标志位</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//有修改文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">msgBox</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">msgBox</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"文本已修改。"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">msgBox</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setInformativeText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"您希望保存到文件吗?"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">msgBox</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setIcon</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Question</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//三个按钮</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">msgBox</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setStandardButtons</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Save</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">|</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QMessageBox</span><span style=" color:#000000;">::</span><span
style=" color:#800080;">Discard</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">|</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Cancel</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">msgBox</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setDefaultButton</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Save</span><span
style=" color:#000000;">);</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRet</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">msgBox</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">exec</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//判断返回值,分别处理</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">switch</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">(</span><span style=" color:#000000;">nRet</span><span
style=" color:#000000;">)</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">case</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Save</span><span
style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//保存文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">SaveTextFile</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">break</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">case</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Discard</span><span
style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//不保存,在后面代码关闭文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">break</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">case</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">Cancel</span><span
style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//取消关闭文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">break</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">default</span><span
style=" color:#000000;">:</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">return</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//取消关闭文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">break</span><span
style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">}</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//关闭文件</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_file</span><span
style=" color:#000000;">.</span><span style=" font-style:italic; color:#000000;">close</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//清空编辑器</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">plainTextEdit</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">clear</span><span
style=" color:#000000;">();</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//没有文件,没有编辑</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">false</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//修改保存按钮文本,不带</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">*</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">pushButtonSaveFile</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre></div>
该函数先判断 m_bTextIsChanged 标志位,如果为真,说明文件修改了尚未保存,<br>定义消息框对象 msgBox,设置基本文本、信息文本、询问图标,<br>设置三个按钮 Save、Discard 、Cancel ,分别代表保存修改、丢弃修改、取消关闭操作。<br>默认按钮为 Save,然后弹出模态消息框,根据用户点击结果判断:<br>msgBox 返回值如果为 Save,调用 SaveTextFile() 保存文件;<br>如果为 Discard,就跳到后面代码,在 switch 后面关闭文件;<br>如果为 Cancel 或默认情况,均直接返回,不进行后续关闭操作。<br>switch 后面代码先关闭文件,清空纯文本编辑器,<br>重置 m_bTextIsChanged 为 false,并重置“保存文件”按钮文本。<br><br>
接下来是“关于本程序”按钮对应槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//关于本程序</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonAboutThis_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">about</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"关于本程序"</span><span style=" color:#000000;">),</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"简单文本编辑器</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">1.0"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre>
</div>
该函数就是简单显示本程序信息,标题栏是 "关于本程序",窗口内文本是 "简单文本编辑器 1.0" 。<br><br>
接下来是“关于Qt”按钮对应槽函数代码:<br>
<div class="code"><span style=" color:#008000;">//关于Qt</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_pushButtonAboutQt_clicked</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QMessageBox</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">aboutQt</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">this</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"关于Qt"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre></div>
这个函数更简单,只设置消息框标题,内容由 aboutQt() 函数自动填充,显示 Qt 版本和许可证等信息。<br><br>
最后是纯文本编辑器控件的文本修改信号对应的槽函数:<br>
<div class="code"><span style=" color:#008000;">//文本修改变化</span>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#808000;">void</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetSimpleTextEdit</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">on_plainTextEdit_textChanged</span><span
style=" color:#000000;">()</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">{</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//文本已修改</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">m_bTextIsChanged</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">true</span><span style=" color:#000000;">;</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">//修改保存按钮文本,带</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">*</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">pushButtonSaveFile</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setText</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"保存文件*"</span><span style=" color:#000000;">));</span></pre>
<pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span
style=" color:#000000;">}</span></pre></div>
该函数先修改 m_bTextIsChanged 为 true,然后设置保存文件按钮文本为 "保存文件*",多个星号提示用户文本已修改,但是尚未保存。<br>示例代码介绍到这,我们生成项目,运行示例,<br>
“浏览”选择一个文本文件,然后“打开文件”,如下图所示:<br>
<center> <img src="images/ch11/ch11-05-03.png" alt="run1"></center>
我们在编辑器修改一下文本,然后再点击“关闭文件”按钮,显示弹出如下:<br>
<center> <img src="images/ch11/ch11-05-04.png" alt="run2"></center>
点击不同按钮,效果不同,Save 会保存文件后再关闭,Discard 会丢弃修改并关闭文件,Cancel 则放弃关闭文件的操作。<br>如果我们修改了文本编辑器,尚未保存,直接再点击“打开文件”按钮,
则会弹出显示是否保存消息框,如下图所示:<br>
<center> <img src="images/ch11/ch11-05-05.png" alt="run3"></center>
如果点击 Yes,程序先保存修改内容再打开新文件;如果点击 No,则放弃保存修改的内容,直接打开新文件。<br>其他功能请自行测试,这里不再逐一测试截图。下一小节我们学习向导对话框。<br>
<br>
<div class="os2">11.5.2 QWizard</div>
<br>
<br>
<br>
<br>
<br>
<br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="width: 40%;">
<div style="text-align: center;"><a href="ch11-04.htm"><img class="pic"
style="width: 32px; height: 32px;" alt="prev" src="images/pics/prev.png"></a></div>
</td>
<td style="width: 20%;">
<div style="text-align: center;"><a href="contents.htm"><img class="pic"
style="width: 32px; height: 32px;" alt="contents" src="images/pics/contents.png"></a></div>
</td>
<td style="width: 40%;">
<div style="text-align: center;"><a href="ch12-00.htm"><img class="pic"
style="width: 32px; height: 32px;" alt="next" src="images/pics/next.png"></a></div>
</td>
</tr>
</tbody>
</table>
</body></html>

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/qtguide-qtguide.git
git@api.gitlife.ru:oschina-mirror/qtguide-qtguide.git
oschina-mirror
qtguide-qtguide
qtguide-qtguide
master