锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 鍩烘湰鐭ヨ瘑錛?
娓告垙瑙勫垯寰堢畝鍗曪紝灝辨槸鍒╃敤鍔犲噺涔橀櫎灝嗘墤鍏嬬墝鐨勪換涓4寮犵墝鐨勭粍鍚堬紝榪愮畻鍚庡緱鍒扮粨鏋?4.
鐣岄潰鐢≦t瀹屾垚鍚?
鐣岄潰娌℃湁澶氫笅鍔熷か錛屽彲浠ユ坊鍔犳椂闂存彁閱掞紝淇敼鐩爣鏁板鹼紙24錛夛紝澧炲姞鐗屾暟搴旇涔熸槸鍙互鐨勶紝涓嶈繃鎴戞病鏈夊幓璇曚竴涓嬨傛劅瑙夎姳鍝ㄤ簡娌℃湁綆鍗曟潵鐨勫ソ涓浜涖?
next浼氱敓鎴愭柊鐨勭墝銆俛nswer鏌ョ湅絳旀銆?
鐗岀殑鏁板間嬌鐢≦lineEdit鍋氱殑錛屽彲浠ヨ嚜宸辮緗箋?
CardCalculation綾諱細鏍規嵁杈撳叆鐨勭墝鏁?vector<double>)璁$畻鍑哄搷搴旂殑琛ㄨ揪寮忋?
璁$畻嫻佺▼錛?
鏁版嵁緇撴瀯鐨勪功榪樻病鐪嬪畬錛岀畻娉曞畬鍏ㄦ槸鑷繁鍑┖鍘昏瘯銆傛渶鍚庡啓鍑烘潵鍚庡叾瀹炰篃涓嶇煡閬撹嚜宸卞啀鑰冭檻榪欐柟渚塊棶棰樻椂鎬濊礬鎬庝箞灞曞紑銆傚敖蹇娊鏃墮棿鐪嬩笅綆楁硶鐨勪功鎵嶈銆?
鎴戜滑鎷?#8251;浣滀負浠繪剰鐨勬搷浣滅錛?-*/錛夋潵璇存槑銆?
鏍規嵁vec鍏冪礌涓暟:
1. a※b錛岀洿鎺ヨ綆楀嵆鍙?
2. a※b※c,鍙兘鎿嶄綔涓?
(a※b)※c
a※(b※c)
3. a※b※c ※d,鍙兘鎿嶄綔涓猴細
(a※b)※c ※d
a※(b※c) ※d
a※b※(c ※d)
鍥犳閲囩敤閫掑綊姹傝В錛屽ソ澶勬槸濡傛灉澧炲姞鐗岀殑寮犳暟鏃跺簲璇ュ鏄撲慨鏀逛竴浜涳紝鍏蜂綋瀹炵幇鍑芥暟涓篶alCards銆?
鍒氫紶榪泇ec鏃訛紝澶у皬涓?錛屽睘浜庣涓夌鎯呭喌銆傞亶鍘唙ec錛屽皢鍙兘鎿嶄綔鍚庡艦鎴愮殑鏂扮殑vector鍐嶆浣滀負鍑芥暟鍙傛暟浼犲叆錛岀洿鍒伴亣鍒?鎯呭喌榪斿洖錛屽鏋滄弧瓚寵繑鍥瀟rue錛屽惁鍒欒繑鍥瀎alse銆傝繑鍥瀟rue鏃訛紝淇敼絎簩涓弬鏁頒繚瀛樿〃杈懼紡銆?
鍏充簬濡備綍榪斿洖琛ㄨ揪寮忚繖閲屾垜寮勪簡寰堥暱鏃墮棿銆傝綆楀嚭姝g‘鐨勮〃杈懼紡鍚庯紝姣忔閫掑綊閮戒細鍔犱竴瀵規嫭鍙鳳紝鍏跺疄鍚庨潰搴旇鍙互瀵硅〃杈懼紡浼樺寲涓嬪彲鑳藉噺灝戞嫭鍙風殑鏁伴噺錛屼笉榪囨垜娌℃湁鍐欍?
紼嬪簭閲岄兘鍐欎簡娉ㄩ噴~濡傛灉榪樻槸鏈夐棶棰樿緇欐垜鐣欒█J /Files/izualzhy/TwentyFourGame.rar
]]>
鍘熸枃閾炬帴:Qt緇忓吀鍑洪敊淇℃伅涔媢ndefined reference to `vtable for classname
聽
榪欎釜鍑洪敊淇℃伅澶父瑙佷簡錛岀敤榪嘠t涓や釜鏈堜互涓婄殑鏈嬪弸鍩烘湰涓婇兘鑳借嚜宸辮В鍐充簡錛屽洜涓哄お緇忓吀浜嗭紝鍙互緇欐柊鎵嬪弬鑰冦?
鍑洪敊淇℃伅涓鑸被浼?undefined reference to `vtable for classname MyWidget`
鍦ㄦ墽琛宮ake鍛戒護鍚庡嚭鐜般?
鍑洪敊鍘熷洜鏄湪瀹氫箟綾葷殑鏃跺欎負浜嗚兘浣跨敤signals鍜宻lot,鍦ㄧ被瀹氫箟鐨勫悗闈㈠姞浜哘_OBJECT寮曡搗銆?鍥犱負Q_OBJECT鏄竴涓畯錛屽湪涓嶅悓鐨勭被涓睍寮鏄笉鍚岀殑浠g爜錛屼緥濡傚湪mywidgeth.h涓敓鎴愮殑
public:
template inline void qt_check_for_QOBJECT_macro(const T &_q_argument) const {
int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument); i = i;
}
聽
static const QMetaObject staticMetaObject;
virtual const QMetaObject *metaObject() const;
virtual void *qt_metacast(const char *);
static inline QString tr(const char *s, const char *c = 0) {
return staticMetaObject.tr(s, c);
}
static inline QString trUtf8(const char *s, const char *c = 0)
{
return staticMetaObject.trUtf8(s, c);
}
static inline QString tr(const char *s, const char *c, int n) {
return staticMetaObject.tr(s, c, n);
}
static inline QString trUtf8(const char *s, const char *c, int n) {
return staticMetaObject.trUtf8(s, c, n);
}
virtual int qt_metacall(QMetaObject::Call, int, void **); private:
鍙互鐪嬪埌浠ヤ笂鐨勪唬鐮佸0鏄庝簡3涓粠鐖剁被緇ф壙鐨?涓櫄鍑芥暟銆傛湰鏉ヨ繖涓変釜鍑芥暟鐨勫疄鐜板簲璇ユ槸鐢眒oc_mywidget.cpp鏉ュ畬鎴愩?br/>鑰屽綋鍓嶇殑閿欒姝f槸鍥犱負Makefile閲屾病鏈夊皢moc_mywidget.cpp鍔犲叆緙栬瘧寮曡搗銆?
Makefile鑳屽悗鐨勫師鍥犳槸qmake錛屽洜涓篞t鐨勭紪璇戠郴緇熸槸閫氳繃qmake灝?pro鏂囦歡杞崲鎴怣akefile鏂囦歡銆傚綋qmake鎵弿.h浠g爜鏃跺彂鐜版湁Q_OBJECT榪欐牱瀛楃溂鐨勪唬鐮佹椂錛屼細灝嗕竴涓敤moc鐢熸垚moc_xxx.cpp浠g爜鐨勪緷璧栧叧緋誨啓鍒癕akefile閲屻傚鏋滄壂鎻忔椂娌℃湁鎵懼埌Q_OBJECT灝變笉浼氱敓鎴愰澶栫殑moc_xxx.cpp榪欐牱鐨勬枃浠躲?
鍑虹幇鏈鍒濅竴騫曠殑鍘熷洜鏄湪鎵цqmake鐨勬椂鍊?h浠g爜閲屽茍娌℃湁O_OBJECT榪欐牱鐨勪唬鐮併傝屾墽琛宮ake鐨勬椂鍊?h閲屽凡緇忔湁Q_OBJECT浜嗭紝瑙e喅鐨勬柟娉曞氨鏄噸鏂版墽琛宷make錛岀劧鍚庢墽琛宮ake.
鈥斺?
shiroki錛氬彟澶栨垜鍐嶅姞涓鍙ワ紝 榪樻湁涓縐嶅彲鑳芥ф槸鍐欎唬鐮佺殑浜烘妸鎵鏈夌殑浠g爜閮藉啓鍦ㄤ簡.cpp鏂囦歡涓傝鐭ラ亾moc宸ュ叿鍙.h鏂囦歡錛屽鏋滄妸Q_OBJECT瀹忔斁鍦╟pp閲宮oc鏄湅涓嶅埌鐨勨︹︽墍浠ュぇ瀹跺啓浠g爜榪樻槸瑕侀伒瀹堝0鏄庢斁.h瀹炵幇鏀綾pp鐨勮鐭╂瘮杈冨ソ銆?
紼嬪簭榪愯鏁堟灉鍥?br />
璐熻矗澶勭悊xml鏂囦歡鐨勬槸XbelWriter錛孹belReader錛岀晫闈㈢殑瀹炵幇鏄敱MainWindow瀹屾垚鐨?/p>
淇濆瓨鍚庣殑xbel鏂囦歡鍓嶅嚑琛屾槸榪欐牱鐨?br /><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xbel>
<xbel version="1.0">
<folder folded="yes">
<title>Literate Programming</title>
<bookmark href=" <title>Synopsis of Literate Programming</title>
</bookmark>
濡傛灉涓嶆柇鐢╮eadNext錛堬級錛屽嵆浠ヤ笅鍑犺浠g爜璇誨彇鏂囦歡鐨勮瘽
while (!xml.atEnd()) {
qDebug() << "name " << xml.name() << "tokentype " << xml.tokenType();
if (xml.isCharacters()) {
qDebug() << xml.text();
}
xml.readNext();
}
鍓嶅嚑琛岃緭鍑猴細
name "" tokentype 0
name "" tokentype 2
name "" tokentype 8
name "xbel" tokentype 4
name "" tokentype 6
"
"
name "folder" tokentype 4
name "" tokentype 6
"
"
name "title" tokentype 4
name "" tokentype 6
"Literate Programming"
name "title" tokentype 5
name "" tokentype 6
"
"
name "bookmark" tokentype 4
name "" tokentype 6
"
"
name "title" tokentype 4
name "" tokentype 6
"Synopsis of Literate Programming"
name "title" tokentype 5
緇撳悎婧愪唬鐮侀噷鍏充簬Tokentype鐨別num瀹氫箟錛?br />enum TokenType {
NoToken = 0,
Invalid,
StartDocument,
EndDocument,
StartElement,
EndElement,
Characters,
Comment,
DTD,
EntityReference,
ProcessingInstruction
};
鍙互鐪嬪嚭錛?br />1.readNext灝辨槸涓涓竴涓殑鏉ヨ錛岀鍒?lt;xxx>,</xxx>,浠ュ強...>xxx<...鐨勬枃瀛?br /> 鐞嗕竴涓嬭鍙栫殑嫻佺▼錛?br /> 1.娌℃湁璇誨彇浠諱綍涓滆タ錛孨oToken
2.璇誨彇<?xml version="1.0" encoding="UTF-8"?>錛孲tartDocument
3.璇誨彇<!DOCTYPE xbel>錛孌TD
4.璇誨彇<xbel version="1.0">錛孲tartElement
5.璇誨彇<xbel version="1.0">涓?lt;folder folded="yes">涔嬮棿鐨刢haracters錛屽鏋滄墦鍗扮浉搴旂殑unicode鍑烘潵鐨勮瘽錛屾槸10 32 32 32 32
鍗?涓崲琛?4涓┖鏍鹼紝鐩存帴鎵撳嵃灝辨槸
"
"
榪欎釜浜嗭紝鍙...>xxx<...鐨勬枃瀛楅兘浼氳鍙栵紝鍗充嬌鏄崲琛?絀烘牸涔嬬被鐨?br /> 6.璇誨彇<folder folded="yes">錛孲tartElement
銆傘傘傘傘傘?br />2.
閬囧埌榪欑<separator/>鍒欑被浼間簬<separator></separator>
name "separator" type 4
name "separator" type 5
鍒╃敤void QXmlStreamWriter::writeEmptyElement("separator")鍙互鍐欏叆鎴愯繖鏍?/p>
1.XbelWriter Class
鍐欏叆鐨勮繃紼嬪叾瀹炴槸寰堢畝鍗曠殑錛屽叿浣撶湅浠g爜錛?br /> bool XbelWriter::writeFile(QIODevice *device)
{
xml.setDevice(device);
xml.writeStartDocument();//鐢熸垚L1
xml.writeDTD("<!DOCTYPE xbel>");//鐢熸垚L2
xml.writeStartElement("xbel");//涓庝笅涓琛屽叡鍚岀敓鎴怢3
xml.writeAttribute("version", "1.0");
for (int i = 0; i < treeWidget->topLevelItemCount(); ++i)
writeItem(treeWidget->topLevelItem(i));
xml.writeEndDocument();
return true;
}
2.XbelReader Class
棣栧厛璇誨彇鍒板悎閫傜殑Element
bool XbelReader::read(QIODevice *device)
{
xml.setDevice(device);
if (xml.readNextStartElement()) {
if (xml.name() == "xbel" && xml.attributes().value("version") == "1.0")//鍗寵鍒皀ame涓簒bel錛屽睘鎬ersion涓?.0鐨凟lement鏃?br />//The actual process of reading only takes place if the file is a valid XBEL 1.0 file.
readXBEL();
else
xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file."));
}
return !xml.error();
}
鐢辨敞閲婂彲浠ョ湅鍒板伐浣滃嚱鏁版槸readXBEL(),鏁翠釜嫻佺▼鐢辮繖鍑犺浠g爜瀹屾垚錛堥噷闈㈠嚱鏁扮敤鍒伴掑綊錛?br />void XbelReader::readXBEL()
{
Q_ASSERT(xml.isStartElement() && xml.name() == "xbel");
while (xml.readNextStartElement()) {
if (xml.name() == "folder")
readFolder(0);
else if (xml.name() == "bookmark")
readBookmark(0);
else if (xml.name() == "separator")
readSeparator(0);
else
xml.skipCurrentElement();
}
}
鐪嬩笅skipCurrentElement()鐨勬簮浠g爜錛?br />void QXmlStreamReader::skipCurrentElement()
{
int depth = 1;
while (depth && readNext() != Invalid) {
if (isEndElement())
--depth;
else if (isStartElement())
++depth;
}
}錛屽彲浠ョ湅鍒板彉閲廳epth鍦ㄨ繘鍏ヤ竴涓柊鐨別lement鍒?1錛屽嚭鏉ュ垯-1
鍥犳鐩村埌鐜板湪鐨勮繖涓猠lement緇撴潫錛堥亣鍒扮浉搴旂殑</xxx>錛夛紝鍑芥暟鎵嶇粨鏉熼鍑?br />Qt Assistant閲岀殑娉ㄩ噴錛?br />The readXBEL() function reads the name of a startElement and calls the appropriate function to read it, depending on whether if its a "folder", "bookmark" or "separator". Otherwise, it calls skipCurrentElement().
鍗沖鏋淓lement鐨刵ame涓篺older,bookmark,separator鏃訛紝璇誨叆鍐呭錛屽惁鍒檚kip
鍏蜂綋浼氶亣鍒扮殑鏈夎繖涔堝嚑縐嶏細
<folder folded="yes">
<title>Literate Programming</title>
<bookmark href="<separator/>
闇瑕佷笉鍚岀殑鍑芥暟鏉ュ鐞?br />渚嬪瓙閲屽垎鍒敤浜?br />readTitle(),readSeparator(),readFolder(),readBookmark()
鎴戜富瑕佸涔犱簡涓媟eadFolder()
fold鏈変袱縐嶆儏鍐?br /><folder folded="yes">
<folder folded="no">
璇誨彇鏃跺樊鍒殑澶勭悊浠g爜
QTreeWidgetItem *folder = createChildItem(item);
bool folded = (xml.attributes().value("folded") != "no");
treeWidget->setItemExpanded(folder, !folded);
絎笁琛屾敞閲婃帀鍚庡姣斾笅鍥劇墖
鍙榛樿鏄笉expand鐨?br />Sets the item referred to by item to either closed or opened, depending on the value of expand.
This function is deprecated. Use QTreeWidgetItem::setExpanded() instead.
void QTreeWidgetItem::setExpanded ( bool expand )
Expands the item if expand is true, otherwise collapses the item
涓昏鏄睍寮榪樻槸鎽哄彔欏廣?br />folder涓嬮潰鐨別lement鍙兘name涓簍itle錛宐ookmark錛宻eparator錛岃繕鍙兘鍐嶇敱folder錛屽儚Useful C++ Links涓嬮潰榪樻湁STL Qt絳夛紝闇瑕侀掑綊澶勭悊錛屽洜姝よ繕闇瑕佷互涓嬩唬鐮?br /> while (xml.readNextStartElement()) {
if (xml.name() == "title")
readTitle(folder);
else if (xml.name() == "folder")
readFolder(folder);
else if (xml.name() == "bookmark")
readBookmark(folder);
else if (xml.name() == "separator")
readSeparator(folder);
else
xml.skipCurrentElement();
}
鍙笉榪囪窡readXBEL閲屽弬鏁頒笉鍚?/p>
鍐嶇湅涓媤hile-loop condition
bool QXmlStreamReader::readNextStartElement ()
Reads until the next start element within the current element. Returns true when a start element was reached. When the end element was reached, or when an error occurred, false is returned.
The current element is the element matching the most recently parsed start element of which a matching end element has not yet been reached. When the parser has reached the end element, the current element becomes the parent element.
This is a convenience function for when you're only concerned with parsing XML elements.
鐪嬩笅璇ュ嚱鏁版簮浠g爜錛?br />bool QXmlStreamReader::readNextStartElement()
{
while (readNext() != Invalid) {
if (isEndElement())
return false;
else if (isStartElement())
return true;
}
return false;
}
鍙互鐪嬪埌鏄鍙栦笅涓涓鍙鳳紝濡傛灉鏄痚ndElement錛屽垯榪斿洖false錛屽惁鍒檛rue
鍐嶅姣斾笅xml鏂囦歡鐪嬩笅璇ュ嚱鏁版槸濡備綍鐢ㄦ潵鍦ㄨ繖涓ぇ鐨別lement鍐呴儴閬嶅巻鐨?br /><folder folded="yes">
<title>Literate Programming</title>
<bookmark href=" <title>Synopsis of Literate Programming</title>
</bookmark>
<bookmark href=" <title>Literate Programming: Propaganda and Tools</title>
</bookmark>
1.璇誨埌璁板彿涓?lt;title>錛岃繘鍏eadTitle()
2.璇誨彇鍐呭錛岃鍙風Щ鍔ㄥ埌</title>
3.榪涘叆while鏉′歡錛屽嵆readNextStartElement()錛屾鏃惰櫧鐒禩okenType鏄疎ndElement錛屼絾鏄痳eadNext鍚庯紝鍙樹負startElement錛屽嵆bookmark錛岃繘鍏eadBookmark()
4.鍚宖older錛宐ookmark涔熶竴鐩磋璇誨埌</bookmark>
鍐嶇湅涓?br />QTreeWidgetItem *XbelReader::createChildItem(QTreeWidgetItem *item)
{
QTreeWidgetItem *childItem;
if (item) {
childItem = new QTreeWidgetItem(item);
} else {
childItem = new QTreeWidgetItem(treeWidget);
}
childItem->setData(0, Qt::UserRole, xml.name().toString());
return childItem;
}
setData鍙傛暟鍙互鎸囧畾涓嶅悓鐨剅ole
Qt::UserRole 32 The first role that can be used for application-specific purposes.
閫氳繃鐩稿簲鐨刣ata鍑芥暟鍙互鍙栧嚭璇String
閫氳繃緇?br />QTreeWidgetItem鐨勬瀯閫犲嚱鏁頒紶鍏ヤ笉鍚岀殑鍙傛暟錛屽彲浠ヤ紶鍏TreeWidget錛屼篃鍙互鏄疩TreeWidgetItem
QTreeWidget浼氭牴鎹悇涓猧tem鐨勫叧緋昏屾帓鍒楀ソ緙╄繘錛熷叧浜嶲TreeWidget榪樻病鐢ㄨ繃錛屾湁絀鴻瀛︿範涓涓嬨?br />
璺熶嬌鐢≦XmlStreamReader QXmlStreamWriter鏄竴鏍風殑錛屽尯鍒湪浜庡鐞嗙殑榪囩▼銆?br />鍓嶄竴涓緥瀛愪富瑕佹槸浼犲弬鐨勬柟寮忥紝灝哘TreeWidget浼犵粰xmlReader錛寈mlWriter
璇誨彇xml鍒癚TreeWidget閲岋紝闅忕潃璇誨叆娣誨姞item鍒伴噷闈紝鍐欑殑鏃跺欏垯閬嶅巻榪欎釜QTreeWidget錛屾妸item鍐欏叆鍒皒ml鏂囦歡
榪欎釜渚嬪瓙鍒欐槸鐢盦TreeWidget媧劇敓涓涓猉belTree鐨勭被鍑烘潵錛岀鏈夋垚鍛橀噷鏈変竴涓猀DomDocument鐨勫璞°?br />綆楁槸涓ょ澶勭悊鏂瑰紡鍚э紝浣嗕紶鍙傚拰緇勫悎鐨勬柟寮忔瘡涓緥瀛愰兘鏄彲浠ョ敤鐨勶紝鍙兘鍙槸QXmlStream鍜孮Dom鏇撮傚悎鍝鏂瑰紡閲屼簡銆?br />榪欎釜渚嬪瓙涓昏鏄涔燪Dom Classes
鍏堣創涓涓猀t Assistant閲岀殑涓孌典唬鐮?br />The QDom classes are typically used as follows:
QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();
// print out the element names of all elements that are direct children
// of the outermost element.
QDomElement docElem = doc.documentElement();
QDomNode n = docElem.firstChild();
while(!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
cout << qPrintable(e.tagName()) << endl; // the node really is an element.
}
n = n.nextSibling();
}
// Here we append a new element to the end of the document
QDomElement elem = doc.createElement("img");
elem.setAttribute("src", "myimage.png");
docElem.appendChild(elem);
Once doc and elem go out of scope, the whole internal tree representing the XML document is deleted.
To create a document using DOM use code like this:
QDomDocument doc("MyML");
QDomElement root = doc.createElement("MyML");
doc.appendChild(root);
QDomElement tag = doc.createElement("Greeting");
root.appendChild(tag);
QDomText t = doc.createTextNode("Hello World");
tag.appendChild(t);
QString xml = doc.toString();
鍔犱笂榪欏嚑鍙ヨ瘽浜х敓xml鏂囦歡鏌ョ湅鍐呭
QFile file("mydocument.xml");
file.open(QIODevice::ReadWrite);
QTextStream m_out(&file);
doc.save(m_out,4);
file.close();
浜х敓鐨刣ocument鍐呭錛?br /><!DOCTYPE MyML>
<MyML>
<Greeting>Hello Wolrd!</Greeting>
</MyML>
1.
XbelTree緇ф壙鑷猀TreeWidget
姣忎竴鍒楃殑澶у皬鏄惁鍙互鑷敱鏀瑰彉錛岀敱璇ュ嚱鏁板喅瀹?br />void QHeaderView::setResizeMode ( ResizeMode mode )
Sets the constraints on how the header can be resized to those described by the given mode.
The resize mode specifies the behavior of the header sections. It can be set on the entire header view or on individual sections using setResizeMode().
Constant Value Description
QHeaderView::Interactive 0 The user can resize the section. The section can also be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize. (See also cascadingSectionResizes.)
QHeaderView::Fixed 2 The user cannot resize the section. The section can only be resized programmatically using resizeSection(). The section size defaults to defaultSectionSize.
QHeaderView::Stretch 1 QHeaderView will automatically resize the section to fill the available space. The size cannot be changed by the user or programmatically.
QHeaderView::ResizeToContents 3 QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically. (This value was introduced in 4.2)
鏈涓婁竴鎺掔殑headview鐨勬寚閽堥氳繃璇ュ嚱鏁拌幏寰?br />QHeaderView * QTreeView::header () const
Returns the header for the tree view.
2.
<img src="myimg.png">
tagName:img
name:src
value:"myimg.png"
QDom
A
QDomElement QDomDocument::documentElement () const
Returns the root element of the document.
璇ュ嚱鏁拌繑鍥炵殑鍗沖湪DOCTYPE鍚庢渶澶х殑涓涓狤lement錛屽嵆root
B
QDomElement QDomNode::firstChildElement ( const QString & tagName = QString() ) const
Returns the first child element with tag name tagName if tagName is non-empty; otherwise returns the first child element. Returns a null element if no such child exists.
璇ュ嚱鏁拌繑鍥炴寚瀹歵agName鐨凲DomElement
鐩稿叧鐨勬湁
firstChildElement(),lastChildElement(), previousSiblingElement(), and nextSiblingElement().
閮介渶瑕佹寚瀹歵agName
褰撳瓙緇撶偣鐨勪笉鏄竴涓狤lement鏃訛紝浣跨敤鍑芥暟鑾峰緱絎竴涓瓙鑺傜偣
QDomNode QDomNode::firstChild () const
Returns the first child of the node. If there is no child node, a null node is returned. Changing the returned node will also change the node in the document tree.
鑾峰緱鏈鍚庝竴涓瓙緇撶偣
QDomNode QDomNode::lastChild () const
Returns the last child of the node. If there is no child node, a null node is returned. Changing the returned node will also change the node in the document tree.
See also firstChild() and childNodes().
鑾峰緱涓嬩竴涓瓙鑺傜偣
QDomNode QDomNode::nextSibling () const
Returns the next sibling in the document tree. Changing the returned node will also change the node in the document tree.
If you have XML like this:
<h1>Heading</h1>
<p>The text...</p>
<h2>Next heading</h2>
and this QDomNode represents the <p> tag, nextSibling() will return the node representing the <h2> tag.
鑾峰緱涓婁竴涓瓙鑺傜偣
QDomNode QDomNode::previousSibling () const
Returns the previous sibling in the document tree. Changing the returned node will also change the node in the document tree.
For example, if you have XML like this:
<h1>Heading</h1>
<p>The text...</p>
<h2>Next heading</h2>
and this QDomNode represents the <p> tag, previousSibling() will return the node representing the <h1> tag.
浣犱篃鍙互鐩存帴鑾峰緱緇撶偣鐨勫垪琛?br />QDomNodeList QDomNode::childNodes () const
Returns a list of all direct child nodes.
Most often you will call this function on a QDomElement object.
For example, if the XML document looks like this:
<body>
<h1>Heading</h1>
<p>Hello <b>you</b></p>
</body>
Then the list of child nodes for the "body"-element will contain the node created by the <h1> tag and the node created by the <p> tag.
The nodes in the list are not copied; so changing the nodes in the list will also change the children of this node.
DOM鏄竴縐嶈В鏋愮敱涓囩淮緗戝崗浼?W3C)鎵寮鍙戠殑XML鏂囨。鐨勬爣鍑嗗簲鐢ㄧ▼搴忕紪紼嬫帴鍙c俀t鎻愪緵涓濂楃敤浜庤鍙栥佹搷浣滃拰緙栧啓XML鏂囨。鐨勯潪楠岃瘉鍨嬩簩綰OM瀹炵幇銆?/p>
DOM鎶奨ML鏂囦歡琛ㄧず鎴愬唴瀛樹腑鐨勪竴媯墊爲銆傛垜浠彲浠ユ寜闇瑕侀亶鍘嗚繖涓狣OM鏍戯紝涔熷彲浠ヤ慨鏀硅繖涓爲騫舵妸瀹冧綔涓篨ML鏂囦歡淇濆瓨鍒扮鐩樹腑銆?/p>
璁╂垜浠冭檻濡備笅榪欎釜XML鏂囨。錛?/p>
<doc>
<quote>Scio me nihil scire</quote>
<translation>I know that I know nothing></translation>
</doc>
瀹冨浜庡涓嬫墍紺虹殑DOM鏍戯細
Document
Element(doc)
Element(quote)
Text(“Scio me nihil scire”)
Element(translation)
Text(“I know that I know nothing”)
榪欎釜DOM鏍戝寘鍚笉鍚岀被鍨嬬殑鑺傜偣銆備緥濡傦紝Element鑺傜偣瀵瑰簲鎵撳紑鏍囩浠ュ強涓庡畠鍖歸厤鐨勫叧闂爣絳俱?font color="#0000ff">鍦ㄨ繖涓や釜鏍囩涔嬮棿鐨勫唴瀹瑰垯浣滀負榪欎釜Element鑺傜偣鐨勫瓙鑺傜偣鍑虹幇銆?/font>錛堟敞鎰忚摑鑹插瓧浣擄級
涓婇潰鐨勪粙緇嶄富瑕佹槸鐢ㄦ潵璇存槑dom鏍戠殑緇撴瀯鎯呭喌錛屼笅闈㈠垯榪涘叆姝i鐪嬪浣曢氳繃qt鏉ヨ揪鍒頒慨鏀箈ml鏂囦歡鐨勭洰鐨勩傛湰鏂囦腑涓昏璇存槑濡備綍淇敼xml灞炴у間互鍙婅妭鐐瑰鹼紝
淇敼灞炴у肩殑浠g爜濡備笅錛?/font>
bool QXmlEdit::SetAValue(const QString &key, const QVariant &value) { try { QString tag; QString attName; QStringList sl=key.split("/"); tag = sl.at(0); attName = sl.at(1); QDomElement docElem = doc.documentElement(); QDomNodeList nodeList = docElem.elementsByTagName(tag); if (nodeList.count() >0 ) { QDomElement el = nodeList.at(0).toElement(); el.setAttribute(attName,value.toString()); //璁劇疆灞炴у?/span>QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } QTextStream out(&file); doc.save(out,4); } return true; } catch (...) { return false; } }淇敼鑺傜偣鍊肩殑浠g爜濡備笅錛?/font>bool QXmlEdit::SetTValue(const QString &key, const QVariant &value) { try { QDomElement docElem = doc.documentElement(); QDomNodeList nodeList = docElem.elementsByTagName(key); if (nodeList.count() >0 ) { QDomElement el = nodeList.at(0).toElement(); //tag涓簁ey鐨勮妭鐐? QDomNode oldnode = el.firstChild(); //鏍囩涔嬮棿鐨勫唴瀹逛綔涓鴻妭鐐圭殑瀛愯妭鐐瑰嚭鐜幫紝寰楀埌鍘熸潵鐨勫瓙鑺傜偣 el.firstChild().setNodeValue(value.toString()); //鐢ㄦ彁渚涚殑value鍊兼潵璁劇疆瀛愯妭鐐圭殑鍐呭QDomNode newnode = el.firstChild(); //鍊間慨鏀硅繃鍚庣殑瀛愯妭鐐?/span>el.replaceChild(newnode,oldnode); //璋冪敤鑺傜偣鐨剅eplaceChild鏂規硶瀹炵幇淇敼鍔熻兘 QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } QTextStream out(&file); doc.save(out,4); } return true; } catch (...) { return false; } }
鑷蟲鎴戜滑灝卞疄鐜頒簡淇敼xml灞炴у強text鐨勫姛鑳姐?/p>

Ok Cancel Button鍙互鐩存帴浣跨敤錛屽啀鐪嬩笅accepted錛宺ejected淇″彿鍦ㄤ粈涔堟儏鍐典笅鍙戦?br />void QDialogButtonBox::accepted () [signal]
This signal is emitted when a button inside the button box is clicked, as long as it was defined with the AcceptRole or YesRole.
void QDialogButtonBox::rejected () [signal]
This signal is emitted when a button inside the button box is clicked, as long as it was defined with the RejectRole or NoRole.
錛岃孫k cancel Button鐨勫畾涔夛細
QDialogButtonBox::Ok 0x00000400 An "OK" button defined with the AcceptRole.
銆傘傘?
QDialogButtonBox::Cancel 0x00400000 A "Cancel" button defined with the RejectRole.
姝ゅ榪樻湁寰堝Button錛屽Save Apply絳夛紝閮藉畾涔夊湪
enum QDialogButtonBox::StandardButton
flags QDialogButtonBox::StandardButtons閲屼簡銆?br />鐢ㄨ搗鏉ユ槸涓嶆槸寰堟柟渚垮憿錛?br />涓嶈繃璇ョ被榪樻湁寰堝涓滆タ娌$湅鎳傦紝濡侫ctionRole絳夛紝鎰熻鏈夌┖榪欎釜綾誨彲浠ュ崟鐙涔犱竴涓嬨?br />B
Incomplete Form Widget鏄竴涓猀MEssgeBOx錛氾細warning
涔嬫墍浠ヤ袱涓被褰掔粨涓轟竴涓煡璇嗙偣錛屾槸鍥犱負QMessageBox涓庝笂闈㈢殑綾繪湁涓鐐硅繕鏄緢鍍忕殑錛岄偅灝辨槸Button涓嶇敤瀹氫箟銆傘傘?br />QMessageBox鐨凚utton閮藉畾涔夊湪榪欓噷浜?br />enum QMessageBox::StandardButton
flags QMessageBox::StandardButtons
璺熶笂闈㈢殑寰堝儚
鐢ㄨ搗鏉ヤ篃闈炲父鏂逛究錛岀湅涓婭ncomplete Form Widget鐨勭浉鍏充唬鐮?br />QMessageBox::StandardButton answer;
answer = QMessageBox::warning(this, tr("Incomplete Form"),
tr("The form does not contain all the necessary information.\n"
"Do you want to discard it?"),
QMessageBox::Yes | QMessageBox::No);
if (answer == QMessageBox::Yes)
reject();
鍏充簬warning鐨勫畾涔?br />Static Public Members
銆傘傘?br />StandardButton warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )
娉ㄦ剰鏄疭tatic鐨?br />榪斿洖綾誨瀷鍗充負涓婇潰璇寸殑鏋氫婦綾誨瀷
2.
QTableView鐨勪笂杈瑰拰宸﹁竟1 2/1 2 3 4鐨勭獥鍙d篃鍙互榪涗竴姝ユ搷浣?br />閫氳繃
QHeaderView * horizontalHeader () const
鎷垮埌瀵瑰簲鎸囬拡錛屼緥濡?br />itemsTable->verticalHeader()->hide();灝卞彲浠ュ皢宸﹁竟鐨? 2 3 4闅愯棌浜?br />3.
QAction * QMenu::addAction ( const QString & text )
This convenience function creates a new action with text. The function adds the newly created action to the menu's list of actions, and returns it.
榪欎釜鍑芥暟闈炲父濂界敤錛屽洜涓哄彲浠ヨ嚜鍔ㄥ垱寤轟竴涓猘ction錛岃繑鍥炲煎氨鏄繖涓猘ction鐨勬寚閽堬紝鍚屾椂鍙﹀鍑犱釜閲嶈澆鍑芥暟鏈夌殑鏇村姞鏂逛究
QAction * QMenu::addAction ( const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0 )
This is an overloaded function.
This convenience function creates a new action with the text text and an optional shortcut shortcut. The action's triggered() signal is connected to the receiver's member slot. The function adds the newly created action to the menu's list of actions and returns it.
鍦ㄥ垱寤虹殑鏃跺欙紝鍙互鐩存帴鎸囧畾榪炴帴鍒扮殑妲姐?br />渚嬪瓙閲岃繖閲屽垎鍒敤鍒頒簡錛?br />QAction *newAction = fileMenu->addAction(tr("&New..."));
newAction->setShortcuts(QKeySequence::New);
printAction = fileMenu->addAction(tr("&Print..."), this, SLOT(printFile()));
printAction->setShortcuts(QKeySequence::Print);
printAction->setEnabled(false);
4.
Order Form Widget閲屼富紿楀彛灝辨槸QTabWidget
鍏朵笅鏄劇ず紿楀彛鏄竴涓猀TextEdit錛岄氳繃鍒╃敤QTextCursor QTextFrame絳夊仛鐨勯潪甯稿ソ鐪?br />鍥犱負榪欎釜鎺掔増涔嬪墠瀹屽叏涓嶇煡閬撴庝箞鍘諱綔錛屼唬鐮佹秹鍙婁簡寰堝鐨勭被錛堥兘寰堢畝鍗曠殑鎺ュ彛錛夛紝鎵浠ヨ窡鐫浠g爜鍋氫簡涓閬嶆敞閲婂涔犱簡涓?br /> QTextCursor cursor(editor->textCursor());
cursor.movePosition(QTextCursor::Start);//鍙栧緱鎸囬拡騫舵斁鍦ㄦ渶寮濮嬬殑浣嶇疆
QTextFrame *topFrame = cursor.currentFrame();//Note that topFrame is the editor's top-level frame and is not shown in the document structure.
QTextFrameFormat topFrameFormat = topFrame->frameFormat();
topFrameFormat.setPadding(16);//璁劇疆鍐呭鐨勮竟璺濓紝鍘繪帀鍚庡姣斾笅
topFrame->setFrameFormat(topFrameFormat);
QTextCharFormat textFormat;
QTextCharFormat boldFormat;
boldFormat.setFontWeight(QFont::Bold);//璁劇疆瀛椾綋錛屼竴浼氫細鐢ㄥ埌
QTextFrameFormat referenceFrameFormat;
referenceFrameFormat.setBorder(1);
referenceFrameFormat.setPadding(8);
referenceFrameFormat.setPosition(QTextFrameFormat::FloatRight);//璇ュ彞浣垮緱鏄劇ず浣嶇疆浠庡彸杈瑰紑濮嬶紝鏀規垚Left鍚庡姣斾笅
referenceFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 40));//QTextLength鏈変笁縐嶇被鍨嬶紝鍚箟濡備笅
Constant Value Description
QTextLength::VariableLength 0 The width of the object is variable
QTextLength::FixedLength 1 The width of the object is fixed
QTextLength::PercentageLength 2 The width of the object is in percentage of the maximum width
cursor.insertFrame(referenceFrameFormat);//鍏充簬璇ュ彞鐨勪粙緇?br /> QTextFrame * QTextCursor::insertFrame ( const QTextFrameFormat & format )
Inserts a frame with the given format at the current cursor position(), moves the cursor position() inside the frame, and returns the frame.
If the cursor holds a selection, the whole selection is moved inside the frame.
娉ㄦ剰緇欑殑鍙傛暟鏄疩TextFramFormat錛岃繑鍥炲兼槸QTextFrame錛屽厜鏍囦細縐誨姩鍒拌Frame鍐?/p>
cursor.insertText("A company", boldFormat);
cursor.insertBlock();//鎹㈣
cursor.insertText("321 City Street");
cursor.insertBlock();
cursor.insertText("Industry Park");
cursor.insertBlock();
cursor.insertText("Another country");
cursor.setPosition(topFrame->lastPosition());
cursor.insertText(name, textFormat);
QString line;
foreach (line, address.split("\n")) {
cursor.insertBlock();
cursor.insertText(line);
}//鍏夋爣琚噸鏂扮Щ鍔ㄥ洖鏉ュ茍涓斿啓鍏ame address
cursor.insertBlock();
cursor.insertBlock();//twice錛屼竴嬈¤〃紺烘崲琛岋紝涓ゆ鎵嶄細浜х敓涓涓┖琛?/p>
QDate date = QDate::currentDate();
cursor.insertText(tr("Date: %1").arg(date.toString("d MMMM yyyy")),
textFormat);//鍙栨棩鏈燂紝insert銆傘傘?br /> cursor.insertBlock();
QTextFrameFormat bodyFrameFormat;
bodyFrameFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
cursor.insertFrame(bodyFrameFormat);
cursor.insertText(tr("I would like to place an order for the following "
"items:"), textFormat);
cursor.insertBlock();
cursor.insertBlock();//nserts standard text into the order form.
//鎺ヤ笅鏉ユ槸涓涓?br />//A QTextTableFormat object, orderTableFormat, is used to hold the type of item and the quantity ordered.
//鐢ㄦ潵瀛樺偍orderItems鍙婂叾鏁伴噺
QTextTableFormat orderTableFormat;
orderTableFormat.setAlignment(Qt::AlignHCenter);
QTextTable *orderTable = cursor.insertTable(1, 2, orderTableFormat);
QTextFrameFormat orderFrameFormat = cursor.currentFrame()->frameFormat();
orderFrameFormat.setBorder(1);
cursor.currentFrame()->setFrameFormat(orderFrameFormat);
//棰樺錛氬叧浜嶲TextTable鐨凷pacing鍜?font>Padding鍦≦TextTable綾葷殑浠嬬粛浜嗘湁涓浘
//鎰熻padding鍗充負澶栧洿鐨勮竟璺濓紝spacing鍗充負鍐呴儴杈硅窛
cursor = orderTable->cellAt(0, 0).firstCursorPosition();
cursor.insertText(tr("Product"), boldFormat);
cursor = orderTable->cellAt(0, 1).firstCursorPosition();
cursor.insertText(tr("Quantity"), boldFormat);
//娣誨姞姣忓垪鐨勬爣棰?font>P Quantity
//涓嶈繃榪欐牱娣誨姞鍑烘潵鐨刢ell鏄彲浠ュ湪鐣岄潰涓婁慨鏀圭殑錛屽浣曢槻姝㈣繖鐐癸紵
//editor->setReadOnly(true);浼氫嬌鏁翠釜欏甸潰涓嶅彲緙栬緫錛屾墍浠ヤ笉鍙敤
for (int i = 0; i < orderItems.count(); ++i) {
QPair<QString,int> item = orderItems[i];
int row = orderTable->rows();//褰撳墠琛屾暟
orderTable->insertRows(row, 1);//鍦ㄦ渶鍚庢坊鍔犻澶栦竴琛?br /> cursor = orderTable->cellAt(row, 0).firstCursorPosition();
cursor.insertText(item.first, textFormat);
cursor = orderTable->cellAt(row, 1).firstCursorPosition();
cursor.insertText(QString("%1").arg(item.second), textFormat);
}
//鍦ㄨ繖孌典唬鐮佸悗鎴戝姞浜嗕竴孌典唬鐮佹祴璇曚簡涓?br /> cursor.insertText("This is a test");
cursor.setPosition(cursor.currentFrame()->firstPosition());
cursor.insertText("another test");
//
//鑷充簬cellAt錛?錛?錛夊鐨勮儗鏅鑹詫紝鏄繖涔堟敼鍙樼殑
QTextTableCell cell = orderTable->cellAt(0,0);
QTextCharFormat format = cell.format();
format.setBackground(Qt::green);
cell.setFormat(format);
/
/
//緇х畫紼嬪簭閲岀殑婧愪唬鐮?br />//The cursor is then moved back to topFrame's lastPosition() and more standard text is inserted.
cursor.setPosition(topFrame->lastPosition());//榪斿洖鍒皌opFrame鐨勬紜綅緗紑濮嬭緭鍏?/p>
cursor.insertBlock();
cursor.insertText(tr("Please update my records to take account of the "
"following privacy information:"));
cursor.insertBlock();
//Another QTextTable is inserted, to display the customer's preference regarding offers.
QTextTable *offersTable = cursor.insertTable(2, 2);
cursor = offersTable->cellAt(0, 1).firstCursorPosition();
cursor.insertText(tr("I want to receive more information about your "
"company's products and special offers."), textFormat);
cursor = offersTable->cellAt(1, 1).firstCursorPosition();
cursor.insertText(tr("I do not want to receive any promotional information "
"from your company."), textFormat);
if (sendOffers)
cursor = offersTable->cellAt(0, 0).firstCursorPosition();
else
cursor = offersTable->cellAt(1, 0).firstCursorPosition();
cursor.insertText("X", boldFormat);
//The cursor is moved to insert "Sincerely" along with the customer's name. More blocks are inserted for spacing purposes. The printAction is enabled to indicate that an order form can now be printed.
cursor.setPosition(topFrame->lastPosition());
cursor.insertBlock();
cursor.insertText(tr("Sincerely,"), textFormat);
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertText(name);
printAction->setEnabled(true);
}
鍏跺疄鎬葷粨涓嬬殑璇濓紝鏁翠釜鐣岄潰鐨勫艦鎴愪富瑕佷緷璧栦簬cursor鐨勭Щ鍔ㄥ拰insert
縐誨姩姣斿榪欐牱
cursor = offersTable->cellAt(0, 1).firstCursorPosition();
鎴栬?br />cursor.setPosition(topFrame->lastPosition());絳夌瓑
杈撳叆涓昏渚濋潬QTextCursor鐨勫洓涓嚱鏁版潵瀹屾垚鐨?br />void insertText ( const QString & text, const QTextCharFormat & format )
QTextFrame * insertFrame ( const QTextFrameFormat & format )
void insertBlock ( const QTextBlockFormat & format, const QTextCharFormat & charFormat )
QTextTable * insertTable ( int rows, int columns, const QTextTableFormat & format )
5.
鍏充簬榪欎竴鍙ュ垎鏋愪竴涓嬫祦紼?br />buttonBox 鐨?accepted錛堬級淇″彿浼氳Е鍙慏etailsDialog鐨剉erify錛堬級妲斤紝璇ユЫ鍑芥暟浼氭墽琛孌ialog鐨刟ccept() or reject()
void QDialog::accept () [virtual slot]
Hides the modal dialog and sets the result code to Accepted.
void QDialog::reject () [virtual slot]
Hides the modal dialog and sets the result code to Rejected.
void QDialog::done ( int r ) [virtual slot]
Closes the dialog and sets its result code to r. If this dialog is shown with exec(), done() causes the local event loop to finish, and exec() to return r.
//TODO錛?br />鏈鍚庣殑print鐣岄潰鐢ㄧ殑QPrintDialog鏉ュ疄鐜幫紝鏈夌┖鍙互鐪嬩笅
QTextCursor浠ュ強鐩稿叧榪橀渶瑕佽繘涓姝ュ涔?br />openDialog鍑芥暟閲岋紝涓嶆墽琛宒ialog.exec()
鐢╠ialog.show(),紿楀彛娌℃湁show鍑烘潵
鍐嶈涓嬪姛鑳斤細
5鍚凲GroupBox鍒嗗埆瀵瑰簲5鍚勬柟闈㈢殑鍔熻兘錛屼篃鍚屾椂瀵瑰簲鐫QLineEidt鐨?涓嚱鏁?br />浠庣涓涓紑濮嬭璧鳳細
鏄劇ず鏂歸潰鐨勶紝鐪嬩笅綾婚噷闈㈣嚜瀹氫箟鐨勬灇涓撅紝姝eソ瀵瑰簲Echo 鐨凜omboxBox鐨?涓夐」
enum QLineEdit::EchoMode
This enum type describes how a line edit should display its contents.
Constant Value Description
QLineEdit::Normal 0 Display characters as they are entered. This is the default.
QLineEdit::NoEcho 1 Do not display anything. This may be appropriate for passwords where even the length of the password should be kept secret.
QLineEdit::Password 2 Display asterisks instead of the characters actually entered.
QLineEdit::PasswordEchoOnEdit 3 Display characters as they are entered while editing otherwise display asterisks.
浣跨敤鐨勫嚱鏁版槸錛?br />void setEchoMode ( EchoMode )
鐩稿叧鐨勫嚱鏁版煡浜嗕笅榪樻湁榪欎袱涓?br />1.displayText : const QString
This property holds the displayed text.
If echoMode is Normal this returns the same as text(); if EchoMode is Password or PasswordEchoOnEdit it returns a string of asterisks text().length() characters long, e.g. "******"; if EchoMode is NoEcho returns an empty string, "".
By default, this property contains an empty string.
2.text : QString
This property holds the line edit's text.
Setting this property clears the selection, clears the undo/redo history, moves the cursor to the end of the line and resets the modified property to false. The text is not validated when inserted with setText().
The text is truncated to maxLength() length.
By default, this property contains an empty string.
欏懼悕鎬濅箟錛宒isplayText榪斿洖鐨勬槸鏄劇ず鍐呭錛屽鏋滄椂瀵嗙爜妯″紡錛岄偅涔堣繑鍥?*************鏄熷彿
絎簩涓細
Validator浣跨敤鐨勫嚱鏁版槸
void QLineEdit::setValidator ( const QValidator * v )
Sets this line edit to only accept input that the validator, v, will accept. This allows you to place any arbitrary constraints on the text which may be entered.
If v == 0, setValidator() removes the current input validator. The initial setting is to have no input validator (i.e. any input is accepted up to maxLength()).
See also validator(), QIntValidator, QDoubleValidator, and QRegExpValidator.
鐪嬩笅紼嬪簭閲屾槸鎬庝箞鐢ㄧ殑
case 0:
validatorLineEdit->setValidator(0);
break;
case 1:
validatorLineEdit->setValidator(new QIntValidator(
validatorLineEdit));
break;
case 2:
validatorLineEdit->setValidator(new QDoubleValidator(-999.0,
999.0, 2, validatorLineEdit));
}
鏄疩LineEdit鍐呭彧鑳借緭鍏ユ暣鏁幫紝0.01綺懼害嫻偣鏁般傚彲浠ョ湅鍒拌繕鍙互鍒╃敤姝e垯鎺у埗杈撳叆鐨勫唴瀹癸細
QRegExp rx("\\d{,6}");
validatorLineEdit->setValidator(new QRegExpValidator(rx,validatorLineEdit));
榪欐牱灝卞彧鑳借緭鍏ユ渶澶?鍚勬暟瀛椾簡錛屽啀閰嶅悎鍓嶉潰鐨勫瘑鐮佹ā寮忚緭鍏ュ氨鍙互鐢ㄦ潵璁劇疆瀵嗙爜浜嗭細錛?br />絎笁涓細璁劇疆瀵歸綈鏂瑰紡
絎笁鍚勭敤鍒扮殑鍑芥暟鏄?br />void setAlignment ( Qt::Alignment flag )
alignment : Qt::Alignment
This property holds the alignment of the line edit.
Both horizontal and vertical alignment is allowed here, Qt::AlignJustify will map to Qt::AlignLeft.
By default, this property contains a combination of Qt::AlignLeft and Qt::AlignVCenter.
闈炲父綆鍗曪紝鐩存帴鐪嬫灇涓劇殑綾誨瀷
enum Qt::AlignmentFlag
flags Qt::Alignment
This enum type is used to describe alignment. It contains horizontal and vertical flags that can be combined to produce the required effect.
The TextElideMode enum can also be used in many situations to fine-tune the appearance of aligned text.
The horizontal flags are:
Constant Value Description
Qt::AlignLeft 0x0001 Aligns with the left edge.
Qt::AlignRight 0x0002 Aligns with the right edge.
Qt::AlignHCenter 0x0004 Centers horizontally in the available space.
Qt::AlignJustify 0x0008 Justifies the text in the available space.
The vertical flags are:
Constant Value Description
Qt::AlignTop 0x0020 Aligns with the top.
Qt::AlignBottom 0x0040 Aligns with the bottom.
Qt::AlignVCenter 0x0080 Centers vertically in the available space.
You can use only one of the horizontal flags at a time. There is one two-dimensional flag:
Constant Value Description
Qt::AlignCenter AlignVCenter | AlignHCenter Centers in both dimensions.
You can use at most one horizontal and one vertical flag at a time. Qt::AlignCenter counts as both horizontal and vertical.
Three enum values are useful in applications that can be run in right-to-left mode:
Constant Value Description
Qt::AlignAbsolute 0x0010 If the widget's layout direction is Qt::RightToLeft (instead of Qt::LeftToRight, the default), Qt::AlignLeft refers to the right edge and Qt::AlignRight to the left edge. This is normally the desired behavior. If you want Qt::AlignLeft to always mean "left" and Qt::AlignRight to always mean "right", combine the flag with Qt::AlignAbsolute.
Qt::AlignLeading AlignLeft Synonym for Qt::AlignLeft.
Qt::AlignTrailing AlignRight Synonym for Qt::AlignRight.
Masks:
Constant Value
Qt::AlignHorizontal_Mask AlignLeft | AlignRight | AlignHCenter | AlignJustify | AlignAbsolute
Qt::AlignVertical_Mask AlignTop | AlignBottom | AlignVCenter
Conflicting combinations of flags have undefined meanings.
The Alignment type is a typedef for QFlags<AlignmentFlag>. It stores an OR combination of AlignmentFlag values.
絎洓涓細鍙互鐢ㄦ潵鎺у埗涓浜涜緭鍏ユ牸寮忥紝鍙互鏃犻』鍊熷姪姝e垯
鐢ㄥ埌鍑芥暟涓昏鏄?br />void setInputMask ( const QString & inputMask )
鐩存帴鐪嬩粙緇?br />inputMask : QString
This property holds the validation input mask.
If no mask is set, inputMask() returns an empty string.
Sets the QLineEdit's validation mask. Validators can be used instead of, or in conjunction with masks; see setValidator().
Unset the mask and return to normal QLineEdit operation by passing an empty string ("") or just calling setInputMask() with no arguments.
The table below shows the characters that can be used in an input mask. A space character, the default character for a blank, is needed for cases where a character is permitted but not required.
Character Meaning
A ASCII alphabetic character required. A-Z, a-z.
a ASCII alphabetic character permitted but not required.
N ASCII alphanumeric character required. A-Z, a-z, 0-9.
n ASCII alphanumeric character permitted but not required.
X Any character required.
x Any character permitted but not required.
9 ASCII digit required. 0-9.
0 ASCII digit permitted but not required.
D ASCII digit required. 1-9.
d ASCII digit permitted but not required (1-9).
# ASCII digit or plus/minus sign permitted but not required.
H Hexadecimal character required. A-F, a-f, 0-9.
h Hexadecimal character permitted but not required.
B Binary character required. 0-1.
b Binary character permitted but not required.
> All following alphabetic characters are uppercased.
< All following alphabetic characters are lowercased.
! Switch off case conversion.
\ Use \ to escape the special characters listed above to use them as separators.
The mask consists of a string of mask characters and separators, optionally followed by a semicolon and the character used for blanks. The blank characters are always removed from the text after editing.
Examples:
Mask Notes
000.000.000.000;_ IP address; blanks are _.
HH:HH:HH:HH:HH:HH;_ MAC address
0000-00-00 ISO Date; blanks are space
>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;# License number; blanks are - and all (alphabetic) characters are converted to uppercase.
To get range control (e.g., for an IP address) use masks together with validators.
Access functions:
QString inputMask () const
void setInputMask ( const QString & inputMask )
See also maxLength.
鐪嬩笅紼嬪簭鐢ㄥ埌鐨勫湴鏂癸細
switch (index) {
case 0:
inputMaskLineEdit->setInputMask("");
break;
case 1:
inputMaskLineEdit->setInputMask("+99 99 99 99 99;_");
break;
case 2:
inputMaskLineEdit->setInputMask("0000-00-00");
inputMaskLineEdit->setText("00000000");
inputMaskLineEdit->setCursorPosition(0);
break;
case 3:
inputMaskLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");
鍏朵腑鍒嗗彿鍚庨潰璺熺殑絎﹀彿浼氫唬鏇挎帀鍓嶉潰鐨勯偅浜涢氶厤絎?br />姣斿閫夊埌License Key 錛孮LIneEdit鍐呭鍙樹負
#####-#####-#####-#####-#####
杈撳叆鏃跺彧鑳借緭鍏ュ瓧姣嶏紝鍚屾椂-浼氳嚜鍔ㄨ煩榪囷紝涓嶅彲鏀瑰彉錛屼笉琚垹闄わ紝鎬諱箣璇曚笅灝辯煡閬撲簡鍝堝搱
5.
寰堢畝鍗曪紝璁劇疆鍐呭鏄惁鍙洿鏀?br />readOnly : bool
This property holds whether the line edit is read only.
In read-only mode, the user can still copy the text to the clipboard, or drag and drop the text (if echoMode() is Normal), but cannot edit it.
QLineEdit does not show a cursor in read-only mode.
By default, this property is false.
鍑犱釜鍑芥暟緇勫悎浣跨敤錛屽姛鑳藉緢寮哄ぇ

璇ヤ緥瀛愭湁涓や釜涓昏鐨勭被錛?br />ShapeItem is a custom widget representing one single shape item.
SortingBox inherits from QWidget and is the application's main widget.
1.SortingBox
SortingBox綾婚噷閲嶆柊瀹炵幇浜嗗緢澶歟vent錛岀敤閫斿涓嬶細
The event() function provides tooltips, the resize() function makes sure the application appears consistently when the user resizes the main widget, and the paintEvent() function displays the shape items within the SortingBox widget. The mouse event handlers are reimplemented to make the user able to move the items around.
涓変釜縐佹湁鐨勬Ы鍑芥暟鍒嗗埆瀵瑰簲鍒涘緩涓夌鍥懼艦
鍦ㄦ瀯閫犲嚱鏁伴噷璋冪敤涓変釜鍑芥暟錛?br />initialItemPosition(), initialItemColor() and createToolButton()
鐪嬪悕瀛楀氨鐭ラ亾鍦ㄥ仛浠涔堜簡
2.
瑕佹樉紺簍ooltip蹇呴』瑕佺煡閬撻紶鏍囩Щ鍔ㄤ簨浠訛紝榛樿鍦ㄤ笉鎸変笅閿椂鏄笉璁板綍榧犳爣杞ㄨ抗鐨勶紝鍥犳闇瑕佽緗笅
setMouseTracking(true);
To be able to show the appropiate tooltips while the user is moving the cursor around, we need to enable mouse tracking for the widget.
If mouse tracking is disabled (the default), the widget only receives mouse move events when at least one mouse button is pressed while the mouse is being moved. If mouse tracking is enabled, the widget receives mouse move events even if no buttons are pressed.
3.濡備綍鐢誨嚭鐨勪笁瑙掑艦錛岀煩褰紝妞渾
鐭╁艦錛屾き鍦嗙敤QPainter鐨勫嚱鏁板氨鍙互浜嗭紝涓夎褰㈢敤drawLine鐢諱笁嬈★紝姣忔鐢諱笁瑙掑艦鏄笉鏄お璐瑰姴浜嗛偅錛熸垨鑰呭鏋滈亣鍒板叾浠栦笉瑙勫垯鐨勫浘褰紝鎬庝箞鐢誨嚭鏉ョ殑鍛紵
榪欓噷灝辯敤鍒頒簡QPainterPath綾諱簡
鍏堢湅涓猘ddRect鍑芥暟
鍐嶇湅涓猘ddEllips
e
鍐嶆潵涓礉娉藉皵鏇?img height="263" alt="" src="http://m.shnenglu.com/images/cppblog_com/izualzhy/QPainterPath__cubicTo.png" width="731" border="0" />綰跨殑
鍏堝畾涔夋洸綰匡紝鐒跺悗鐢≦Painter::drawPath()灝監K浜?br />鏈変竴涓棶棰樻槸錛宒rawPath錛堬級騫朵笉鑳芥寚瀹歱osition
鐪嬩笅瀹氫箟
鍥犳鍦ㄦ兂瑕佸湪鏌愪釜鐐瑰紑濮嬬敾鍑烘兂瑕佺殑鏇茬嚎錛屽厛縐誨姩鍧愭爣緋?br /> foreach (ShapeItem shapeItem, shapeItems) {
//! [8] //! [9]
painter.translate(shapeItem.position());
//! [9] //! [10]
painter.setBrush(shapeItem.color());
painter.drawPath(shapeItem.path());
painter.translate(-shapeItem.position());
}
榪欎箞鍋氱殑瑙i噴錛?br />The painter will by default draw all the shape items at position (0,0) in the SortingBox widget. The QPainter::translate() function translates the coordinate system by the given offset, making each shape item appear at its defined position. But remember to translate the coordinate system back when the item is drawn, otherwise the next shape item will appear at a position relative to the item we drawed last.
4.濡備綍瀹炵幇縐誨姩錛?br />鎬濊冧竴涓嬶紝鑲畾鏄湪mousePressEvent,mouseMoveEvent,moutReleaseEvent涓婁慨鏀廣?br />鑰屼笖璇存槸縐誨姩錛屽叾瀹炴槸瀹屽叏閲嶇粯浜嗕竴閬嶆暣涓獁idget銆?br />閭d箞錛屾庝箞鐭ラ亾褰撳墠榧犳爣鐐瑰湪鍝釜鍥懼艦涓婂憿錛?br />鍏跺疄榪欎釜闂姣斾箣鍓嶆垜鎯崇殑瑕佺畝鍗曪細
紼嬪簭鍒╃敤浜嗗涓嬪嚱鏁幫細
int SortingBox::itemAt(const QPoint &pos)
{
for (int i = shapeItems.size() - 1; i >= 0; --i) {
const ShapeItem &item = shapeItems[i];
if (item.path().contains(pos - item.position()))
return i;
}
return -1;
}
寰幆閬嶅巻姣忎釜shapwItem錛岄嗗簭閬嶅巻鐨勫師鍥犳槸褰撹繖涓偣鍦ㄥ涓猻hapeitem涓婃椂錛岃繑鍥炴渶涓婇潰鐨勪竴涓紝鍒╃敤QPainterPath鐨勫嚱鏁癱ontains
bool QPainterPath::contains ( const QPointF & point ) const
Returns true if the given point is inside the path, otherwise returns false.
璁板綍榧犳爣縐誨姩鍓嶅悗鐨勫亸縐婚噺錛岃緗粰褰撳墠鐨剆hapeItem錛岀劧鍚巙pdate錛堬級閲嶇粯銆?br />鍊煎緱娉ㄦ剰鐨勬槸瀵箂hapeItems錛屽嵆瀛樺偍鍥懼艦鐨勫垪琛ㄧ殑淇敼錛屽亣璁捐浠ュ姩鐨勫浘褰㈠湪鍒楄〃閲岀儲寮曟槸index錛岃皟鐢ㄤ笅鍒楄鍙?br /> shapeItems.move(index, shapeItems.size() - 1);
鏄綋鍓嶄慨鏀逛綅緗殑鍥懼艦璺戝埌鍒楄〃鏈鍚庛?br />5.濡備綍瀹炵幇toolTops鐨勶紵
bool SortingBox::event(QEvent *event)
{
//! [5] //! [6]
if (event->type() == QEvent::ToolTip) {
QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
int index = itemAt(helpEvent->pos());
if (index != -1) {
QToolTip::showText(helpEvent->globalPos(), shapeItems[index].toolTip());
} else {
QToolTip::hideText();
event->ignore();
}
return true;
}
return QWidget::event(event);
}
鐩存帴鐪婹t assistant鐨勮В閲?br />If the type is QEvent::ToolTip, we cast the event to a QHelpEvent, otherwise we propagate the event using the QWidget::event() function.
The QHelpEvent class provides an event that is used to request helpful information about a particular point in a widget.
For example, the QHelpEvent::pos() function returns the event's position relative to the widget to which the event is dispatched. Here we use this information to determine if the position of the event is contained within the area of any of the shape items. If it is, we display the shape item's tooltip at the position of the event. If not, we hide the tooltip and explicitly ignore the event. This makes sure that the calling code does not start any tooltip specific modes as a result of the event. Note that the QToolTip::showText() function needs the event's position in global coordinates provided by QHelpEvent::globalPos().
璇ヤ緥瀛愮敱涓や釜涓昏鐨勭被
MainWindow錛歱ublic QMainWindow
PreviewForm: public QDialog
1.
鏂囨湰杈撳叆鐣岄潰浣跨敤鐨凲TextEdit錛岄氳繃 setCentralWidget(textEdit);鑰岃瀹氫負MainWindow鐨勪富紿楀彛錛屽簲璇ユ槸寰堢啛鎮夎繖涓簡锝烇綖
QTextEdit鏈変竴涓睘鎬ф槸
enum QTextOption::WrapMode
This enum describes how text is wrapped in a document.
Constant Value Description
QTextOption::NoWrap 0 Text is not wrapped at all.
QTextOption::WordWrap 1 Text is wrapped at word boundaries.
QTextOption::ManualWrap 2 Same as QTextOption::NoWrap
QTextOption::WrapAnywhere 3 Text can be wrapped at any point on a line, even if it occurs in the middle of a word.
QTextOption::WrapAtWordBoundaryOrAnywhere 4 If possible, wrapping occurs at a word boundary; otherwise it will occur at the appropriate point on the line, even in the middle of a word.
琛ㄧず鍦ㄨ鏈浣曟崲琛屾垨鑰呬笉鎹㈣銆?br>2.
鐢變簬緙栫爜鏂瑰紡寰堝錛屼負浜嗕嬌鑿滃崟鐪嬩笂鍘葷編瑙備簺錛岃璁劇疆涓涓嬮偅浜涘尮閰嶅埌涓鍧楋紝浜庢槸鐢ㄥ埌浜哘RegExp
榪欓噷鐢ㄧ殑姝e垯鏄緢綆鍗曠殑錛屾垜涔熷彧鐪嬩簡涓嬬敤鍒扮殑涓や釜鍑芥暟
A銆?br>bool QRegExp::exactMatch ( const QString & str ) const
Returns true if str is matched exactly by this regular expression; otherwise returns false. You can determine how much of the string was matched by calling matchedLength().
For a given regexp string R, exactMatch("R") is the equivalent of indexIn("^R$") since exactMatch() effectively encloses the regexp in the start of string and end of string anchors, except that it sets matchedLength() differently.
For example, if the regular expression is blue, then exactMatch() returns true only for input blue. For inputs bluebell, blutak and lightblue, exactMatch() returns false and matchedLength() will return 4, 3 and 0 respectively.
榪欎釜琛ㄧず鐨勬槸瑕佸畬鍏ㄥ尮閰嶏紝鑰屼笉鏄粠涓棿寮濮嬨傜被浼間簬Python閲屾鍒欒〃杈懼紡鐨剆earch()鍜宮atch錛堬級
B銆?br>QString QRegExp::cap ( int nth = 0 ) const
Returns the text captured by the nth subexpression. The entire match has index 0 and the parenthesized subexpressions have indexes starting from 1 (excluding non-capturing parentheses).
閫氳繃鍦╮x閲屾坊鍔犳嫭鍙鋒煡鐪嬭繑鍥炲鹼綖锝?br>鍖歸厤濂戒簡涔嬪悗浼氬悓鏃朵駭鐢熶竴涓猧nt綾誨瀷鐨剅ank鍊鹼紝鍔犲湪codec鐨勫悕瀛楀墠浣滀負key錛宑odec浣滀負value緇勬垚QMap
3.
鍦╬rivate閲屽畾涔変簡QList<QTextCodec *> codecs;
灝嗗垰瀹氫箟鐨凲Map閫氳繃values錛堬級鍑芥暟璧嬬粰璇ョ鏈夊彉閲忥紝娉ㄦ剰values錛堬級鍑芥暟鐨勬弿榪幫細
QList<T> QMap::values () const
Returns a list containing all the values in the map, in ascending order of their keys. If a key is associated with multiple values, all of its values will be in the list, and not just the most recently inserted one.
浼氭湁涓涓『搴忕殑闂錛岃繖涔熸槸鎴戜滑娣誨姞rank鐨勫師鍥犱簡锝烇綖
榪欐牱璧嬪間箣鍚庯紝QList codecs閲屽氨鏄帓搴忎箣鍚庣殑浜嗭綖锝?br>寰楀厛蹇欏伐浣滅殑涓滆タ浜嗭紝鏈夌┖瑕佸啀鐮旂┒涓?/p>
QAbstractScrollArea is a low-level abstraction of a scrolling area. The area provides a central widget called the viewport, in which the contents of the area is to be scrolled (i.e, the visible parts of the contents are rendered in the viewport).
Next to the viewport is a vertical scroll bar, and below is a horizontal scroll bar. When all of the area contents fits in the viewport, each scroll bar can be either visible or hidden depending on the scroll bar's Qt::ScrollBarPolicy. When a scroll bar is hidden, the viewport expands in order to cover all available space. When a scroll bar becomes visible again, the viewport shrinks in order to make room for the scroll bar.
It is possible to reserve a margin area around the viewport, see setViewportMargins(). The feature is mostly used to place a QHeaderView widget above or beside the scrolling area. Subclasses of QAbstractScrollArea should implement margins.1
1.
fontMetrics().width(QLatin1Char('9'))
榪斿洖鍙湁涓涓瓧絎?#8216;9’鏃剁殑鍚堢悊瀹藉害锝烇綖
2.
void QAbstractScrollArea::setViewportMargins ( int left, int top, int right, int bottom )
榪欎釜鍑芥暟涓鐩村紕娣蜂簡錛岃鍑芥暟瀹為檯涓婃槸鍒涘緩浜嗗洓涓猰argin
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
渚嬪璇ヨ鍙ュ嵆鏄湪宸﹁竟鍒涘緩涓涓猰argin锝烇綖铏界劧鍙紶榪涗簡涓涓猧nt鍊鹼紝浣唙iewport鍛ㄥ洿鐨刴argin鏄幆緇曟暣涓獀iewport鐨勶紝鎵浠ュ彟涓鏉¤竟鏄浐瀹氬ぇ灝忕殑錛堟棭鎯沖埌榪欑偣灝變笉浼氱湅涓嶆槑鐧戒簡銆傘傘傦級
Shape Analog Clock example
1.
Qt::FramelessWindowHint鍙互浣跨獥鍙e幓鎺夊叧闂?鏈澶у寲 鏈灝忓寲絳夛紝浣嗘槸闇瑕侀噸鏂拌嚜瀹氫箟move絳変簨浠?br>璇lag鍦≦t閲屼粙緇嶏細
Produces a borderless window. The user cannot move or resize a borderless window via the window system
鍦ㄨ繖涓緥瀛愰噷浜庢槸鎴戜滑闇瑕佽嚜瀹氫箟mouseMoveEvent,mousePressEvent錛屽師鏂囧涓嬶細
We inform the window manager that the widget is not to be decorated with a window frame by setting the Qt::FramelessWindowHint flag on the widget. As a result, we need to provide a way for the user to move the clock around the screen.
璇ヤ緥瀛愬疄鐜板悗鍙互鍦ㄦ闈笂鎷栧姩涓涓被浼間簬琛ㄧ殑widget錛屽悓鏃舵病鏈夎竟妗嗭紝寰堢編瑙傦紝閭d箞瀹氫箟mouseMoveEvent()鏃訛紝鎴戜滑濡備綍紜畾璇idget搴斿綋move鍒頒粈涔堝湴鏂瑰憿錛?br>渚嬪瓙閲屽畾涓涓竴涓猵rivate data 錛?br>QPoint dragPosition;
鍦╩ousePressEvent閲?br>dragPosition = event->globalPos() - frameGeometry().topLeft();
鍦╩ouseMoveEvent閲?br>move(event->globalPos() - dragPosition);
鍗蟲妸榧犳爣鎸変笅鏃剁殑浣嶇疆涓庢帶浠跺乏涓婄殑鍧愭爣璺濈浣滀負涓涓亸縐婚噺
鍘熸枃浠嬬粛濡備笅錛?br>If the left mouse button is pressed over the widget, we record the displacement in global (screen) coordinates between the top-left position of the widget's frame (even when hidden) and the point where the mouse click occurred. This displacement will be used if the user moves the mouse while holding down the left button. Since we acted on the event, we accept it by calling its accept() function.
娉ㄦ剰榪欓噷鏃秅lobalPos()~~
2.sizeHint()
鍒氬紑濮嬫病鏈夊紕鎳備負浠涔堣瀹氫箟sizeHint()
鍘繪帀鍚庡姣斾笅鍥懼儚鍙戠幇鍥懼ぇ浜嗕笉灝戯紝鎵嶆槑鐧藉湪鍥懼儚鏈寚瀹氬ぇ灝忔椂絎竴嬈how鐨勬椂鍊欎細鑷姩璋冪敤sizeHint錛堬級
渚嬪瓙涓鏄庡涓?br>Finally, we implement the sizeHint() for the widget so that it is given a reasonable default size when it is first shown:
鍘繪帀sizeHint鍑芥暟鐨?br>
鐢ㄤ簡sizeHint鐨勫氨姣旇緝緹庤浜?br>3.mask
閭d箞濡備綍鏄獥鍙e憟鐜頒負涓涓渾褰㈢殑鍛紵
鍏跺疄紿楀彛榪樻槸鐭╁艦鐨勶紝涓嶈繃閫氳繃mask璁句負涓閮ㄥ垎鍙錛屼竴閮ㄥ垎閫忔槑浜?br>setMask鏈変袱縐嶅艦寮忥紝涓嶈繃浣滅敤閮芥槸綾諱技鐨?br>void QWidget::setMask ( const QBitmap & bitmap )
Causes only the pixels of the widget for which bitmap has a corresponding 1 bit to be visible
void QWidget::setMask ( const QRegion & region )
Causes only the parts of the widget which overlap region to be visible.
鍗蟲槸鍖哄煙鍐呭彲瑙侊紝鍏朵綑涓嶅彲瑙併?br>鍙﹀涓涓叧浜庨忔槑鎬х殑鍑芥暟涓?br>void setWindowOpacity ( qreal level )
windowOpacity : double
This property holds the level of opacity for the window.
The valid range of opacity is from 1.0 (completely opaque) to 0.0 (completely transparent).
璇ュ嚱鏁版帶鍒剁殑鏄暣涓獁idget鐨勯忔槑鎬э綖
濡傛灉setMask錛堬級涓巗etWindowOpacity錛堬級涓璧風敤浜嗭紝QREgion澶栦粛鏄忔槑鐨勶紝涓嶈繃mask鐨勫尯鍩熼忔槑搴﹀洜涓簊etWindowOpacity錛堬級鏀瑰彉浜嗐?br>榪欐槸鎴戠敤浜嗚鍑芥暟涔嬪悗鐨勪緥瀛?br>
鍙互鐪嬪埌鍙樺緱閫忔槑浜?br>榪欏紶鍥懼姣斿氨鏇存槑鏄句簡
4.濡備綍鎸変笅榧犳爣鍙抽敭寮瑰嚭Action鐨勶紵
鐩存帴new涓涓猀Action鍑烘潵鐒跺悗add灝辮浜嗭綖锝?br>浼拌鏄洜涓哄湪榪欑widget涓嬫病鏈夊叾浠栧湴鏂規斁灝遍粯璁ゆ斁鍦ㄥ彸閿噷浜嗭紝闇瑕佽繘涓姝ュ涔犳墠鑳界‘瀹氾綖锝?/p>
鍦≦t4.0閲孲cribble example閲岀敾綰挎潯鏄厛draw鍦ㄤ竴涓猧mage涓婏紝image鍦ㄩ氳繃drawImage鐢誨埌widget涓婏紝緇欏嚭浜嗚繖鏍風殑鐞嗙敱
At this point, you might wonder why we don't just draw directly onto the widget instead of drawing in a QImage and copying the QImage onto screen in paintEvent(). There are at least three good reasons for this:
The window system requires us to be able to redraw the widget at any time. For example, if the window is minimized and restored, the window system might have forgotten the contents of the widget and send us a paint event. In other words, we can't rely on the window system to remember our image.
Qt normally doesn't allow us to paint outside of paintEvent(). In particular, we can't paint from the mouse event handlers. (This behavior can be changed using the Qt::WA_PaintOnScreen widget attribute, though.)
If initialized properly, a QImage is guaranteed to use 8-bit for each color channel (red, green, blue, and alpha), whereas a QWidget might have a lower color depth, depending on the monitor configuration. This means that if we load a 24-bit or 32-bit image and paint it onto a QWidget, then copy the QWidget into a QImage again, we might lose some information.
image姣攚idget瑕佺◢澶т竴鐐癸紝鐞嗙敱濡備笅
When the user starts the Scribble application, a resize event is generated and an image is created and displayed in the scribble area. We make this initial image slightly larger than the application's main window and scribble area, to avoid always resizing the image when the user resizes the main window (which would be very inefficient). But when the main window becomes larger than this initial size, the image needs to be resized.
褰撻渶瑕侀噸緇樼殑浜嬩歡paintEvent鏃訛紝閫氳繃浼犵粰update錛堬級涓涓弬鏁板彲鑳戒細鎻愰珮鏁堢巼錛屽綋鐒跺墠鎻愭槸浣犵煡閬撹繖涓弬鏁?br>We could call the update() function with no parameter, but as an easy optimization we pass a QRect that specifies the rectangle inside the scribble are needs updating, to avoid a complete repaint of the widget.
渚嬪榪欓噷浼犲叆鐨勬槸錛岄紶鏍囨柊鐢葷殑綰挎 涓庣瑪鐨勫ぇ灝忕粍鎴愮殑涓涓煩褰㈠尯鍩熴傝繖鏍峰彧浼氶噸鏂扮粯鍒惰繖閲屼簡锝烇綖
Painter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawLine(lastPoint, endPoint);
modified = true;
int rad = (myPenWidth / 2) + 2;
update(QRect(lastPoint, endPoint).normalized()
.adjusted(-rad, -rad, +rad, +rad));
褰撴湁澶氫釜signal榪炴帴鍒頒竴涓Ы鏃訛紝渚嬪鍦ㄤ繚瀛樻枃浠舵椂錛岀粰鍑虹殑QAction鏈?BMP JPG JPEG 絳夛紝姝ゆ椂鍙渶瑕佷竴涓猻ave錛堬級鐨勬Ы鍑芥暟錛宻ave錛堬級鍑芥暟闇瑕佺煡閬撴槸鍝釜signal鍙戦佽繃鏉ワ紝姝ゆ椂灝嗘牴鎹甉Object * QObject::sender () const [protected]鏉ュ垽鏂璼ender
璇ュ嚱鏁頒粙緇嶅涓嬶細
QObject * QObject::sender () const [protected]
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. The pointer is valid only during the execution of the slot that calls this function from this object's thread context.
The pointer returned by this function becomes invalid if the sender is destroyed, or if the slot is disconnected from the sender's signal.
Warning: This function violates the object-oriented principle of modularity. However, getting access to the sender might be useful when many signals are connected to a single slot.
Warning: As mentioned above, the return value of this function is not valid when the slot is called via a Qt::DirectConnection from a thread different from this object's thread. Do not use this function in this type of scenario.
鍏蜂綋scribble 閲屼唬鐮佸涓?br> void MainWindow::save()
{
QAction *action = qobject_cast<QAction *>(sender());
QByteArray fileFormat = action->data().toByteArray();
saveFile(fileFormat);
}
絎竴鍙ワ細
QAction *action = qobject_cast<QAction *>(sender());
铏界劧涓嶇煡閬搒ender錛堬級鍏蜂綋鏄摢涓涓猀Action錛屼絾鎴戜滑鐭ラ亾鑲畾鏄竴涓猀Action錛屽洜姝ゅ彲浠ast榪囨潵銆?br>鐢╯tatic_cast<>涔熸槸鍙互鐨勶紝榪欓噷浣跨敤qobject_cast鐨勫師鍥犱緥瀛愰噷璇存槑濡備笅
but as a defensive programming technique we use a qobject_cast(). The advantage is that if the object has the wrong type, a null pointer is returned. Crashes due to null pointers are much easier to diagnose than crashes due to unsafe casts.
絎簩鍙ワ細
QAction閲屾湁setdata錛堬級 鍜?data錛堬級涓や釜鍑芥暟浣滅敤鏄繖鏍風殑
1.
QVariant QAction::data () const
Returns the user data as set in QAction::setData.
2.
void QAction::setData ( const QVariant & userData )
Sets the action's internal data to the given userData.
鍦ㄥ垱寤篞Action鐨勬椂鍊欙紝鎴戜滑setData錛堬級
foreach (QByteArray format, QImageWriter::supportedImageFormats()) {
QString text = tr("%1...").arg(QString(format).toUpper());
QAction *action = new QAction(text, this);
action->setData(format);
connect(action, SIGNAL(triggered()), this, SLOT(save()));
saveAsActs.append(action);
}
姝ゆ椂閫氳繃ction->data().toByteArray();鍙栧嚭鏉?br>鐒跺悗浼犵粰絎笁鍙ワ紝saveFile鍑芥暟灝辯煡閬撹瀛樹負鍝鏍煎紡浜?/p>
ps錛?br>鍦ㄧ晫闈㈢紪紼嬮噷錛屼緥濡傚啓涓涓猻ave錛堬級鍑芥暟錛屽彲鑳絪aveFile錛堬級鎵嶆槸瀹炵幇淇濆瓨鏂囦歡鐨勪綔鐢ㄧ殑銆?br>浣嗙偣鍑籹aveaction鏃訛紝璋冪敤save錛堬級錛屽疄闄呬笂闇瑕佸垽鏂悇縐嶆儏鍐碉紝渚嬪鏂囦歡鍚嶅悎娉?鍚屽悕絳夛紝闇瑕佹牴鎹笉鍚屾儏鍐靛脊鍑轟笉鍚岀殑瀵硅瘽妗嗭紝褰撳叏閮藉垽鏂璽rue鍚庯紝鎵嶄細璋冪敤saveFile錛堬級鍑芥暟錛岃繖鍙兘灝辨槸璺熶箣鍓嶈嚜宸辯紪紼嬪樊鍒瘮杈冨ぇ鐨勬儏鍐典簡锝烇綖