64 Star 180 Fork 34

qtguide / qtguide

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ch08-02.htm 263.67 KB
一键复制 编辑 原始数据 按行查看 历史
qtguide 提交于 2016-06-20 11:55 . ch8.2.6
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>ch08-02</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">8.2 表格控件</div>
<br>
本节先介绍表格控件 QTableWidget 的功能特性,表格控件一般有多行多列,它的单元格条目为
QTableWidgetItem,单元格条目可以有丰富的图标、文字、工具提示,支持设置显示的字体、字体颜色、背景色等。另外表格控件拥有水平表头和垂直表头,可以设
置表头的丰富特性,表头设置通常会影响表格控件整体外观。由于表格控件的具有多行多列,它的选择区域和选择行为也变得复杂起来,本节会依次讲述这些内容,最后通过
两个示例展示表格控件相关知识运用。<br>
<br>
<div class="os2">8.2.1 QTableWidget</div>
<br>
在 Qt 设计师可以拖动表格控件到界面窗口,下图可以直观看到表格控件的大致组成:<br>
<center> <img src="images/ch08/ch08-02-01.png" alt="tablewidget" width="800"></center>
表格控件最上面一排是只读的水平表头,最左边一列是只读的垂直表头。表头又可以细分为多个分段(section),水平表头的分段就是表格各个列的列首,垂直表头
分段就是表格各个行的行首。表格控件的实体区域是按行、列排布的单元格,单元格内容一般用 QTableWidgetItem
条目填充,单元格如果不填充任何东西,那么默认是 NULL。本小节先介绍表格控件基本的内容,并且讲解基类中比较实用的内容;后面 8.2.2
小节介绍单元格条目 QTableWidgetItem;控件表头的内容也比较丰富,单独放在 8.2.3
小节介绍;然后详细介绍关于表格控件的选中区域、选中行为、选中模式、单次选中命令等内容,选中区域是表格控件独有的特性,而选中行为、选中模式、单次选中命令是本章所有
基于条目的控件都具有的特性,放在 8.2.4 节一块讲解了。<br>
<br>
先来看看表格控件的构造函数:<br>
<div class="code">&nbsp;&nbsp;&nbsp; QTableWidget(QWidget * parent = 0)</div>
<div class="code">&nbsp;&nbsp;&nbsp; QTableWidget(int rows, int columns,
QWidget * parent = 0)</div>
构造函数里最重要的参数是行数 rows 和列数 columns,表格控件必须指定行数、列数,才能进行后续操作。第一个构造函数没指定行数、列数,必须用下面
两个函数设置行数和列数:<br>
<div class="code">void&nbsp;&nbsp; &nbsp;setRowCount(int rows) //设置行数</div>
<div class="code">void&nbsp;&nbsp; &nbsp;setColumnCount(int columns) //设置列数</div>
行数、列数对应的获取函数为:<br>
<div class="code">int&nbsp;&nbsp; &nbsp;rowCount() const //获取行数</div>
<div class="code">int&nbsp;&nbsp; &nbsp;columnCount() const //获取列数</div>
设置好行数、列数之后就可以进行后续的操作。表格控件在设置行数、列数之后,就有排布好的单元格,默认这些单元格是空的,即 NULL。程序员可以通过
setItem()
函数可以设置各个单元格条目;程序运行时,表格控件默认所有单元格都可以双击编辑,用户的编辑操作会使表格控件自动生成条目存储用户输入的内容。setItem()
函数和用户编辑都可以填充单元格。<br>
<br>
这里提前说明一下,表格控件的有很多名字相似的两套函数或信号,比如设置当前高亮单元格函数 setCurrentCell(int row, int
column) 和设置当前高亮条目 ​setCurrentItem(QTableWidgetItem *
item),这两个函数名字很类似,二者最主要的区别就是:名字带 Cell 的函数是基于单元格的,对 NULL 空单元格也可以使用;而名字带 Item
的函数,必须是填充了实体条目的单元格,针对实际存在的单元格条目操作。下面大致按函数功能介绍表格控件的相关内容。<br>
<br>
(1)设置条目<br>
之前 8.1 节 QListWidget 有很多添加条目或设置条目的函数,但是本节表格控件因为设置单元格必须同时知道行号、列号,所以没有 Add** 和
Insert** 之类的添加函数,表格只有如下设置条目函数:<br>
<div class="code">void QTableWidget::​setItem(int row, int column,
QTableWidgetItem * item)</div>
一般用 new 新建条目,然后设置到指定行号、列号的单元格里面,比如下面一段示范代码:<br>
<div class="code">&nbsp;&nbsp;&nbsp; QTableWidgetItem *newItem = new
QTableWidgetItem(tr("newitem");<br>
&nbsp;&nbsp;&nbsp; tableWidget-&gt;setItem(row, column, newItem);</div>
从代码而言,就只有 setItem()
函数设置单元格条目;当然,程序运行时用户可以双击编辑单元格,表格控件可以自动新建条目保存用户编辑的内容存到单元格里面。单元格条目不仅支持文本,还能设置图标、丰富
字体、字体颜色、工具提示等信息,后面还会介绍 QTableWidgetItem 类。<br>
<span style="font-weight: bold; color: red;">关于 setItem()
函数需要注意的问题是:一个条目只能设置给唯一的一个单元格,不能重复设置给多个单元格!</span>如果希望把条目换一个单元格放置,必须用下面移除条目
函数把条目从表格控件卸载下来,再重新设置给新的单元格。<br>
<br>
(2)移除条目<br>
<div class="code">QTableWidgetItem * QTableWidget::​takeItem(int row, int
column)</div>
​takeItem() 函数会把指定行号、列号的条目从表格控件卸载下来,但不会完全删除。返回的指针如果非空,那么该指针会指向内存中的条目,如果希望把条目
彻底删除,还需要自己手动 delete 删掉内存空间。如果不彻底删除,那么还可以把非空指针重新设置给表格控件。<br>
表格控件还有关于整行删除、整列删除、清空表格的几个槽函数,等会再介绍。<br>
<br>
(3)条目访问函数<br>
表格控件可以根据行号、列号获取条目对象指针:<br>
<div class="code">QTableWidgetItem * QTableWidget::​item(int row, int
column) const</div>
注意判断返回的指针是否为空,非空指针才能进行后续操作。<br>
另外还能根据表格控件内部的坐标点位来获取位于该坐标位置的条目:<br>
<div class="code">QTableWidgetItem * QTableWidget::​itemAt(const QPoint
&amp; point) const</div>
<div class="code">QTableWidgetItem * QTableWidget::​itemAt(int ax, int ay)
const</div>
这两个 itemAt() 函数等价的,坐标是指表格控件内部的相对坐标,表格控件内部左上角是 (0,0) 原点。itemAt()
函数也是可能返回空指针的,一定要判断返回值是否为 NULL。<br>
如果知道了条目 item 非空指针,可以用条目的函数获取行号、列号,如 item-&gt;row() 是行号,item-&gt;column()
是列号。根据条目的非空指针,也可以获取条目的可视矩形(条目在表格可见范围内的显示矩形):<br>
<div class="code">QRect QTableWidget::​visualItemRect(const QTableWidgetItem
* item) const</div>
<br>
(4)当前选中条目的操作<br>
表格控件一般是多行多列的,实际的选中操作是比较复杂的,我们在这里先介绍最简单的内容,就是不按键盘,只用鼠标左边点击选中当前条目的情况,就是简单的单选操
作。<br>
获取当前选中条目的函数为:<br>
<div class="code">QTableWidgetItem * QTableWidget::​currentItem() const</div>
返回的值如果非空那就是实际存在的条目,如果返回了 NULL,说明单元格是空的。<br>
不管选中的单元格内部是否为空,都可以获取当前选中单元格的行号、列号:<br>
<div class="code">int QTableWidget::​currentRow() const //当前选中行号</div>
<div class="code">int QTableWidget::​currentColumn() const //当前选中列号</div>
除了鼠标点击选中当前高亮的单元格,也可以用函数设置当前单元格:<br>
<div class="code">void QTableWidget::​setCurrentCell(int row, int column)</div>
<div class="code">void QTableWidget::​setCurrentCell(int row, int column,
QItemSelectionModel::SelectionFlags command)</div>
单元格不管是不是空的,上面两个函数都可以设置当前高亮选中状态,第二个 ​setCurrentCell() 多了个单次选中命令的参数,等到 8.4.4
节再 详细说。<br>
如果知道了非空条目指针,可以设置该条目为当前选中条目:<br>
<div class="code">void QTableWidget::​setCurrentItem(QTableWidgetItem *
item)</div>
<div class="code">void QTableWidget::​setCurrentItem(QTableWidgetItem *
item, QItemSelectionModel::SelectionFlags command)</div>
第二个函数的 command 参数等到 8.4.4 节再 详细说。<br>
<br>
无论是通过代码改变当前选中条目还是用户点击操作改变当前选中条目,都会触发当前单元格变化的信号:<br>
<div class="code">void&nbsp;&nbsp; &nbsp;currentCellChanged(int currentRow,
int currentColumn, int previousRow, int previousColumn)<br>
//单元格无论是否为空都起作用,当前选中单元格的变化信号,参数指出了新当前单元格行列号和旧单元格的行列号</div>
<div class="code">void&nbsp;&nbsp; &nbsp;currentItemChanged(QTableWidgetItem
* current, QTableWidgetItem * previous)<br>
//当前选中条目变化信号,新、旧当前选中单元格至少有一个非空才会触发这个信号</div>
<br>
(5)条目查找和排序<br>
表格控件也可以根据模版字符串查找匹配的条目:<br>
<div class="code">QList&lt;QTableWidgetItem *&gt;
QTableWidget::​findItems(const QString &amp; text, Qt::MatchFlags flags)
const</div>
参数里的 text 是模板字符串,第二个 flags 是匹配标志,匹配标志 Qt::​MatchFlags 在 8.1.1 节列举过了,一模一样,比如
Qt::MatchContains 是包含子串,Qt::MatchWildcard 是通配符(*、?)匹配,Qt::MatchRegExp
是正则表达式匹配,等等。<br>
<br>
表格控件的排序操作复杂一些,因为有很多列,开启自动排序后需要指定按照哪一列来排序。开启自动排序函数为:<br>
<div class="code">void&nbsp;&nbsp; &nbsp;setSortingEnabled(bool enable)
//设置是否自动排序</div>
<div class="code">bool&nbsp;&nbsp; &nbsp;isSortingEnabled() const
//判断是否开启了自动排序</div>
开启自动排序之后,需要指定按照哪一列来排序:<br>
<div class="code">void QTableView::​sortByColumn(int column, Qt::SortOrder
order)</div>
排序的函数是从基类 QTableView 继承的,指定自动排序的列 column 和 order (升序 Qt::AscendingOrder 或降序
Qt::DescendingOrder)。<br>
除了上面自动排序的函数,还有一个手动排序函数,可以在不开启自动排序时手动调整排序:<br>
<div class="code">void QTableWidget::​sortItems(int column, Qt::SortOrder
order = Qt::AscendingOrder)</div>
<br>
开启自动排序之后,会对 ​setItem(int row, int column, QTableWidgetItem * item)
函数产生重要影响:新设置的条目如果正好在指定的自动排序列,那么新条目会自动排序,被挪到按序排列好的行号,​setItem() 函数指定的行号不一定有效。<br>
如果用代码设置某一行的条目,该行的条目是联系在一起的,比如是某人的个人信息,那么就需要特别注意自动排序的问题:比如预期一行条目的行号是
therow,自动排序列为 sortcol,对于前几个列的条目,使用 therow 设置条目给表格,该行位置不变;当设置到 therow
行、sortcol 列的条目时,问题来了,自动排序列会根据新条目内容调整该行序号,很有可能把该行移动到了新的行号 newrow,那么如果后面代码继续用
旧的 therow 行号设置后面几列的条目,后面几列条目就会错位。这是很严重的问题。<br>
<span style="color: red; font-weight: bold;">因此在调用 setItem()
函数设置整行的多列条目时,一定要提前关闭自动排序,把新条目都设置完毕后再考虑开启自动排序!</span><br>
<br>
(6)单元格控件和运行时条目编辑<br>
单元格除了设置条目,还可以设置独立的单元格控件:<br>
<div class="code">void QTableWidget::​setCellWidget(int row, int column,
QWidget * widget)</div>
需要注意的问题是单元格控件 widget 会完全覆盖住单元格条目 item,默认情况下单元格条目 item 与单元格控件 widget
是完全没关联的,比如设置给单元格一个组合框,组合框显示的东西与单元格原本条目没关系,修改了组合框内容不会影响单元格条目内容。如果希望单元格控件与单元格条
目有关,需要自己编代码。<br>
获取单元格控件使用如下函数:<br>
<div class="code">QWidget * QTableWidget::​cellWidget(int row, int column)
const</div>
注意返回的指针可能为空。<br>
单元格控件也可以被删除掉:<br>
<div class="code">void QTableWidget::​removeCellWidget(int row, int column)</div>
删除单元格控件后,单元格就会显示原来的条目内容(如果条目原本是 NULL ,就剩下空单元格)。<br>
<br>
表格控件的单元格也可以开启持续编辑器和关闭持续编辑器(必须配对使用):<br>
<div class="code">void QTableWidget::​openPersistentEditor(QTableWidgetItem
* item) //开启持续编辑器</div>
<div class="code">void QTableWidget::​closePersistentEditor(QTableWidgetItem
* item) //关闭持续编辑器</div>
因为表格控件默认的条目全都带了可以编辑标志,所以一般用不到持续编辑器,如果想用代码让单元格进行编辑状态,可以用更简单的函数:<br>
<div class="code">void QTableWidget::​editItem(QTableWidgetItem * item)</div>
对于可编辑的条目,都可以用 ​editItem() 开启编辑器,这个临时编辑器可以自动关闭的,不需要调用其他函数。<br>
<br>
程序运行时,表格控件默认所有单元格都可以双击编辑,但如果希望表格全部是只读的,那么可以用下面函数关闭表格控件的整体编辑触发器:<br>
<div class="code">void&nbsp;&nbsp; &nbsp;setEditTriggers(EditTriggers
triggers) //设置编辑触发器</div>
<div class="code">EditTriggers&nbsp;&nbsp; &nbsp;editTriggers() const
//获取编辑触发器</div>
默认的编辑器触发器是双击、回车键等可以编辑单元格,修改为 QAbstractItemView::NoEditTriggers
就不会开启任何编辑器了,相当于整个表格只读了。如果不希望整个表格只读,而只是希望某部分的单元格只读,那么只有设置单元格条目自身的标志
位,item-&gt;setFlags(Qt::ItemFlags flags),把 flags 设置为不带可编辑标志的:<br>
(item-&gt;flags())&nbsp; &amp; (~Qt::ItemIsEditable) 。<br>
<br>
(7)信号<br>
除了之前介绍的 currentCellChanged() 和 currentItemChanged()
信号,还有多个关于单元格和条目操作的信号,按照基于单元格操作触发还是实体条目操作触发,大致分为两类:<br>
<div class="code">void&nbsp;&nbsp; &nbsp;cellActivated(int row, int column)
//单元格被激活</div>
<div class="code">void&nbsp;&nbsp; &nbsp;cellChanged(int row, int column)
//单元格内部发生变化,如从NULL变成有条目,或者条目内部数据发生变化</div>
<div class="code">void&nbsp;&nbsp; &nbsp;cellClicked(int row, int column)
//单元格被点击</div>
<div class="code">void&nbsp;&nbsp; &nbsp;cellDoubleClicked(int row, int
column) //单元格被双击</div>
<div class="code">void&nbsp;&nbsp; &nbsp;cellEntered(int row, int column) <br>
//鼠标进入单元格,只有在 mouseTracking 开启时或鼠标移动时点击了单元格,才触发该信号</div>
<div class="code">void&nbsp;&nbsp; &nbsp;cellPressed(int row, int column)
//单元格被鼠标点击按下</div>
第二类是基于实体条目触发的:
<div class="code">void&nbsp;&nbsp; &nbsp;itemActivated(QTableWidgetItem *
item) //条目被激活</div>
<div class="code">void&nbsp;&nbsp; &nbsp;itemChanged(QTableWidgetItem *
item) //条目内部数据发生变化,如文本修改、图标变化、复选状态变化等</div>
<div class="code">void&nbsp;&nbsp; &nbsp;itemClicked(QTableWidgetItem *
item) //条目被点击</div>
<div class="code">void&nbsp;&nbsp; &nbsp;itemDoubleClicked(QTableWidgetItem
* item) //条目被双击</div>
<div class="code">void&nbsp;&nbsp; &nbsp;itemEntered(QTableWidgetItem *
item)<br>
//鼠标进入条目,只有在 mouseTracking 开启时或鼠标移动时点击了条目,才触发该信号</div>
<div class="code">void&nbsp;&nbsp; &nbsp;itemPressed(QTableWidgetItem *
item) //条目被鼠标点击按下</div>
因为表格条目是多选的,可以有很多高亮选中条目,高亮选中的状态发生变化时,会触发如下信号:
<div class="code">void&nbsp;&nbsp; &nbsp;itemSelectionChanged()</div>
上面信号参数没有指明哪些条目被选中,要获取所有选中的条目,可以用如下函数:
<div class="code">QList&lt;QTableWidgetItem *&gt;
QTableWidget::​selectedItems() const</div>
更多关于表格选中操作的内容后面再介绍。<br>
<br>
(8)槽函数<br>
表格有两个清空内容的槽函数,首先是 clear() 槽函数:<br>
<div class="code">void QTableWidget::​clear()</div>
​clear() 函数删除表格内所有条目内容,单元格全为 NULL,清除选中状态,水平表头和垂直表头的设置内容清空,但是会保留表格的行数、列数,表头清掉
之后,会使用默认的数字行号、列号。<br>
第二个清除内容的槽函数:<br>
<div class="code">void QTableWidget::​clearContents()</div>
这个函数删除所有条目内容,单元格全为 NULL,清除选中状态,但是表头的设置内容都保留,表头文本等照旧显示,表格行数、列数也不变。<br>
还有关于整行、整列插入删除操作的槽函数:<br>
<div class="code">void QTableWidget::​insertColumn(int column) //插入新的一列,新列序号
column,新列的单元格默认 NULL</div>
<div class="code">void QTableWidget::​insertRow(int row) //插入新的一行,新行序号
row,新行的单元格默认 NULL</div>
<div class="code">void QTableWidget::​removeColumn(int column) //删除第 column
整列条目</div>
<div class="code">void QTableWidget::​removeRow(int row) //删除第 row 整行条目</div>
最后是关于条目滚动显示的函数,表格的行、列很多时,只有一部分的单元格显示在表格控件可视矩形里,如果希望滚动表格,让指定的条目显示出来,使用如下函数:<br>
<div class="code">void QTableWidget::​scrollToItem(const QTableWidgetItem *
item, QAbstractItemView::ScrollHint hint = EnsureVisible)</div>
QAbstractItemView::ScrollHint 枚举常量在 8.1.1 节列举过,这里不重复介绍了。<br>
<br>
(9)基类 QTableView 的函数<br>
表格控件还从基类 QTableView 继承了很多函数,这里介绍一些比较实用的,更多关于 QTableView 视图的内容会等到模型/视图章节再详解。<br>
表头控件的表头分段有分隔线,拖动分隔线可以调整各列的宽度,各行高度也可以拖动分隔线调整。如果通过函数代码调整列宽、行高,可以用下面的函数:<br>
<div class="code">void QTableView::​setColumnWidth(int column, int width)
//设置列宽</div>
<div class="code">int QTableView::​columnWidth(int column) const //获取列宽</div>
<div class="code">void QTableView::​setRowHeight(int row, int height) //设置行高</div>
<div class="code">int QTableView::​rowHeight(int row) const //获取行高</div>
用户在程序运行时可以双击表头分段的分隔线,那样表格控件会自动根据该列单元格内容最宽的条目,调整列宽,让该列条目内容都显示出来,这个操作也有对应的函数:
(下面四个都是槽函数)<br>
<div class="code">void&nbsp;&nbsp; &nbsp;resizeColumnToContents(int column)
//自动调整第 column 列列宽,将该列条目显示完整</div>
<div class="code">void&nbsp;&nbsp; &nbsp;resizeColumnsToContents()
//自动调整所有列宽</div>
<div class="code">void&nbsp;&nbsp; &nbsp;resizeRowToContents(int row)
//自动调整第 row 行行高,将该行条目显示完整</div>
<div class="code">void&nbsp;&nbsp; &nbsp;resizeRowsToContents() //自动调整所有行高</div>
如果希望隐藏或显示指定列的所有条目或指定行的条目,可以用如下函数:<br>
<div class="code">void QTableView::​setColumnHidden(int column, bool hide)
//设置指定列隐藏或显示</div>
<div class="code">bool QTableView::​isColumnHidden(int column) const
//判断指定列是否被隐藏</div>
<div class="code">void QTableView::​setRowHidden(int row, bool hide)
//设置指定行隐 藏或显示</div>
<div class="code">bool QTableView::​isRowHidden(int row) const //判断指定行是否被隐藏</div>
关于显示或隐藏行列,还有四个快捷槽函数:hideColumn(int column),hideRow(int row),showColumn(int
column),showRow(int row),这些函数都可以灵活使用,效果与 ​setColumnHidden() 和
​setRowHidden() 函数是一样的。<br>
<br>
QTableView 也有更抽象的基类 QAbstractItemView ,QAbstractItemView
里面也有一些实用函数,这里先介绍一个设置条目显示图标大小的函数:<br>
<div class="code">void QAbstractItemView::setIconSize(const QSize &amp;
size)</div>
单元格里图标默认显示为 16*16 的,可以用该函数设置图标显示尺寸为 24*24 或 32*32 。 QAbstractItemView
类还有关于条目选中模式、选中行为等重要函数,后面再介绍。基类的内容暂时介绍的到这,下面来学学单元格条目的知识。<br>
<br>
<div class="os2">8.2.2 QTableWidgetItem</div>
<br>
表格控件条目 QTableWidgetItem 与 8.1.2 节 QListWidgetItem 有很多相似的地方,<br>
QTableWidgetItem 也是一个纯数据类,不是控件,没有基类,也就没有信号和槽函数。QTableWidgetItem 可以直接用数据流
QDataStream 读写,表格条目不单单有文本,还可以有自己的图标、复选框等特性,表格控件会根据条目对象的丰富特性来呈现数据并进行交互操作。<br>
(1)首先来看看条目的构造函数:<br>
<div class="code">&nbsp;&nbsp;&nbsp; QTableWidgetItem(int type = Type)</div>
<div class="code">&nbsp;&nbsp;&nbsp; QTableWidgetItem(const QString &amp;
text, int type = Type)</div>
<div class="code">&nbsp;&nbsp;&nbsp; QTableWidgetItem(const QIcon &amp;
icon, const QString &amp; text, int type = Type)</div>
<div class="code">&nbsp;&nbsp;&nbsp; QTableWidgetItem(const QTableWidgetItem
&amp; other) //复制构造函数</div>
参数里的 type 一般用于派生类,指定条目独特的分类,对于普通的表格控件条目,通常用默认的数值即可。第二个构造函数是指定了条目的文本,第三个构造函数同
时指定了条目图标和文本。<br>
在构造函数里不能直接指定所属的表格控件,因为表格控件有行号、列号所以不能简单追加到表格里,而只能用表格控件的
QTableWidget::​setItem()
函数把条目设置到指定单元格里。注意一个条目只能设置给表格控件唯一的一个单元格,不能把一个条目设置给多个单元格。如果希望为多个单元格设置相似的条目数据内容,那么可
以用后面介绍的 clone() 函数制造大量新的复制体条目。<br>
复制构造函数不会复制旧条目的 type() 类型值和tableWidget() 所属表格控件指针,条目其他的内部数据和条目标志都会复制。<br>
<br>
(2)复制函数和运算符函数<br>
除了复制构造函数,还有专门的克隆函数:<br>
<div class="code">QTableWidgetItem * QTableWidgetItem::​clone() const</div>
克隆函数会新建一个当前条目的复制体,然后返回新复制体条目的指针,如果要构造并添加大量重复内容的条目,这个函数就比较实用。clone()
函数内部会调用复制构造函数创建新条目并返回。<br>
条目复制还可以用等于号函数:<br>
<div class="code">QTableWidgetItem &amp; QTableWidgetItem::​operator=(const
QTableWidgetItem &amp; other)</div>
等于号函数复制的内容与复制构造函数差不多,type() 类型值和 tableWidget() 所属表格控件指针也不复制。<br>
还有个小于号函数,可以比较条目文本的字典序:<br>
<div class="code">QTableWidgetItem &amp; QTableWidgetItem::​operator=(const
QTableWidgetItem &amp; other)</div>
如果希望使用更多的比较函数,可以直接用 QString 的比较函数。<br>
<br>
(3)QTableWidgetItem 的功能函数与内部数据<br>
QTableWidgetItem 内部的数据也大致分为两类:第一类是以数据角色形式管理的通用数据,这些数据自动参与 QDataStream
数据流的读写;第二类是非通用数据,不参与数据流读写,与 QListWidgetItem 和 QListWidget
自身特性有关。QTableWidgetItem 主要的功能函数也是围绕这两类内部数据展开的。<br>
<br>
● 第一类:通用数据及其处理函数<br>
通用数据是以数据角色与数据变量一一对应的形式存储管理,比如设置文本 setText()、设置图标 setIcon()
等函数,其本质都是根据各自的角色调用通用设置数据的函数:<br>
<div class="code">virtual void setData(int role, const QVariant &amp; value)</div>
也可以根据角色来获取各个数据变量:<br>
<div class="code">virtual QVariant data(int role) const</div>
查看表格控件的源代码文件可以看到关于图标操作函数的源码:<br>
<div class="code">inline QIcon icon() const<br>
&nbsp;&nbsp; { return
qvariant_cast&lt;QIcon&gt;(data(Qt::DecorationRole)); }</div>
<div class="code">inline void QTableWidgetItem::setIcon(const QIcon
&amp;aicon)<br>
&nbsp;&nbsp; { setData(Qt::DecorationRole, aicon); }</div>
QTableWidgetItem 的通用数据与 QListWidgetItem 的通用数据一模一样,函数名也一样,列表如下:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 110px;" align="center"><b>获取函数</b></td>
<td style="width: 280px;" align="center"><b>设置函数</b></td>
<td style="width: 150px;" align="center"><b>数据角色</b></td>
<td align="center"><b> 描述 </b></td>
</tr>
<tr>
<td>text()</td>
<td> setText(const QString &amp;text) </td>
<td> Qt::DisplayRole </td>
<td> 条目显示的文本。 </td>
</tr>
<tr class="d1">
<td>icon()</td>
<td> setIcon(const QIcon &amp;icon) </td>
<td> Qt::DecorationRole </td>
<td> 条目显示的图标。 </td>
</tr>
<tr>
<td>statusTip()</td>
<td> setStatusTip(const QString &amp;statusTip) </td>
<td> Qt::StatusTipRole </td>
<td> 如果主界面有状态栏,鼠标悬停在该条目上时显示该状态信息到状态栏。 </td>
</tr>
<tr class="d1">
<td>toolTip()</td>
<td> setToolTip(const QString &amp;toolTip) </td>
<td> Qt::ToolTipRole </td>
<td> 鼠标悬停在该条目上时显示的工具提示信息。 </td>
</tr>
<tr>
<td>whatsThis()</td>
<td> setWhatsThis(const QString &amp;whatsThis) </td>
<td> Qt::WhatsThisRole </td>
<td> 如果主界面窗口标题栏有?帮助按钮,点击帮助按钮再点击该条目会显示该帮助信息。 </td>
</tr>
<tr class="d1">
<td>font()</td>
<td> setFont(const QFont &amp;font) </td>
<td> Qt::FontRole </td>
<td> 显示条目文本用的字体。 </td>
</tr>
<tr>
<td>textAlignment()</td>
<td> setTextAlignment(int alignment) </td>
<td> Qt::TextAlignmentRole </td>
<td> 文本的对齐方式。 </td>
</tr>
<tr class="d1">
<td>backgroundColor()</td>
<td> setBackgroundColor(const QColor &amp;color)</td>
<td> Qt::BackgroundColorRole </td>
<td> 文本背景色。 </td>
</tr>
<tr>
<td>textColor()</td>
<td> setTextColor(const QColor &amp;color)</td>
<td> Qt::TextColorRole </td>
<td> 文字颜色。 </td>
</tr>
<tr class="d1">
<td>background()</td>
<td> setBackground(const QBrush &amp;brush) </td>
<td> Qt::BackgroundRole </td>
<td> 条目的背景画刷。 </td>
</tr>
<tr>
<td>foreground()</td>
<td> setForeground(const QBrush &amp;brush) </td>
<td> Qt::ForegroundRole </td>
<td> 条目的前景画刷。 </td>
</tr>
<tr class="d1">
<td>checkState()</td>
<td> setCheckState(Qt::CheckState state)</td>
<td> Qt::CheckStateRole </td>
<td> 条目自带的复选框选中状态,可以是三态复选框。 </td>
</tr>
<tr>
<td>sizeHint()</td>
<td> setSizeHint(const QSize &amp;size) </td>
<td> Qt::SizeHintRole </td>
<td> 条目显示的建议尺寸。 </td>
</tr>
</tbody>
</table>
<br>
表格条目 QTableWidgetItem 可以直接用数据流 QDataStream 读写,是通过下面运算符重载函数实现的:<br>
<div class="code">QDataStream &amp;&nbsp;&nbsp;
&nbsp;operator&lt;&lt;(QDataStream &amp; out, const QTableWidgetItem &amp;
item)</div>
<div class="code">QDataStream &amp;&nbsp;&nbsp;
&nbsp;operator&gt;&gt;(QDataStream &amp; in, QTableWidgetItem &amp; item)</div>
使用 &gt;&gt;&lt;&lt; 运算符进行流输入输出比较常见,当然也可以用 QTableWidgetItem
类内部的读写函数进行输入输出:<br>
<div class="code">void QTableWidgetItem::​read(QDataStream &amp; in)</div>
<div class="code">void QTableWidgetItem::​write(QDataStream &amp; out) const</div>
<br>
● 第二类:非通用数据及其操作函数<br>
表格控件的条目也是在构造函数一次性指定条目类型,然后这个类型是只读的:<br>
<div class="code">int QTableWidgetItem::​type() const</div>
类型值一般用于派生类区分不同的条目类型,自定义的条目类型值需要大于等于 QTableWidgetItem::UserType 。<br>
新条目通过表格控件 QTableWidget::​setItem() 函数设置给表格控件之后,条目会自动保存所属的表格控件指针,可以用如下函数获取:<br>
<div class="code">QTableWidget * QTableWidgetItem::​tableWidget() const</div>
<br>
程序运行时,除了可以通过表格控件的 QTableWidget::​selectedItems() 获取用户选中的条目列表,每个单元格条目自身也会记录是否
被用户选中,并且能设置自身是否被选中:<br>
<div class="code">bool QTableWidgetItem::​isSelected() const //判断条目自身是否高亮选中</div>
<div class="code">void QTableWidgetItem::​setSelected(bool select) //设置条目自身是
否被选中</div>
<br>
表格控件条目初始化时也有默认的标志位,并且运行时可以修改条目的特性标志:<br>
<div class="code">Qt::ItemFlags QTableWidgetItem::​flags() const //获取特性标志位</div>
<div class="code">void QTableWidgetItem::​setFlags(Qt::ItemFlags flags)
//设置特性标志位</div>
QTableWidgetItem 构造时默认的标志位为:<br>
&nbsp;&nbsp;&nbsp; Qt::ItemIsEditable<br>
&nbsp;&nbsp;&nbsp; |Qt::ItemIsSelectable<br>
&nbsp;&nbsp;&nbsp; |Qt::ItemIsUserCheckable<br>
&nbsp;&nbsp;&nbsp; |Qt::ItemIsEnabled<br>
&nbsp;&nbsp;&nbsp; |Qt::ItemIsDragEnabled<br>
&nbsp;&nbsp;&nbsp; |Qt::ItemIsDropEnabled<br>
因此程序代码生成的所有表格条目默认都是可编辑的,程序运行时用户双击空的单元格时,表格控件会自动创建新条目保存用户编辑内容,这些自动创建的条目也都是可编辑
的。如果希望条目是只读的,那么把标志位设置不带Qt::ItemIsEditable 的新标志位即可。<br>
<br>
Qt::ItemFlags 所有的枚举常量在 8.1.2
节末尾有详细的表格描述,这里不重复贴了。表格条目默认就是支持复选状态的,只是默认没有显示出来,要让表格控件显示条目对应的复选框,可以用下面一句代码实现:<br>
<div class="code">&nbsp;&nbsp;&nbsp; item-&gt;setCheckState( Qt::Unchecked
);&nbsp; //显示复选框</div>
对于表格控件和表格条目,自带单行编辑器和复选框功能,因此不需要为单元格添加自定义的单行编辑器或复选框,如果要用到其他更多的输入控件,那才需要定制单元格。<br>
<br>
表格控件条目与 QListWidgetItem 最大的一个区别就是表格条目既有行号,也有列号:<br>
<div class="code">int QTableWidgetItem::​row() const //获取行号</div>
<div class="code">int QTableWidgetItem::​column() const //获取列号</div>
行号和列号也是在 QTableWidget::​setItem() 函数里指定的,条目自身并不能修改行号或列号。表格控件条目与
QListWidgetItem 另一个区别是,表格控件条目没有隐藏函数,所有表格条目都是显示的(只要不被单元格控件QTableWidget::​
cellWidget() 遮挡)。<br>
<br>
在 QtCreator 设计模式和 Qt 设计师界面,表格控件 QTableWidget 和其条目 QTableWidgetItem&nbsp;
都是可以可视化编辑的,上面介绍的通用数据和非通用数据都是可以在属性栏设置的,后面会介绍表格控件及其条目的可视化编辑。<br>
<br>
<div class="os2">8.2.3 表头设置</div>
<br>
表格控件在指定了行号和列号之后,就会自动生成水平表头和垂直表头,默认的表头都是从数字 1 开始递增编号,如下图所示:<br>
<center> <img src="images/ch08/ch08-02-02.png" alt="autoheader"></center>
虽然默认的表头文本看起来从 1 开始编号,但是内部的表头分段(section)都是从 0 开始编号的,代码里设置表头分段时,需要从 0 编号。<br>
表格控件的既可以简单地设置表头分段文本,也可以利用 QTableWidgetItem 为表头分段设置功能更丰富的表头条目,设置表头文本的函数如下:<br>
<div class="code">void QTableWidget::​setHorizontalHeaderLabels(const
QStringList &amp; labels) //设置水平表头文本</div>
<div class="code">void QTableWidget::​setVerticalHeaderLabels(const
QStringList &amp; labels) //设置垂直表头文本</div>
QStringList 内字符串列表,第 0 个字符串就是第 0 号表头分段的文本,一般要求 QStringList
字符串个数与表头分段数目一样,这样不会有漏的,也不会有多的。通常比较多的是设置水平表头(就是每列的列首),垂直表头一般可以不设置,用自动编号。<br>
<br>
​setHorizontalHeaderLabels() 和 ​setVerticalHeaderLabels()&nbsp;
其实是两个快捷函数,本质是循环构建表头分段条目并设置给表头分段:<br>
<div class="code">void QTableWidget::​setHorizontalHeaderItem(int column,
QTableWidgetItem * item) //设置指定列的水平表头分段条目</div>
<div class="code">QTableWidgetItem * QTableWidget::​horizontalHeaderItem(int
column) const //获取指定列的水平表头分段条目</div>
<div class="code">void QTableWidget::​setVerticalHeaderItem(int row,
QTableWidgetItem * item) //设置指定行的垂直表头分段条目</div>
<div class="code">QTableWidgetItem * QTableWidget::​verticalHeaderItem(int
row) const //获取指定行的垂直表头分段条目</div>
这里可以发现 QTableWidgetItem 既可以作为表格控件普通单元格的条目,也可以用来设置表头分段。<br>
QTableWidgetItem
作为表头分段条目使用时,肯定是只读的,程序运行时不能双击编辑表头,而且也不能有复选框。表头分段条目支持显示文本、图标、工具提示信息,并能设置文本对齐、字体、前景
画刷和背景画刷等等,这样表头显示的功能就比较丰富,而不是单纯的文本。<br>
使用 ​setHorizontalHeaderItem() 和 ​setVerticalHeaderItem() 函数时,一个
QTableWidgetItem 只能设置给唯一的一个表头分段,并且设置给表头分段之后,也不要把表头分段条目设置给表格控件普通的单元格。<br>
表头分段的条目也可以从表格控件卸载卸载下来:<br>
<div class="code">QTableWidgetItem *
QTableWidget::​takeHorizontalHeaderItem(int column) //卸下指定列的水平表头条目</div>
<div class="code">QTableWidgetItem *
QTableWidget::​takeVerticalHeaderItem(int row) //卸下指定行的垂直表头条目</div>
卸载下来的条目并不会自动删除,还存在内存中,如果希望彻底删除需要手动 delete,或者不删除的话,卸载下来的条目可以重新设置给另一个表头分段。<br>
<br>
表头分段的条目与普通的单元格条目有重要的区别,普通单元格条目可编辑、可以有复选状态,但是表头分段的条目只有显示功能,不能双击编辑或复选。把
QTableWidgetItem 设置给表头分段后,QTableWidgetItem 条目会有额外的内部特性标志:<br>
<div class="code"><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">&nbsp;&nbsp;&nbsp;
if</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">(</span><span
style=" color:#000000;">item</span><span style=" color:#000000;">)</span><span
style=" color:#c0c0c0;"> </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:#c0c0c0;"> </span><span style=" color:#000000;">item</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">view</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">view</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;">item</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">itemFlags</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">Qt</span><span style=" color:#000000;">::</span><span style=" color:#800080;">ItemFlags</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">item</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">itemFlags</span><span style=" color:#000000;">)|</span><span
style=" color:#800080;">ItemIsHeaderItem</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>
</div>
上面小段代码是从 QTableWidget 源代码里截取的,设置表头分段条目时,item 的标志位会额外设置 ItemIsHeaderItem
标志位(ItemIsHeaderItem 是内部专用标志位,并不公开),因此设置给表头的条目是不能再设置给普通单元格的。条目 item
设置给一个表头分段后,也不能设置给其他的表头分段,因为表格控件销毁时会 delete
每个表头条目,一个条目指针如果设置给多个表头分段,那么同一个条目指针最后会被 delete 多次,删除野指针会导致程序崩溃。<br>
<br>
表格控件并不要求必须设置表头文本或表头条目,在不设置表头文本和表头条目的情况下,默认是所有的表头分段条目是 NULL
,所以要注意表头条目获取和卸载函数返回的指针可能是 NULL。不设置任何表头文本和表头条目时,表格控件依然是有表头的,默认的表头是按照前面截图的从 1
开始自动编号,自动编号表头是没有实体表头条目的,这些自动编号表头是从基类 QTableView 视图类继承而来的。<br>
<br>
QTableWidget 本层类带有前面介绍 8 个表头分段操作的函数,各个表头分段设置从微观角度展示表头的功能,而基类 QTableView
有从宏观描述的整体表头,即:<br>
<div class="code">QHeaderView * QTableView::​horizontalHeader() const
//获取整个的水平表头</div>
<div class="code">void QTableView::​setHorizontalHeader(QHeaderView *
header) //设置整个的水平表头</div>
<div class="code">QHeaderView * QTableView::​verticalHeader() const
//获取整个的垂直表头</div>
<div class="code">void QTableView::​setVerticalHeader(QHeaderView * header)
//设置整个的垂直表头</div>
因为表格控件自己就会带有水平表头和垂直表头,所以一般不需要调用 ​setHorizontalHeader() 和
​setVerticalHeader() 函数,直接获取原本的表头对象指针,然后调用表头类 QHeaderView 的函数修改特性即可。<br>
水平表头和垂直表头都是同一个类 QHeaderView ,水平表头和垂直表头可以通过如下函数判断: <br>
<div class="code">Qt::Orientation QHeaderView::​orientation() const</div>
返回值如果是 Qt::Horizontal 表示水平表头,Qt::Vertical 是垂直表头,这个方向特性是在构造函数时指定的,之后不能进行修改。<br>
QHeaderView 功能是非常丰富的,这里介绍一些比较实用的,以后到模型视图章节还会介绍更多的。<br>
获取当前表头的分段数目,使用函数:<br>
<div class="code">int QHeaderView::​count() const</div>
显示整个表头显示的像素点长度(水平表头是表头整体宽度,垂直表头是表头整体高度):<br>
<div class="code">int QHeaderView::​length() const</div>
<br>
在 Qt 设计师界面,选中表格控件,可以看到水平表头和垂直表头的属性,这里以水平表头的属性举例:<br>
<center> <img src="images/ch08/ch08-02-03.png" alt="header2" width="800"></center>
当表格控件开启自动排序属性 sortingEnabled 之后,水平表头就会自动设置 horizontalHeaderShowSortIndicator
为勾选状态,这样程序运行时根据一个列自动排序时,该列水平表头分段显示三角形排序指示符。如果勾选水平表头的
horizontalHeaderStretchLastSection
属性,那么表格最后一列会自动拉伸占据剩下的空间。水平表头和垂直表头这些属性都有对应的 QHeaderView 类的函数,罗列描述如下:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 180px;" align="center"><b>属性</b></td>
<td style="width: 280px;" align="center"><b>QHeaderView类设置 函数</b></td>
<td style="width: 180px;" align="center"><b>QHeaderView类获取 函数</b></td>
<td align="center"><b> 描述 </b></td>
</tr>
<tr>
<td>**visible</td>
<td> setVisible(bool v) </td>
<td>isVisible()</td>
<td> 是否显示表头。</td>
</tr>
<tr class="d1">
<td>**cascadingSectionResizes</td>
<td>setCascadingSectionResizes(bool enable) </td>
<td> cascadingSectionResizes()</td>
<td> 调整分段尺寸时是否仅相邻分段调整。 </td>
</tr>
<tr>
<td>**defaultSectionSize</td>
<td>setDefaultSectionSize(int size) </td>
<td> defaultSectionSize()</td>
<td> 分段默认尺寸,水平表头是指分段宽度(默认100),垂直表头指分段高度(默认30)。 </td>
</tr>
<tr class="d1">
<td>**highlightSections</td>
<td> setHighlightSections(bool highlight)</td>
<td> highlightSections()</td>
<td> 条目高亮选中时,其行首、列首是否也有高亮效果。一般不需要手动设置。 </td>
</tr>
<tr>
<td>**minimumSectionSize</td>
<td> setMinimumSectionSize(int size) </td>
<td> minimumSectionSize() </td>
<td> 分段尺寸下限。水平表头分段默认最小宽度 21,垂直表头分段默认最小高度 21。 </td>
</tr>
<tr class="d1">
<td>**showSortIndicator</td>
<td> setSortIndicatorShown(bool show) </td>
<td> isSortIndicatorShown() </td>
<td> 表头是否显示排序指示符。开启自动排序后通常自动显示排序指示符,一般不需要手动设置。 </td>
</tr>
<tr>
<td>**stretchLastSection</td>
<td> setStretchLastSection(bool stretch) </td>
<td> stretchLastSection() </td>
<td> 是否自动拉伸末尾分段。 </td>
</tr>
</tbody>
</table>
<br>
按照上表描述,如果希望隐藏垂直表头,那么就可以执行如下代码:<br>
<div class="code"><span style=" color:#c0c0c0;">&nbsp;</span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">verticalHeader</span><span
style=" color:#000000;">()-&gt;</span><span style=" font-style:italic; color:#000000;">setVisible</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">false</span><span
style=" color:#000000;">);</span></div>
如果修改默认的列宽,让每列显示出更长的文本,可以把默认值 100 修改为 160 像素:<br>
<div class="code"><span style=" color:#800000;">&nbsp;ui</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">horizontalHeader</span><span style=" color:#000000;">()-&gt;</span><span
style=" color:#000000;">setDefaultSectionSize</span><span style=" color:#000000;">(</span><span
style=" color:#000080;">160</span><span style=" color:#000000;">);</span></div>
其他属性的意义比较明确,关于 cascadingSectionResizes 属性是指用户拖动表头分段的分隔线调整列宽或行高时,是否仅相邻分段调整:<br>
如果 cascadingSectionResizes 为
true,那么调整尺寸操作仅涉及分隔线相邻的两个分段,比如左边列变宽,那么右边一列就会变窄。如果 cascadingSectionResizes 为
false,就是默认的递推调整右侧所有列,左边一列变宽时,右边所有列列宽固定,并且右边所有列整体右移,表格整体拉大。<br>
<br>
除了 Qt 设计师里能看到的属性,QHeaderView 还有 defaultAlignment
属性决定表头文本的对齐方式,maximumSectionSize 属性决定分段的尺寸上限。<br>
<br>
表头的特性设置通常会影响表格控件整体的外观,之前介绍过表格控件基类 QTableView 调整列宽和隐藏/显示整列的函数,其实 QTableView
很多关于整行或整列调整的函数都是在内部调用表头 QHeaderView 的函数来实现,比如设置列宽的:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">QTableView</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">setColumnWidth</span><span style=" color:#000000;">(</span><span
style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">column</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">width</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:#000080;">Q_D</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">const</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableView</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;">d</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">horizontalHeader</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">resizeSection</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">column</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">width</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>
QHeaderView::resizeSection(int logicalIndex, int size),就是调整分段尺寸,对于水平表头是调整列宽,
对于垂直表头是调整行高。<br>
QTableView 设置某列隐藏或显示的函数,内部代码为:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">QTableView</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">setColumnHidden</span><span style=" color:#000000;">(</span><span
style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">column</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">bool</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">hide</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:#000080;">Q_D</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QTableView</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:#c0c0c0;"> </span><span style=" color:#000000;">(</span><span style=" color:#000000;">column</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&lt;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">0</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">||</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">column</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&gt;=</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">d</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">horizontalHeader</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">count</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;">d</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">horizontalHeader</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">setSectionHidden</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">column</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">hide</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>
<span style="font-weight: bold;">QTableView 很多功能都是通过表头 QHeaderView
的函数实现的,如果表格控件需要设置特定的功能找不到直接的设置或获取函数,那么在 QHeaderView 类里面找函数是比较有效的方法。</span>
如 QHeaderView 类可以查询隐藏的分段计数:<br>
<div class="code">int QHeaderView::​hiddenSectionCount() const</div>
如果表格控件隐藏了一些列,水平表头的 ​hiddenSectionCount() 就是隐藏的列数目。<br>
默认情况下,表格控件所有的列显示顺序是固定的,如果用户希望可以自己调整列的顺序,那么可以设置表头的分段是可移动的:<br>
<div class="code">void QHeaderView::​setSectionsMovable(bool movable)</div>
<br>
利用表头类的函数,我们可以通过调整水平表头和垂直表头的默认分段尺寸设置单元格默认尺寸:<br>
<div class="code"><span style=" color:#c0c0c0;">&nbsp;&nbsp;&nbsp; </span><span
style=" color:#800000;">ui</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">horizontalHeader</span><span style=" color:#000000;">()-&gt;</span><span
style=" color:#000000;">setDefaultSectionSize</span><span style=" color:#000000;">(</span><span
style=" color:#000080;">150</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:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">verticalHeader</span><span
style=" color:#000000;">()-&gt;</span><span style=" color:#000000;">setDefaultSectionSize</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">50</span><span style=" color:#000000;">);</span></pre>
</div>
这样表格的单元格尺寸就会变大很多:<br>
<center> <img src="images/ch08/ch08-02-04.png" alt="cellsize"></center>
现在单元格的尺寸是可以按自己意愿调整的,但是如果希望调整上面水平表头的高度或调整左侧垂直表头的宽度,那怎么办呢?<br>
**SectionSize 只能指定表头分段延伸的一维尺寸,不会影响水平表头高度,也不影响垂直表头的宽度。 QHeaderView
表头类其实也是通用控件和窗口类 QWidget 的派生类,QWidget 设置控件或窗口最小尺寸的函数对 QHeaderView 也适用,表头
QHeaderView 相当于是表格控件内嵌的子控件,表头 QHeaderView 还可以有自己独立的 QSS 样式表。我们使用表头类的基类
QWidget 的函数可以设置水平表头的最小高度和垂直表头的最小宽度,这样可以调整表头自身尺寸:<br>
<div class="code"><span style=" color:#008000;">&nbsp;&nbsp;&nbsp;
//水平表头高度设置</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:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">horizontalHeader</span><span
style=" color:#000000;">()-&gt;</span><span style=" color:#000000;">setMinimumHeight</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">50</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">verticalHeader</span><span
style=" color:#000000;">()-&gt;</span><span style=" color:#000000;">setMinimumWidth</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">100</span><span style=" color:#000000;">);</span></pre>
</div>
设置效果如下:<br>
<center> <img src="images/ch08/ch08-02-05.png" alt="headersize"></center>
<br>
关于表头的内容大致介绍到这,QtCreator 设计模式和 Qt 设计师可以编辑表格控件的表头和单元格,后面再介绍。提前说一下,Qt 5.4.*
开发环境里的 QtCreator 设计模式和 Qt 设计师在添加表格控件表头时有 bug,需要第二次编辑并调整顺序,表头才能正常显示,因此更推荐用代码来
设置水平表头和垂直表头。<br>
<br>
<div class="os2">8.2.4 选中区域和选中行为</div>
<br>
(1)选中区域<br>
之前介绍过,获取选中的条目可以如下函数:<br>
<div class="code">QList&lt;QTableWidgetItem *&gt;
QTableWidget::​selectedItems() const</div>
选中的单元格如果有实体条目,会记录在 ​selectedItems() 返回的条目列表里,但是如果选中了内部为 NULL
的单元格,那是无法存到选中条目列表的。为了能更完整地描述选中的单元格区域, Qt 专门提供了一个 QTableWidgetSelectionRange
类,记录表格控件的选中区 域,不管单元格有没有条目,都可以描述选中单元格范围。<br>
表格控件可以同时有多个选中区域,获取表格当前选中区域列表的函数如下:<br>
<div class="code">QList&lt;QTableWidgetSelectionRange&gt;
QTableWidget::​selectedRanges() const</div>
一般用 Shift 键选中的连续矩形区域对应一个选中区域,而使用 Ctrl 键多次点击选中的多个离散单元格对应多个选中区域。<br>
QTableWidget 也能通过代码设置某个矩形区域是否为高亮选中:<br>
<div class="code">void QTableWidget::​setRangeSelected(const
QTableWidgetSelectionRange &amp; range, bool select)</div>
<br>
QTableWidgetSelectionRange 类的构造函数可以指定矩形区域,是以单元格的行列号标出矩形区域:<br>
<div class="code">QTableWidgetSelectionRange(int top, int left, int bottom,
int right)</div>
<div class="code">QTableWidgetSelectionRange(const
QTableWidgetSelectionRange &amp; other) //复制构造函数</div>
参数里的 top 是矩形区域最上面的行号,bottom 是最下面的行号;left 是矩形选择区域最左边的列号,right 是矩形最右边的列号。如果
top 和 bottom 数值一样,同时 left 与 right 数值一样,那么矩形区域就退化为一个单元格了。<br>
QTableWidgetSelectionRange 的功能函数也比较简单,就是获取上下行号、左右列号、行列计数:<br>
<div class="code">int&nbsp;&nbsp; &nbsp;topRow() const //最上面的行号</div>
<div class="code">int&nbsp;&nbsp; &nbsp;bottomRow() const //最下面的行号</div>
<div class="code">int&nbsp;&nbsp; &nbsp;leftColumn() const //最左边的列号</div>
<div class="code">int&nbsp;&nbsp; &nbsp;rightColumn() const //最右边的列号</div>
<div class="code">int&nbsp;&nbsp; &nbsp;rowCount() const //区域内行计数</div>
<div class="code">int&nbsp;&nbsp; &nbsp;columnCount() const //区域内列计数</div>
QTableWidgetSelectionRange 比较简单,只能在构造函数设置矩形范围,没有其他修改矩形范围的函数。如果要修改矩形选中区域,可以直接
新创建一个矩形选中区域。<br>
<br>
QTableWidgetSelectionRange 是表格控件 QTableWidget
独有的东西,接下来介绍选中行为、选中模式和单次选中命令,后面三项内容对本章所有基于条目控件都适用的,在这里统一介绍。<br>
<br>
(2)选中行为<br>
表格控件默认情况下,点击一个单元格只会选中该单元格本身,如果表格的行内部数据关联性很紧密,比如是同一个人的各种身份信息(姓名、性别、住址等),这时候用户
可能希望点击一次选中整行的单元格,这就是选中行为决定的特性。表格控件从祖辈基类 QAbstractItemView 继承了
selectionBehavior 属性,其获取和设置函数如下:<br>
<div class="code">QAbstractItemView::SelectionBehavior&nbsp;&nbsp;
&nbsp;selectionBehavior() const</div>
<div class="code">void&nbsp;&nbsp;
&nbsp;setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)</div>
调用设置函数就可以改变选中行为,具体的选中行为枚举常量如下表所示:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 360px;" align="center"><b>QAbstractItemView::SelectionBehavior
枚举常量</b></td>
<td style="width: 140px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>QAbstractItemView::SelectItems</td>
<td> 0 </td>
<td> 鼠标点击时只选中该条目,这个是默认值。 </td>
</tr>
<tr class="d1">
<td>QAbstractItemView::SelectRows</td>
<td> 1 </td>
<td> 鼠标点击时选中整行的条目。 </td>
</tr>
<tr>
<td>QAbstractItemView::SelectColumns </td>
<td> 2 </td>
<td> 鼠标点击时选中整列的条目。 </td>
</tr>
</tbody>
</table>
<br>
如果我们希望用户点击时自动选中整行,那么把选中行为设置为 QAbstractItemView::SelectRows 即可。<br>
对于 8.1 节列表控件和 8.3 节树形控件,它们也有选中行为属性,但是它们每行只有一个条目,所以一般用不到选中行为属性。<br>
<br>
(3)选中模式<br>
对于表格控件,默认就支持使用 Ctrl 和 Shift 键进行多选操作,而 8.1
节的列表控件默认是单选模式,决定到底是单选还是多选,就是选中模式决定的特性。表格控件、列表控件以及后文的属性控件都从祖辈基类
QAbstractItemView 继承了 selectionMode 属性,其获取和设置函数如下:<br>
<div class="code">QAbstractItemView::SelectionMode&nbsp;&nbsp;
&nbsp;selectionMode() const</div>
<div class="code">void&nbsp;&nbsp;
&nbsp;setSelectionMode(QAbstractItemView::SelectionMode mode)</div>
选中模式的枚举常量比较多,列举描述如下:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 360px;" align="center"><b>QAbstractItemView::SelectionMode
枚举常量</b></td>
<td style="width: 140px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>QAbstractItemView::SingleSelection</td>
<td> 1 </td>
<td> 单选模式。用户点击选中新条目时,旧的选中条目取消选中状态。只用鼠标点击不能取消选中当前条目,鼠标点击总有一个条目是高亮选中的。</td>
</tr>
<tr class="d1">
<td>QAbstractItemView::ContiguousSelection</td>
<td> 4 </td>
<td> 连续多选模式。平常点击与单选模式特性一样;但如果用户按下 Shift 键同时点击条目,可以进行连续区域的选中或取消选中。以按下
Shift键之前的当前条目为基准条目,基准条目与Shift键按下后最后一次点击的条目之间的连续区域会被选中,其他条目取消选中。 </td>
</tr>
<tr>
<td>QAbstractItemView::ExtendedSelection </td>
<td> 3 </td>
<td> 扩展多选模式。平常点击与单选模式特性一样;但如果用户按下Shift键同时点击条目可以选中连续区域;如果用户按下Ctrl键可以选中或
取消选中离散条目的高亮选中状态。另外,鼠标左键按下后连续拖动也可以选中连续的区域。 </td>
</tr>
<tr class="d1">
<td>QAbstractItemView::MultiSelection</td>
<td> 2 </td>
<td> 普通多选模式。用户点击某个条目时,该条目的选中状态取反:如果之前是选中就变为非选中,如果之前是非选中就变为选中。鼠标左键按下后连续
拖动可以选中或取消选中连续区域。</td>
</tr>
<tr>
<td>QAbstractItemView::NoSelection </td>
<td> 0 </td>
<td> 无选模式。不能选中条目,这个很少用到。 </td>
</tr>
</tbody>
</table>
<br>
对于 8.1 列表控件和 8.3 树形控件,默认选中模式为单选模式 SingleSelection ,本节的表格控件默认是扩展多选模式
ExtendedSelection ,操作系统的文件资源管理器一般都是扩展多选模式。程序中常用的就是单选模式、扩展多选模式。<br>
<br>
(4)单次选中命令<br>
通常使用上面介绍的选中行为和选中模式就能满足绝大多数情况下的选中特性需求,但也有少数例外的情况,比如我们不希望修改控件默认的选中行为和选中模式,但又想使
用默认选中行为和选中模式规定之外的选中操作,那就涉及到单次选中命令这一特性了。对于列表控件、表格控件以及后面的树形控件,都有类似下面的函数:<br>
<div class="code">void setCurrentItem(QTableWidgetItem * item,
QItemSelectionModel::SelectionFlags command)</div>
setCurrentItem() 函数第一个参数是条目指针,第二个参数就是单次选中命令(可以是一个标志位或者多个选中标志位的或值),通过
setCurrentItem() 函数可以在控件默认的选中行为和选中模式之外进行一些特定的选中或取消选中的操作。<br>
<br>
在解释单次选中命令之前,我们先明确一下“当前条目”和“选中条目”的区别:<br>
当前条目通常是指鼠标最后一次点击的条目,这个条目是唯一的,在图形上用虚线框标记,当前条目可以处于非选中状态,也可以处于选中状态,如下面两个图所示(假定下面两个图
的所有单元格都有条目,非 NULL):<br>
<center> <img src="images/ch08/ch08-02-06.png" alt="curItem1"> </center>
<center> <img src="images/ch08/ch08-02-07.png" alt="curItem2"> </center>
当前条目通常情况下都是用户鼠标最后一次点击的条目,但 setCurrentItem()
函数也可以把当前条目设置为参数里指定的非空条目。获取当前条目的函数是 currentItem() 。<br>
<br>
②选中条目就是从图形界面上看到的有高亮背景的条目,选中条目可以没有,可以有一个,也可以有多个。对于没有选中、选中一个的情况,看上面两个图即可。<br>
高亮选中的多个条目可以与当前条目有重叠,也可以不重叠,如下面两个图示范(假定下面两个图的所有单元格都有条目,非 NULL):<br>
<center> <img src="images/ch08/ch08-02-08.png" alt="selected1"> </center>
<center> <img src="images/ch08/ch08-02-09.png" alt="selected2"> </center>
获取高亮选中的条目函数为 selectedItems(),设置某个条目选中或非选中,可以直接用条目自身的函数 item-&gt;setSelected(
bool ) 。<br>
<br>
归纳一下就是:<span style="font-weight: bold;">图形界面上虚线框包裹的唯一条目,是当前条目;拥有高亮选中背景的是选中条
目,选中条目可以没有,也可以有一个或多个。当前条目与选中条目可以有重叠,也可以无重叠。</span><br>
<br>
解释完当前条目和选中条目,下面介绍具体的选中标志位。一个选中标志位或者多个选中标志位的或值 构成单次选中命令,选中标志位的枚举常量如下表描述:<br>
<br>
<table class="tabel">
<tbody>
<tr class="d1">
<td style="width: 360px;" align="center"><b>QItemSelectionModel::​
SelectionFlag 枚举常量</b></td>
<td style="width: 140px;" align="center"><b>数值</b></td>
<td align="center"><b> 描述</b></td>
</tr>
<tr>
<td>QItemSelectionModel::NoUpdate</td>
<td> 0x0000</td>
<td> 保持旧的选中状态,不更新选中条目。 </td>
</tr>
<tr class="d1">
<td>QItemSelectionModel::Clear</td>
<td> 0x0001 </td>
<td> 清除所有选中状态,没有选中条目了。 </td>
</tr>
<tr>
<td>QItemSelectionModel::Select </td>
<td> 0x0002 </td>
<td style="height: 16px;"> 选中指定的条目。 </td>
</tr>
<tr class="d1">
<td>QItemSelectionModel::Deselect</td>
<td> 0x0004 </td>
<td style="height: 16px;"> 指定的条目取消选中状态。 </td>
</tr>
<tr>
<td>QItemSelectionModel::Toggle </td>
<td> 0x0008 </td>
<td> 指定条目的选中状态取反,原先选中就不选中,原先不选中就选中。 </td>
</tr>
<tr class="d1">
<td>QItemSelectionModel::Current</td>
<td> 0x0010 </td>
<td> 将指定的条目设为当前条目。 </td>
</tr>
<tr>
<td>QItemSelectionModel::Rows </td>
<td> 0x0020 </td>
<td> 对指定条目所在的整行进行选中或非选中。 </td>
</tr>
<tr class="d1">
<td>QItemSelectionModel::Columns</td>
<td> 0x0040 </td>
<td> 对指定条目所在的整列进行选中或非选中。 </td>
</tr>
<tr>
<td>QItemSelectionModel::SelectCurrent </td>
<td> Select | Current </td>
<td> 选中指定条目,并且将其设置为当前条目。 </td>
</tr>
<tr class="d1">
<td>QItemSelectionModel::ToggleCurrent</td>
<td> Toggle | Current </td>
<td> 指定条目的选中状态取反,并将其设置为当前条目。 </td>
</tr>
<tr>
<td>QItemSelectionModel::ClearAndSelect </td>
<td> Clear | Select </td>
<td> 先清除旧的选中状态,然后设置指定条目为选中状态。 </td>
</tr>
</tbody>
</table>
<br>
选中标志位的按位或值,一般要考虑实际意义会不会冲突,比如 Select、Deselect、Toggle
三者是冲突的,不应该同时用。意义不冲突的标志位就可以进行位或,比如 Select | Rows 就是整行选中。<br>
在不修改选中行为和选中模式的时候,下面一句代码就会选中指定条目所在的整行:<br>
<div class="code">ui-&gt;tableWidget-&gt;setCurrentItem(pItem ,
QItemSelectionModel::Select | QItemSelectionModel::Rows);</div>
setCurrentItem() 会自动把当前条目设置为参数里指定的非空条目。<br>
如果需要清空所有旧的选中状态,可以用下面这句:<br>
<div class="code">ui-&gt;tableWidget-&gt;setCurrentItem(NULL,
QItemSelectionModel::Clear);</div>
setCurrentItem() 参数里指定的条目是 NULL 时,控件自动把当前条目设置为打头的第 0 个条目,对应到表格就是第 0 行 0
列的单元格。<br>
<br>
本小节的选中行为、选中模式、单次选中命令对列表控件、表格控件和树形控件都是通用的,而且以后讲到模型和视图章节时,这三小块内容对列表视图、表格视图、树形视
图也是通用的。<br>
关于表格控件的知识点介绍就到这里,下面来通过两个例子操练一下。<br>
<br>
<div class="os2">8.2.5 个人信息表格示例</div>
<br>
本小节展示利用表格控件显示个人信息,表格共有姓名、性别、生日、婚否和住址五列内容,可以对表格按行增加或删除个人信息记录。在开始例子之前,先下载如下网络文
件夹里的五个图片文件,作为个人信息里的头像使用:<br>
<a href="https://lug.ustc.edu.cn/sites/qtguide/QtProjects/ch08/personinfo/images/"
target="new">
https://lug.ustc.edu.cn/sites/qtguide/QtProjects/ch08/personinfo/images/ </a>
<br>
<br>
我们打开 QtCreator,新建一个 Qt Widgets Application 项目,在新建项目的向导里填写:<br>
①项目名称 personinfo,创建路径 D:\QtProjects\ch08,点击下一步;<br>
②套件选择里面选择全部套件,点击下一步;<br>
③基类选择 QWidget,点击下一步;<br>
④项目管理不修改,点击完成。<br>
在编辑界面之前,我们在项目文件夹 D:\QtProjects\ch08\personinfo\ 里新建一个 images
子文件夹,把下载的五个图片文件放到 images 子文件夹里面。然后在 QtCreator 里,右击左边栏里的 personinfo
项目名,在右键菜单选择“添加新文件”:<br>
<center> <img src="images/ch08/ch08-02-10.png" alt="qrc01" width="800"> </center>
在新建文件对话框,左边选择“Qt”,中间选择“Qt Resource File”,点击右下角 Choose 按钮,进入下面界面:<br>
<center> <img src="images/ch08/ch08-02-11.png" alt="qrc02" width="800"> </center>
编辑文件名为 photos,点击“下一步”,进入如下界面:
<center> <img src="images/ch08/ch08-02-12.png" alt="qrc03" width="800"> </center>
点击“完成”,然后左边栏 personinfo 项目管理的资源子文件夹里找到 photos.qrc 文件,右击 photos.qrc
文件名,右键菜单选择“添加现有文件”,把文件系统里 D:\QtProjects\ch08\personinfo\images\
目录里五个图片文件都添加到资源里:<br>
<center> <img src="images/ch08/ch08-02-13.png" alt="qrc04" width="800"> </center>
这样图片都添加到资源文件 photos.qrc 里了。以后章节示例遇到需要添加 *.qrc 和添加图片到 *.qrc 里的时候,就简单描述,不再逐个步骤
截图示范了。<br>
我们按 Ctrl+S 快捷键保存 photos.qrc 文件,并关闭该资源文件。<br>
双击打开 widget.ui 界面文件,把主界面的窗口大小调整为 400*360,然后按照下图拖入控件:<br>
<center> <img src="images/ch08/ch08-02-14.png" alt="ui" width="800"> </center>
界面共五行控件,第一行是表格控件,对象名 tableWidget。<br>
第二行,标签控件,文本为 "姓名";单行编辑器,对象名 lineEditName;标签控件,文本为 "头像";组合框,对象名
comboBoxPhotos 。第二行按照水平布局器布局。<br>
第三行,标签控件,文本为 "性别";单行编辑器,对象名 lineEditGender;标签控件,文本为 "生日";日期编辑器控件,对象名为
dateEdit;复选框,文本为 "婚否",对象名为 checkBoxIsMarried,复选框勾选代表已婚,没勾选代表未婚。第三行也按水平布局器布局。<br>
第四行,标签控件,文本为 "住址";单行编辑器,对象名为 lineEditAddress。第四行也按照水平布局器布局。<br>
第五行两边的是水平空白条,中间是两个按钮,第一个按钮文本 "添加",对象名 pushButtonAdd,第二个按钮文本 "删除" ,对象名
pushButtonDel。第五行也是按照水平布局器布局。<br>
最后对于主界面,点击空白位置,只选中主界面的窗口本身,然后点击上面的垂直布局按钮,为主界面设置主布局器,布局好之后的界面如下:<br>
<center> <img src="images/ch08/ch08-02-15.png" alt="ui2" width="800"> </center>
我们首先右击头像组合框,右键菜单选择“编辑项目”,为组合框添加五个条目,条目的文本为空格字符 " ",条目的图标就是资源里面的五个图片,如下图所示:<br>
<center> <img src="images/ch08/ch08-02-16.png" alt="combo"> </center>
设置好组合框的条目后,点击“OK”按钮回到主界面。<br>
然后我们在主界面右击表格控件 tableWidget,右键菜单选择“编辑项目”,弹出表格控件的编辑界面:<br>
<center> <img src="images/ch08/ch08-02-17.png" alt="table1"> </center>
表格控件的编辑对话框上方是标签页选择,可以编辑列(列首,即水平表头)、行(行首,即垂直表头)、项目(单元格条目)。对话框下方,左边是增加条目、删除条目按
钮,中间是控制条目上移或下移,右边“属性”按钮点开后看到条目的详细内容设置界面。<br>
这个示例中,我们只设置列首(水平表头),行首不设置(用自动行号),按照如下操作,先编辑列首,添加五个列:<br>
<center> <img src="images/ch08/ch08-02-18.png" alt="table2"> </center>
添加"姓名"、"性别"、"生日"、"婚否"、"住址" 五个列后,点击对话框右下角 OK 按钮,回到主界面,看到如下效果:<br>
<center> <img src="images/ch08/ch08-02-19.png" alt="table3" width="800"> </center>
我们刚才添加的五个列首,现在变成了 "新建列"、"姓名"、"性别"、"生日"、"住址" ,有一个列首被
QtCreator设计模式弄丢了,顺序也错位了,这是 QtCreator设计模式也就是 Qt 设计师的 bug 。<span style="font-weight: bold;">
以更推荐用代码设置表格控件的列首和行首,使用代码设置列首和行首是没问题的,只有 QtCreator设计模式和 Qt 设计师有这样的 bug。</span><br>
<br>
为了修正表头,我们重新右击表格控件,在右键菜单选择“编辑项目”,再弹出表格控件的编辑界面。这次把错误的 "新建列" 文字修改为
"婚否",然后选中"婚否",点击下方中间向下按钮调整 "婚否" 列首的顺序,调整后如下图所示:<br>
<center> <img src="images/ch08/ch08-02-20.png" alt="table4"> </center>
经过第二次编辑后,点击对话框右下角 OK 按钮,然后我们回到主界面,可以看到正确的列首:<br>
<center> <img src="images/ch08/ch08-02-21.png" alt="table5" width="800"> </center>
表头设置正确之后,我们选中表格控件,在 QtCreator 右下角的属性栏可以看到这时候的 rowCount 为 0,columnCount 为 5。<br>
对于 QtCreator 设计模式和 Qt 设计师,如果希望使用自动编号的数字表头,那么就修改 rowCount 或 columnCount
数值,自动生成垂直表头或水平表头。如果不希望使用自动编码的表头,那么就右击表格控件选择 “编辑项目”,然后手动编辑列首或行首。<br>
<br>
注意,对于水平表头,如果决定使用自动编号,那么就不要在编辑表格控件的对话框里额外添加列首;如果决定在编辑表格控件的对话框里手动编辑列首(即水平表头),那
么就不要再修改列计数 columnCount 。<span style="font-weight: bold;">对于同一方向表头,不要既使用自动编号,
又手动编辑表头,那样导致表头混乱而又无法修改自动表头文本</span>,因为QtCreator设计模式和Qt设计师对自动表头和手动表头编辑二者不兼
容,也有 bug,就如下图错误示范:<br>
<center> <img src="images/ch08/ch08-02-22.png" alt="tableerror" width="800">
</center>
如上图示范的三个步骤,先设置手动列首,再增加自动列首,然后就发现自动增加的两个列首 "3"、"4"
根本无法编辑,表格编辑对话框是看不到自动编号表头文本的。(表格编辑对话框只能编辑 QTableWidget 的手动表头条目
QTableWidgetItem 对象,自动表头有文本但没有表头条目 QTableWidgetItem 对象,因为自动编号表头是从基类
QTableView 继承的,无法在设计师里编辑。)<br>
<span style="font-weight: bold; color: red;">最好的办法还是用代码设置表头,这样不会有这些莫名其妙的问题。</span><br>
<br>
现在回到这个例子,我们的水平表头,也就是行首设置好之后,我们把表格控件的垂直表头设置自动的编号,就是修改 rowCount 属性为
2。这时候水平表头是手动编辑的,垂直表头是全自动编号的,二者不在一个维度,因此不会有冲突:<br>
<center> <img src="images/ch08/ch08-02-23.png" alt="table6" width="800"> </center>
这时候表格为 2 行 5 列的,既有行计数,也有列计数,就能编辑单元格条目了。我们右击表格控件,右键菜单选择“编辑项目”,第三次进入表格编辑对话框,在表
格编辑对话框点击“项目”标签页,就可以编辑单元格了:<br>
<center> <img src="images/ch08/ch08-02-24.png" alt="table7" width="800"> </center>
我们把姓名一列的单元格既设置文本,也设置图标作为头像,其他列的单元格如下图所示设置:<br>
<center> <img src="images/ch08/ch08-02-25.png" alt="table8" width="800"> </center>
婚否一列的单元格,文本是空的,然后把单元格 checkState 属性设置成 Checked,就会如上图所示,勾选代表已婚的。<br>
设置好单元格之后,点击 OK 按钮,回到主界面:<br>
<center> <img src="images/ch08/ch08-02-26.png" alt="table9" width="800"> </center>
关于控件的编辑就是上面描述的,下面我们为控件添加信号对应的槽函数。<br>
右击表格控件,右键菜单选择“转到槽”,为表格控件添加 currentItemChanged 信号对应的槽函数:<br>
<center> <img src="images/ch08/ch08-02-27.png" alt="slot1"> </center>
然后我们为 "添加"、"删除" 两个按钮都添加 clicked 信号对应的槽函数:<br>
<center> <img src="images/ch08/ch08-02-28.png" alt="slot2-3"> </center>
添加如上三个槽函数后,保存 widget.ui 界面文件,然后关闭该文件,回到代码编辑模式。<br>
在代码编辑模式,我们先编辑头文件 widget.h,添加一行头文件包含:<br>
<div class="code"><span style=" color:#000080;">#ifndef</span><span style=" color:#c0c0c0;">
</span>WIDGET_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;">WIDGET_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;QTableWidgetItem&gt;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">//头文件槽函数使用到了</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;">Widget</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;">Widget</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;">Widget</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;">Widget</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_tableWidget_currentItemChanged</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span>current<span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span>previous<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_pushButtonAdd_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_pushButtonDel_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:#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;">Widget</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:#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;">WIDGET_H</span></pre>
</div>
表格控件信号对应的槽函数里使用到了条目类,因此需要增加头文件包含 &lt;QTableWidgetItem&gt;,其他代码都是自动生成的,不需要修改。<br>
接下来编辑源文件 widget.cpp,首先来看头文件包含和构造函数的内容:<br>
<div class="code"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;">
</span><span style=" color:#008000;">"widget.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_widget.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=" 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;QHeaderView&gt;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">//表头视图类</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:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">Widget</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;">Widget</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=" 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;">dateEdit</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setCalendarPopup</span><span
style=" color:#000000;">(</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></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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setSelectionBehavior</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QAbstractItemView</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">SelectRows</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setSelectionMode</span><span
style=" color:#000000;">(</span><span style=" color:#800080;">QAbstractItemView</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">SingleSelection</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setSortingEnabled</span><span
style=" color:#000000;">(</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:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">sortByColumn</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">0</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Qt</span><span style=" color:#000000;">::</span><span
style=" color:#800080;">AscendingOrder</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">horizontalHeader</span><span
style=" color:#000000;">()-&gt;</span><span style=" color:#000000;">setStretchLastSection</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">true</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;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">verticalHeader</span><span
style=" color:#000000;">()-&gt;</span><span style=" color:#000000;">setDefaultSectionSize</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">36</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setIconSize</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QSize</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">32</span><span style=" color:#000000;">,</span><span
style=" color:#000080;">32</span><span style=" color:#000000;">)</span><span style=" color:#c0c0c0;"> </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:#800080;">QSize</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">sz</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">this</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">size</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;">sz</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">setWidth</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">640</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;">this</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">resize</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">sz</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:#800080;">Widget</span><span style=" color:#000000;">::~</span><span
style=" font-style:italic; color:#000000;">Widget</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:#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>
头文件包含增加了调试类、消息框类和表格控件的表头视图类。<br>
构造函数先设置日期编辑器可以弹出日历 setCalendarPopup(true),方便用户选择日子。<br>
然后对表格控件进行了如下设置:<br>
设置选择行为 QAbstractItemView::SelectRows,每次点击都是按照整行选择;<br>
设置选择模式为 QAbstractItemView::SingleSelection,即单选模式(方便删除按钮操作),单选与整行选择行为不冲突,单选加整
行选中的意思,就是每次点击只能选中一行,而不能用 Shift 或 Ctr l键选中多行;<br>
开启自动排序,并且按照第 0 行升序排序;<br>
设置表格末尾列自动拉伸,这样表格控件比较宽时,最后一列自动占据表格控件剩余水平空间;<br>
设置默认的行高为 36 像素,然后把单元格的默认图标尺寸设置为 32*32 。<br>
对表格控件进行如上设置后,我们把主界面的窗口宽度拉伸至640,方便显示拉伸表格末尾列的效果。<br>
<br>
然后我们编辑监视表格控件当前条目的槽函数:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_tableWidget_currentItemChanged</span><span style=" color:#000000;">(</span><span
style=" color:#800080;">QTableWidgetItem</span><span style=" color:#c0c0c0;">
</span><span style=" color:#000000;">*</span><span style=" color:#000000;">current</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QTableWidgetItem</span><span style=" color:#c0c0c0;">
</span><span style=" color:#000000;">*</span><span style=" color:#000000;">previous</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:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">current</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">!=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">NULL</span><span style=" color:#000000;">)</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;">{</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;">qDebug</span><span
style=" color:#000000;">()&lt;&lt;</span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"当前条目行号:%1,列号:%2,文本:%3"</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=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">current</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">row</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=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">current</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">column</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=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">current</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">text</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>
这个槽函数就是打印非空的当前条目信息,对于涉及到指针的操作,一定要先判断指针非空。<br>
对于非空的当前条目,打印它的行号、列号文本。<br>
<br>
接下来我们编辑 "添加" 按钮对应的槽函数:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonAdd_clicked</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:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</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;">lineEditName</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><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:#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:#000000;">strName</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:#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:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setSortingEnabled</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">false</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:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nOldRowCount</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;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">rowCount</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">insertRow</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nOldRowCount</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:#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;">QIcon</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">iconHead</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;">comboBoxPhotos</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">itemIcon</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;">comboBoxPhotos</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">currentIndex</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:#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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">iconHead</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nOldRowCount</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemName</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strGender</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;">lineEditGender</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">text</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemGender</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strGender</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nOldRowCount</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">1</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemGender</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strBirthday</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;">dateEdit</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">date</span><span style=" color:#000000;">().</span><span
style=" color:#000000;">toString</span><span style=" color:#000000;">(</span><span
style=" color:#008000;">"yyyy/MM/dd"</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemBirthday</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strBirthday</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nOldRowCount</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">2</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemBirthday</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;">Qt</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">CheckState</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">cks</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;">checkBoxIsMarried</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">checkState</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemIsMarried</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</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;">itemIsMarried</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setCheckState</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">cks</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nOldRowCount</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemIsMarried</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strAddress</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;">lineEditAddress</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">text</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemAddress</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strAddress</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nOldRowCount</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">4</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemAddress</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;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setSortingEnabled</span><span
style=" color:#000000;">(</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:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">sortByColumn</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">0</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Qt</span><span style=" color:#000000;">::</span><span
style=" color:#800080;">AscendingOrder</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;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setCurrentItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">itemName</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">scrollToItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">itemName</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>
然后我们获取表格控件的行计数 nOldRowCount ,然后在表格末尾插入一行,新的末尾行号就成了 nOldRowCount ,末尾行默认是全
NULL 的条目,有了新的空行才能进行后续的设置新条目操作。<br>
接下来我们获取头像图标,并与姓名字符串一起构建姓名条目,添加到末尾行第 0 列;<br>
获取性别文本,新建条目并添加到末尾行第 1 列;<br>
获取生日文本,新建条目并添加到末尾行第 2 列;<br>
获取婚否复选框的状态,新建条目并设置选状态,添加到末尾行第 3 列;<br>
获取住址文本,新建条目并添加到末尾行第 4 列。<br>
添加该行的所有条目之后,就可以开启自动排序,设置按照第 0 列升序排列。<br>
最后设置新的当前条目为刚才新增的姓名条目,并且滚动显示出该姓名条目,在表格的行很多时,自动滚动到设定的条目就很有用了。<br>
<br>
最后我们来看看删除 "按钮" 的代码:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonDel_clicked</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:#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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">curItem</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;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">currentItem</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;">curItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">!=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">NULL</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:#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;">curItem</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">isSelected</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">removeRow</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">curItem</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">row</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:#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>
因为之前在主窗体构造函数设置了单选模式,所以只需要删除一行,而且单选模式的选中行默认都是和当前条目重叠的。<br>
<br>
例子代码介绍完了,下面运行例子看看:<br>
<center> <img src="images/ch08/ch08-02-29.png" alt="run1"></center>
经过主窗体构造函数的调整,单元格的图标明显变大了很多,表格末尾 "住址"
一列自动占据了剩余空间,表格内部显得比较充实。如果我们新加一个行,会变成下面这样,自动排序是生效的:
<center> <img src="images/ch08/ch08-02-30.png" alt="run2"></center>
"删除" 按钮读者可以自行测试,另外鼠标点击某个单元格时,当前单元格的信息会打印到应用程序输出面板。<br>
最后说明一下,主窗体没有修改行或单元格的按钮,因为双击任意单元格都可以编辑文本内容,一般用不着编辑按钮。关于本小节的例子介绍到这,下面来看看第二个例子。<br>
<br>
<div class="os2">8.2.6 成绩信息表示例</div>
<br>
本小节编写一个记录学生成绩的表格示例,学生的姓名放在每行的行首,列首是各课程名字(行首、列首均用代码添加),每个单元格就是某学生某课程的成绩,支持按照列
查询成绩并将符合条件的单元格高亮显示。<br>
我们重新打开 QtCreator,新建一个 Qt Widgets Application 项目,在新建项目的向导里填写:<br>
①项目名称 scores,创建路径 D:\QtProjects\ch08,点击下一步;<br>
②套件选择里面选择全部套件,点击下一步;<br>
③基类选择 QWidget,点击下一步;<br>
④项目管理不修改,点击完成。<br>
打开 widget.ui 文件,进行 QtCreator 设计模式,先把主窗体尺寸修改为 480*400,然后按照下图拖入控件:<br>
<center> <img src="images/ch08/ch08-02-31.png" alt="ui1" width="800"></center>
界面第一行是表格控件,对象名 tableWidget。<br>
第二行:标签控件,文本为 "指定列";组合框,对象名 comboBoxColumns;标签控件,文本 "分值";组合框,对象名
comboBoxCompares;单行编辑器,对象名 lineEditFindText;按钮,文本为 "查找",pushButtonFind
。第二行控件按照水平布局器布局。<br>
第三行:标签控件,文本为 "姓名";单行编辑器,对象名 lineEditName;按钮,文本为 "增加行",对象名
pushButtonAddLine;按钮,文本为 "删除选中行",对象名 pushButtonDelLines;按钮,文本为 "删除选中条目",对象名
pushButtonDelSelectedItems 。第三行也按照水平布局器布局。<br>
主窗体的主布局器是按照垂直布局器排布。<br>
<br>
表格控件的内容等会我们用代码来填充,这里编辑一下两个组合框的内容,对于 "指定列" 右边相邻的组合框,右击组合框并在右键菜单选择 “编辑项目”,为
comboBoxColumns 组合框增加四个项:<br>
<center> <img src="images/ch08/ch08-02-32.png" alt="ui2"></center>
然后我们右击 "分值" 右边的组合框 comboBoxCompares,为第二个组合框添加三个比较项:<br>
<center> <img src="images/ch08/ch08-02-33.png" alt="ui3"></center>
设置好两个组合框之后,主界面如下图所示:<br>
<center> <img src="images/ch08/ch08-02-34.png" alt="ui4" width="800"></center>
主窗体第二行的控件就是在指定列内,根据分值比较条件来查询符合条件的单元格,并高亮显示出来。<br>
第三行控件是根据姓名添加新的空行,用户可以双击单元格编辑空单元格的成绩,因此没有编辑单元格的按钮;<br>
"删除选中行" 是连行首的表头分段一起整行删除,而 "删除选中条目" 只删除选中区域的单元格条目。<br>
<br>
我们现在添加控件信号对应的槽函数,首先右击表格控件,添加 itemSelectionChanged
信号对应的槽函数,这个槽函数用于监视选中条目和选中矩形区域的变化:<br>
<center> <img src="images/ch08/ch08-02-35.png" alt="slot1"></center>
然后我们为四个按钮分别添加各自的 clicked 信号对应的槽函数:<br>
<center> <img src="images/ch08/ch08-02-36.png" alt="slot2-5"></center>
添加完槽函数后,我们保存界面文件 widget.ui,然后关闭该文件,回到代码编辑模式。<br>
首先编辑头文件 widget.h 的代码,增加两个功能函数声明:<br>
<div class="code"><span style=" color:#000080;">#ifndef</span><span style=" color:#c0c0c0;">
</span>WIDGET_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;">WIDGET_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="-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;">Widget</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;">Widget</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;">Widget</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;">Widget</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_tableWidget_itemSelectionChanged</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_pushButtonFind_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_pushButtonAddLine_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_pushButtonDelLines_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_pushButtonDelSelectedItems_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:#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;">Widget</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:#808000;">void</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">InitScoresTable</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;">SetTableRow</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>nRow<span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span>strName<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;">QString</span><span
style=" color:#c0c0c0;"> </span>strChinese<span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span>strMath<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;">QString</span><span
style=" color:#c0c0c0;"> </span>strForeignLanguage<span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span>strFood<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;">WIDGET_H</span></pre>
</div>
Widget 类声明末尾增加了两个私有函数,InitScoresTable() 是用于初始化表格控件,SetTableRow()
根据参数设置表格控件指定行号的行首文本以及四个单元格条目文本。<br>
然后编辑 widget.cpp 源代码文件,先添加头文件包含和构造函数内容:<br>
<div class="code"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;">
</span><span style=" color:#008000;">"widget.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_widget.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=" 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;QTableWidgetItem&gt;</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:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">&lt;QTableWidgetSelectionRange&gt;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">//表格选中区域</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:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">Widget</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;">Widget</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=" 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;">InitScoresTable</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:#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:#800080;">Widget</span><span style=" color:#000000;">::~</span><span
style=" font-style:italic; color:#000000;">Widget</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:#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>
头文件包含增加了调试类、消息框类、表格控件条目类和表格选中区域类。<br>
构造函数里调用了 InitScoresTable()
函数对表格控件进行初始化,本小节示例没使用排序,也没有修改表格控件其他特性,是默认的表格控件属性。由于行首不参与实时排序,如果排序会导致行首姓名与单元格分数错
位,所以不能开启排序功能。<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;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">InitScoresTable</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><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">2</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;"></span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">4</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setRowCount</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">2</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setColumnCount</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">4</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;">QStringList</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listHeaders</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;">listHeaders</span><span
style=" color:#000000;">&lt;&lt;</span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"语文"</span><span style=" color:#000000;">)&lt;&lt;</span><span
style=" color:#000000;">tr</span><span style=" color:#000000;">(</span><span style=" color:#008000;">"数学"</span><span
style=" color:#000000;">)&lt;&lt;</span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"外语"</span><span style=" color:#000000;">)&lt;&lt;</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:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setHorizontalHeaderLabels</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">listHeaders</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:#008000;">//第0行的行首和单元格</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;">SetTableRow</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">0</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;">"66"</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;">"77"</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;">"88"</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;">"99"</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;">//第1行的行首和单元格</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;">SetTableRow</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">1</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;">"99"</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;">"88"</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;">"77"</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;">"66"</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>
表格初始化函数先把表格的维度设成 2 行 4 列的,然后设置水平表头(就是四个列首)的文本为四门课程名。<br>
然后调用函数 SetTableRow() 为第 0 行设置行首文本和四个单元格文本;<br>
再调用函数 SetTableRow() 为第 1 行设置行首文本和四个单元格文本。<br>
因为表格设置某行的操作比较多,而且代码类似,重复使用的多,因此将设置行内容单独编一个函数 SetTableRow() 方便调用。
SetTableRow() 函数的内容如下:<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;">Widget</span><span
style=" color:#000000;">::</span><span style=" color:#000000;">SetTableRow</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">nRow</span><span style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#800080;">QString</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strName</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strChinese</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strMath</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strForeignLanguage</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strFood</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemName</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strName</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setVerticalHeaderItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nRow</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemName</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemChinese</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strChinese</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nRow</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemChinese</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemMath</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strMath</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nRow</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">1</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemMath</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemForeignLanguage</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strForeignLanguage</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nRow</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">2</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemForeignLanguage</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemFood</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strFood</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nRow</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemFood</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>
SetTableRow() 函数第一个参数是行号,第二个参数是行首的字符串,后面四个参数是四个单元格的文本。该函数先以 strName
构造一个表格条目,设置为垂直表头的条目(即行首);<br>
然后分别根据 strChinese、strMath、strForeignLanguage、strFood 构造四个条目,设置到对应的单元格中。<br>
<br>
接下来编写监视表格控件选中区域变化的槽函数:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_tableWidget_itemSelectionChanged</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:#800080;">QList</span><span
style=" color:#000000;">&lt;</span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">*&gt;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listItems</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">selectedItems</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;">nItemsCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listItems</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">count</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:#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;">QList</span><span
style=" color:#000000;">&lt;</span><span style=" color:#800080;">QTableWidgetSelectionRange</span><span
style=" color:#000000;">&gt;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listRanges</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;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">selectedRanges</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;">nRangesCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listRanges</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">count</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;">nCellsCount</span><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:#808000;">for</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">i</span><span style=" color:#000000;">=</span><span style=" color:#000080;">0</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</span><span
style=" color:#000000;">&lt;</span><span style=" color:#000000;">nRangesCount</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</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;">nCellsCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">+=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">(</span><span style=" color:#000000;">listRanges</span><span
style=" color:#000000;">[</span><span style=" color:#000000;">i</span><span style=" color:#000000;">].</span><span
style=" color:#000000;">rowCount</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;">(</span><span style=" color:#000000;">listRanges</span><span
style=" color:#000000;">[</span><span style=" color:#000000;">i</span><span style=" color:#000000;">].</span><span
style=" color:#000000;">columnCount</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:#000080;">qDebug</span><span
style=" color:#000000;">()&lt;&lt;</span><span style=" color:#000000;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"选中条目数:%1,选中区域数:%2,选中单元格数:%3"</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=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">nItemsCount</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=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">nRangesCount</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=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">nCellsCount</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>
这个槽函数先获取选中的条目列表 listItems ,获知选中条目计数 nItemsCount;<br>
然后获取选中矩形区域的列表 listRanges ,获知选中矩形区域的块数 nRangesCount ;<br>
接着根据矩形区域列表,计算每个矩形区域占据的的单元格数目,累加得到选中单元格的总数 nCellsCount 。<br>
最后打印 nItemsCount、nRangesCount 、nCellsCount 。如果选中区域内没有 NULL
单元格,那么选中条目计数会与选中单元格数目一致,如果选中区域内有 NULL 单元格,那么 nCellsCount 会比 nItemsCount
大。等会程序运行时注意比较这些数值。<br>
<br>
接下来是四个按钮对应的槽函数,先看 "查找"
按钮的代码,该槽函数代码较多,使用循环遍历指定列的条目进行查找,这里将代码拆成三块讲解,分为循环前、循环本身、循环后三块的代码,首先是循环前的代码:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonFind_clicked</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;">const</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strDstScore</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;">lineEditFindText</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:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strDstScore</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:#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:#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;">const</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">nDstScore</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strDstScore</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">toInt</span><span
style=" color:#000000;">();</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#008000;">//用于比较的分值</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:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nTheColumn</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;">comboBoxColumns</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">currentIndex</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;">//指定的比较运算符,0是等于,1是小于等于,2是大于等于</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;">nCompare</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;">comboBoxCompares</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">currentIndex</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:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRowCount</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;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">rowCount</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;">nFilteredCount</span><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:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">dblTotal</span><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:#808000;">double</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">dblAverage</span><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:#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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemFilteredFirst</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">NULL</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setCurrentItem</span><span
style=" color:#000000;">(</span><span style=" color:#000080;">NULL</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QItemSelectionModel</span><span
style=" color:#000000;">::</span><span style=" color:#800080;">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>
</div>
"查找" 按钮代码开始获取用于比较的目标字符串 strDstScore ,如果字符串为空就不处理。<br>
对于非空字符串计算其对应的数值 nDstScore 。<br>
然后获取指定的查询列号 nTheColumn ,获取比较运算符的编号 nCompare。<br>
接下来是循环之前的准备,计算总行数 nRowCount 用于遍历指定列单元格;<br>
将统计量过滤后的符合条件数目 nFilteredCount 、总值 dblTotal 、均值 dblAverage 初始化为 0;<br>
准备保存首个符合条件的单元格条目指针到 itemFilteredFirst ,这个指针初始为 NULL;<br>
然后清空表格旧的选中状态,因为查询后会将符合条件的都进行高亮选中。<br>
<br>
接下来看循环本身的代码:<br>
<div class="code"><span style=" color:#c0c0c0;">&nbsp;&nbsp;&nbsp; </span><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:#c0c0c0;"> </span><span style=" color:#808000;">for</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">i</span><span style=" color:#000000;">=</span><span style=" color:#000080;">0</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</span><span
style=" color:#000000;">&lt;</span><span style=" color:#000000;">nRowCount</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</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;">QTableWidgetItem</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">*</span><span style=" color:#000000;">itemCur</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;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">item</span><span style=" color:#000000;">(</span><span style=" color:#000000;">i</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nTheColumn</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:#000080;">NULL</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">==</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">itemCur</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:#808000;">continue</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:#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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strCur</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">itemCur</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:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nCurScore</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">strCur</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">toInt</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:#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:#000080;">0</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">==</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nCompare</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:#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;">nCurScore</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">!=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">nDstScore</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:#808000;">continue</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:#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:#808000;">else</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span style=" color:#000000;">(</span><span
style=" color:#000080;">1</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">==</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nCompare</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:#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:#000000;">!</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">(</span><span style=" color:#000000;">nCurScore</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&lt;=</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nDstScore</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:#808000;">continue</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:#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:#808000;">else</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:#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:#000000;">!</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">(</span><span style=" color:#000000;">nCurScore</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&gt;=</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nDstScore</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:#808000;">continue</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:#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=" color:#008000;">//</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">end</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">if-elseif-else</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;">nFilteredCount</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:#000000;">dblTotal</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">+=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">nCurScore</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:#000000;">itemCur</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setSelected</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">true</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;">if</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">NULL</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">==</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">itemFilteredFirst</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;">itemFilteredFirst</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">itemCur</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setCurrentItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">itemFilteredFirst</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">scrollToItem</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">itemFilteredFirst</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><span style=" color:#008000;">//end</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#008000;">for</span></pre>
</div>
循环内先获取第 i 行第 nTheColumn 列的单元格条目 itemCur ,如果条目非空才继续后续操作。<br>
对非空条目,获取 itemCur 文本 strCur ,并转为数值 nCurScore 。<br>
接下来是根据选定的比较运算符编号 nCompare,分别进行判断,对于不符合比较条件的都进行 continue 操作跳过后续代码,而对于符合条件的条目,
自动执行到循环末尾代码:<br>
&nbsp;&nbsp;&nbsp; 将计数 nFilteredCount 增加;<br>
&nbsp;&nbsp;&nbsp; 总分值累加;<br>
&nbsp;&nbsp;&nbsp; 设置符合条件的条目 itemCur 为高亮选中;<br>
&nbsp;&nbsp;&nbsp; 然后判断是否已经保存了第一个符合条件的条目,如果尚未保存第一个符合条件的条目,那么保存 itemCur
为第一个符合条件的条目,并设置当前条目为该条目,并滚动到该条目。<br>
<br>
循环结束后,最后是循环后的代码:
<div class="code"><span style=" color:#c0c0c0;">&nbsp;&nbsp;&nbsp; </span><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:#c0c0c0;"> </span><span style=" color:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nFilteredCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&gt;</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></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;">dblAverage</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">dblTotal</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">/</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nFilteredCount</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;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strMsg</span><span
style=" color:#c0c0c0;"> </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;">"匹配条目数:%1,匹配条目的总值:%2,均值:%3"</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=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">nFilteredCount</span><span
style=" color:#000000;">).</span><span style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">dblTotal</span><span style=" color:#000000;">).</span><span
style=" color:#000000;">arg</span><span style=" color:#000000;">(</span><span style=" color:#000000;">dblAverage</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;">information</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;">strMsg</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">setFocus</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>
循环结束后,先判断符合条件的条目计数 nFilteredCount ,如果大于 0 就计算均值;<br>
然后构造消息字符串,显示符合条件条目的计数、总分值、均分值。<br>
最后把显示焦点切换到表格控件,方便用户看到高亮选中的条目。<br>
<br>
讲完查找功能,接下来 "增加行" 的按钮槽函数代码如下:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonAddLine_clicked</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:#800080;">QString</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</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;">lineEditName</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:#808000;">if</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">strName</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:#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;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nOldRowCount</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;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">rowCount</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;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">insertRow</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nOldRowCount</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;">SetTableRow</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nOldRowCount</span><span
style=" color:#000000;">,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">strName</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;">"0"</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;">"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;">tr</span><span
style=" color:#000000;">(</span><span style=" color:#008000;">"0"</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;">"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:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">scrollToBottom</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:#000000;">}</span></pre>
</div>
该槽函数获取名字 strName ,判断名字是否非空。<br>
对于非空名字,在表格末尾增加一个新行,然后设置该行的行首和四个单元格内容,行首就是姓名,四个单元格初始化为 "0"
,最后将表格控件滚动到底部,显示出末尾行。QTableWidget 从基类继承了滚动到顶部的函数 scrollToTop() 和滚动到底部的函数
scrollToBottom(),这里调用的是滚动到底部函数。<br>
<br>
接下来是 "删除选中行" 按钮对应的槽函数代码:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonDelLines_clicked</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:#800080;">QList</span><span
style=" color:#000000;">&lt;</span><span style=" color:#808000;">int</span><span
style=" color:#000000;">&gt;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listRowIndex</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:#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;">QList</span><span
style=" color:#000000;">&lt;</span><span style=" color:#800080;">QTableWidgetSelectionRange</span><span
style=" color:#000000;">&gt;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listRanges</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;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">selectedRanges</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;">nRangeCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listRanges</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">count</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;">nRangeCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&lt;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">1</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:#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:#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;">for</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">i</span><span style=" color:#000000;">=</span><span style=" color:#000080;">0</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</span><span
style=" color:#000000;">&lt;</span><span style=" color:#000000;">nRangeCount</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</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;">nCurRangeRowCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listRanges</span><span style=" color:#000000;">[</span><span
style=" color:#000000;">i</span><span style=" color:#000000;">].</span><span style=" color:#000000;">rowCount</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;">for</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">j</span><span style=" color:#000000;">=</span><span style=" color:#000080;">0</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">j</span><span
style=" color:#000000;">&lt;</span><span style=" color:#000000;">nCurRangeRowCount</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">j</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:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nRowIndex</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listRanges</span><span style=" color:#000000;">[</span><span
style=" color:#000000;">i</span><span style=" color:#000000;">].</span><span style=" color:#000000;">topRow</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;">j</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:#000000;">!</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listRowIndex</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">contains</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nRowIndex</span><span
style=" color:#000000;">)</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">)</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;">{</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;">listRowIndex</span><span
style=" color:#000000;">.</span><span style=" color:#000000;">append</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">nRowIndex</span><span
style=" color:#000000;">);</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;">}</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:#000000;">qSort</span><span
style=" color:#000000;">(</span><span style=" color:#000000;">listRowIndex</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;">nRowIndexCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listRowIndex</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">count</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;">for</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">i</span><span style=" color:#000000;">=</span><span style=" color:#000000;">nRowIndexCount</span><span
style=" color:#000000;">-</span><span style=" color:#000080;">1</span><span style=" color:#000000;">;</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</span><span style=" color:#000000;">&gt;=</span><span
style=" color:#000080;">0</span><span style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">i</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:#800000;">ui</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#800000;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">removeRow</span><span
style=" color:#000000;">(</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">listRowIndex</span><span
style=" color:#000000;">[</span><span style=" color:#000000;">i</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:#000000;">listRanges</span><span
style=" color:#000000;">.</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:#000000;">listRowIndex</span><span
style=" color:#000000;">.</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:#000000;">}</span></pre>
</div>
这个函数支持删除多个选中的行。函数开头定义了保存选中行序号的列表 listRowIndex,然后获取选中矩形区域列表 listRanges
,获知选中矩形区域块数 nRangeCount 。<br>
然后循环遍历每个选中矩形区域,将选中区域覆盖的行号 nCurRangeRowCount 提取出来,判断列表 listRowIndex
是否已经包含该行序号,如果不包含就添加到列表里。这样遍历所有选中举行区域后,就统计出了不重复的选中行号列表 listRowIndex 。<br>
因为删除行操作需要从末尾开始删除,所以需要对行序号列表进行排序&nbsp; qSort(listRowIndex);<br>
排序之后,从末尾的选中行号开始删除选中的行,直到删除所有选中的行为止。<br>
函数末尾清空了用到的两个列表 listRanges 和 listRowIndex ,因为这些矩形区域和行都删除干净了。<br>
<br>
例子最后一个函数是 "删除选中条目" 按钮对应的槽函数:<br>
<div class="code"><span style=" color:#808000;">void</span><span style=" color:#c0c0c0;">
</span><span style=" color:#800080;">Widget</span><span style=" color:#000000;">::</span><span
style=" color:#000000;">on_pushButtonDelSelectedItems_clicked</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:#800080;">QList</span><span
style=" color:#000000;">&lt;</span><span style=" color:#800080;">QTableWidgetItem</span><span
style=" color:#000000;">*&gt;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listItems</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;">tableWidget</span><span
style=" color:#000000;">-&gt;</span><span style=" color:#000000;">selectedItems</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;">nCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listItems</span><span style=" color:#000000;">.</span><span
style=" color:#000000;">count</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;">nCount</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">&lt;</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000080;">1</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:#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;">for</span><span
style=" color:#000000;">(</span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">i</span><span style=" color:#000000;">=</span><span style=" color:#000080;">0</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</span><span
style=" color:#000000;">&lt;</span><span style=" color:#000000;">nCount</span><span
style=" color:#000000;">;</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000000;">i</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:#808000;">int</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nItemRow</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listItems</span><span style=" color:#000000;">[</span><span
style=" color:#000000;">i</span><span style=" color:#000000;">]-&gt;</span><span
style=" color:#000000;">row</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;">nItemColumn</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">=</span><span style=" color:#c0c0c0;"> </span><span
style=" color:#000000;">listItems</span><span style=" color:#000000;">[</span><span
style=" color:#000000;">i</span><span style=" color:#000000;">]-&gt;</span><span
style=" color:#000000;">column</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;">delete</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#800000;">ui</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#800000;">tableWidget</span><span style=" color:#000000;">-&gt;</span><span
style=" color:#000000;">takeItem</span><span style=" color:#000000;">(</span><span
style=" color:#000000;">nItemRow</span><span style=" color:#000000;">,</span><span
style=" color:#c0c0c0;"> </span><span style=" color:#000000;">nItemColumn</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;">listItems</span><span
style=" color:#000000;">.</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:#000000;">}</span></pre>
</div>
这个槽函数比较简单,获取选中的条目列表 listItems ,获知选中条目计数 nCount 。<br>
如果计数小于 1 说明没有选中的,就不处理。<br>
对应有选中的条目,获取每个选中条目的行号、列号,将条目从表格控件卸载下来,直接 delete 删除,彻底删除选中的条目。<br>
函数最后清空列表 listItems 。<br>
<br>
示例的代码讲解到这,我们运行例子看看:<br>
<center> <img src="images/ch08/ch08-02-37.png" alt="run1" width="800"></center>
在 QtCreator 下面的输出面板可以看到实时的选中条目、选中区域和选中单元格计数。<br>
选择指定列、比较运算符并设置比较分值后,可以查找符合条件的列统计信息,比如:<br>
<center> <img src="images/ch08/ch08-02-38.png" alt="run2" width="800"></center>
可以对表格增加行,删除一些条目,对于删除过条目的 NULL 单元格,如果包含在选中矩形区域内,再看 QtCreator 的输出面板:<br>
<center> <img src="images/ch08/ch08-02-39.png" alt="run3" width="800"></center>
可以看到选中条目数是 2,而选中单元格数目是 4。在实际编程中需要注意区分基于单元格处理的函数和基于条目处理的函数,对于存在 NULL
单元格的时候尤其要留心。另外说明一下,这个示例没有 "编辑" 或 "修改" 单元格之类的按钮功能,因为可以直接双击编辑单元格内容,为了简化例子,才没有对
单元格文本内容进行筛选,如果输入汉字就会有不正常的统计数值出现,以后的示例再考虑在单元格 内容输入时做合法性判断。<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="ch08-01.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="ch08-03.htm"><img class="pic"
style="width: 32px; height: 32px;" alt="next" src="images/pics/next.png"></a></div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
HTML
1
https://gitee.com/qtguide/qtguide.git
git@gitee.com:qtguide/qtguide.git
qtguide
qtguide
qtguide
master

搜索帮助