1、Qt 工具栏(
QToolBar、
QAction)添加:
在.h文件中定义变量:
private:
QToolBar*
m_pToolBar;
QAction*
m_pExitAct;
在.cpp文件中依次做如下处理:
//“:/images/close.png”为“工程上
右键
>
添加新文件
>
Qt资源文件”
m_pExitAct
=
new
QAction(
QIcon(
":/images/close.png"),
tr(
"&Exit"),
this);
m_pExitAct->setShortcuts(
QKeySequence::
New);
m_pExitAct->setStatusTip(tr(
"Create
a
new
file"));
connect(
m_pExitAct,
SIGNAL(triggered()),
this,
SLOT(close()));
m_pToolBar
=
new
QToolBar(
"ToolBar",
this);
m_pToolBar->addAction(
m_pExitAct);
this->addToolBar(
m_pToolBar);
m_pToolBar->setToolButtonStyle(
Qt::
ToolButtonIconOnly);
2、Qt两个窗口间发送信号(
signals、
slots),参看Qt4.7.4 customtypesending 示例。
3、QTreeView添加Item:
QAbstractItemModel*
pPageCtrlTIM
=
new
QAbstractItemModel(
ui->
PageTreeV);
QStandardItem*
pItem
=
new
QStandardItem(
m_IconMap[
"Floder"],
"W1");
pPageCtrlTIM->appendRow(pItem);
QStandardItem*
pItemDetail
=
new
QStandardItem(
"W1说明");
pPageCtrlTIM->setItem(pItemModel->indexFromItem(pItem).row(),
1,
pItemDetail);
QStandardItem*
pChild
=
new
QStandardItem(
m_IconMap[
"Page"],
"W11");
pItem->setChild(
0,
pChild);
pChild
=
new
QStandardItem(
m_IconMap[
"Page"],
"W12");
pItem->setChild(
1,
pChild);
pItem
=
new
QStandardItem(
m_IconMap[
"Floder"],
"W2");
pPageCtrlTIM->appendRow(pItem);
pItemDetail
=
new
QStandardItem(
"W2说明");
pPageCtrlTIM->setItem(pItemModel->indexFromItem(pItem).row(),
1,
pItemDetail);
ui->
PageTreeV->
setModel(pPageCtrlTIM);
4、QTreeView遍历:
QTreeWidget
*
PageTreeW;
int
iItemCount
=
ui->
PageTreeW->topLevelItemCount();
for(
int
i
=
0;
i
<
iItemCount;
i++)
{
QTreeWidgetItem*
pItem
=
ui->
PageTreeW->topLevelItem(i);
if(NULL
==
pItem)
continue;
int
iChildCount
=
pItem->childCount();
for(
int
i
=
0;
i
<
iChildCount;
i++)
{
QTreeWidgetItem*
pChildItem
=
pItem->child(i);
}
}
5、QTreeView节点删除:
QTreeWidgetItem*
pParItem
=
pCurPageItem->parent();
if(NULL
==
pParItem)
//父节点为根节点
{
pCurPageItem->takeChildren();
ui->
PageTreeW->takeTopLevelItem(
ui->
PageTreeW->indexOfTopLevelItem(pCurPageItem));
}
else
{
pParItem->removeChild(pCurPageItem);
ui->
PageTreeW->setCurrentItem(pParItem);
}
6、自定义指针转
QVariant:
//.h自定义头文件中
class
QPageCfgInfo
{
public:
explicit
QPageCfgInfo();
};
Q_DECLARE_METATYPE(QPageCfgInfo*)
//引用自定义头文件的cpp中
QPageCfgInfo*
pPageCfg
=
new
QPageCfgInfo();
if(NULL
==
pPageCfg)
return
false;
pNewPageItem
=
new
QTreeWidgetItem(
ui->
PageTreeW,
QStringList(pPageCfg->GetPageName()));
if(NULL
==
pNewPageItem){
delete
pPageCfg;pPageCfg
=
NULL;
return
false;}
pNewPageItem->setFlags(pNewPageItem->flags()
|
Qt::
ItemIsEditable);
pNewPageItem->setIcon(
0,
m_IconMap[
"Page"]);
pNewPageItem->
setData(
0,
PageTreeRole,
QVariant::fromValue(pPageCfg));
void
QPageTreeWidget::PageTreeItemChanged(
QTreeWidgetItem
*pItem,
int
iColumn)
{
if((NULL
==
pItem)
||
(
0
>
iColumn))
{
return;
}
QVariant
varData
=
pItem->
data(
0,
PageTreeRole);
QPageCfgInfo*
pPageCfg
=
varData.value();
if(NULL
==
pPageCfg)
return;
if(pItem->text(
0)
!=
pPageCfg->GetPageName())
{
pPageCfg->SetPageName(pItem->text(
0));
}
}
7、使用
QDomDocument自定义xml文件的读写类,构建不通过,报如下错误:
.cpp:5:
undefined reference to `_imp___ZN12QDomDocumentC1Ev'
.cpp:5: undefined reference to `_imp___ZN12QDomDocumentC1Ev'
.cpp:11: undefined reference to `_imp___ZN25QDomProcessingInstructionC1Ev'
.cpp:12: undefined reference to `_imp___ZN12QDomDocument27createProcessingInstructionERK7QStringS2_'
.cpp:12: undefined reference to `_imp___ZN25QDomProcessingInstructionaSERKS_'
.cpp:13: undefined reference to `_imp___ZN8QDomNode11appendChildERKS_'
.cpp:13: undefined reference to `_imp___ZN8QDomNodeD1Ev'
.cpp:15: undefined reference to `_imp___ZN12QDomDocument13createElementERK7QString'
.cpp:16: undefined reference to `_imp___ZN8QDomNode11appendChildERKS_'
.cpp:16: undefined reference to `_imp___ZN8QDomNodeD1Ev'
.cpp:23: undefined reference to `_imp___ZNK8QDomNode6isNullEv'
.cpp:25: undefined reference to `_imp___ZN11QDomElementC1ERKS_'
添加各种头文件,即使将自定义类继承子QObject仍编译不通过,修改方法为:
打开.pro工程文件,将:
QT
+=
core
gui
修改为:
QT
+=
core
gui
\
xml
8、使用
QDomDocument方式写xml文件:
QDomDocument
docItem;
QDomProcessingInstruction
myProcInstruct;
myProcInstruct
=
docItem.createProcessingInstruction(tr(
"xml"),
tr(
"version=\"1.0\"
encoding=\"UTF-8\""));
docItem.appendChild(myProcInstruct);
QDomElement
xmlItemRoot
=
docItem.createElement(
"PageItems");
docItem.appendChild(xmlItemRoot);
QDomElement
xmlItem
=
docItem.createElement(
"Item");
xmlItemParent.appendChild(xmlItem);
xmlItem.setAttribute(tr(
"iType"),
5);
xmlItem.setAttribute(tr(
"iStartX"),
0);
xmlItem.setAttribute(tr(
"iStartY"),
0);
xmlItem.setAttribute(tr(
"iStopX"),
100);
xmlItem.setAttribute(tr(
"iStopY"),
100);
QString
sItemXml
=
QApplication::applicationDirPath()
+
"/CreGuiCreator/Pages/page1
.xml";
QFile
fileItemXml(sItemXml);
if(!fileItemXml.
open(
QIODevice::
WriteOnly
|
QIODevice::
Truncate))
{
return
false;
}
QTextStream
xmlOut(&fileItemXml);
xmlOut.setCodec(
"UTF-8");
docItem.save(xmlOut,
4);
8、使用
QDomDocument方式 读xml文件:
QString
sItemXml
=
QApplication::applicationDirPath()
+
"/CreGuiCreator/Pages/
page1
.xml";
QFile
fileItemXml(sItemXml);
QDomDocument
docItem;
if(!fileItemXml.
open(
QIODevice::
ReadOnly
|
QIODevice::
Text))
{
return;
}
if(!docItem.setContent(&fileItemXml,
false))
{
return;
}
QDomElement
xmlItemRoot
=
docItem.documentElement();
if(tr(
"PageItems")
!=
xmlItemRoot.tagName())
{
return;
}
QDomElement
xmlItem
=
xmlItemRoot.firstChildElement(tr(
"Item"));
while(!xmlItem.isNull())
{
int
iType
=
xmlItem.attribute(tr(
"iType")).toInt();
int
iStartX
=
xmlItem.attribute(tr(
"iStartX")).toDouble();
int
iStartY
=
xmlItem.attribute(tr(
"iStartY")).toDouble();
int
iStopX
=
xmlItem.attribute(tr(
"iStopX")).toDouble();
int
iStopY
=
xmlItem.attribute(tr(
"iStopY")).toDouble();
xmlItem
=
xmlItem.nextSiblingElement(
"Item");
}