It is simple to construct a tree view displaying data from a model. In the following example, the contents of a directory are supplied by a QFileSystemModel and displayed as a tree:
QFileSystemModel *model = new QFileSystemModel
model->setRootPath(QDir::currentPath())
QTreeView *tree = new QTreeView(splitter)
tree->setModel(model)
如果要使用QTreeWidget 就要使用QFileInfo对文件夹进行遍历,挨个添加到QTreeWidget中。有关QFileInfo请参考帮助文档:http://qt-project.org/doc/qt-4.8/qfileinfo.html
TreeWidget 与 TreeView 中实现右键菜单稍有不同,TreeView 中是靠信号与槽
connect(ui->treeView,SIGNAL(customContextMenuRequested(constQPoint&)), this,SLOT(onCustomContextMenuRequested(constQPoint&)))
实现TreeView内不同地方的右键菜单,文章见 http://blog.csdn.net/liukang325/article/details/22734735
TreeWidget 中是靠重构 contextMenuEvent(QContextMenuEvent*event)
函数实现不同地方的不同右键菜单的。具体代码如下:
h文件:
[cpp] view plaincopy
private slots:
void onGrpMenuTriggered(QAction *action)
void onMemberMenuTriggered(QAction *action)
void onAddAreaMenuTriggered(QAction *action)
private:
//grp menu
QMenu *m_grpMenu
//child menu
QMenu *m_memberMenu
QMenu *m_addAreaMenu
//grp action
QAction *m_grpAddOneMember
QAction *m_grpDeleteGrp
QAction *m_grpAddOneGrp
//child action
QAction *m_memberDelete
QAction *m_memberMove
QAction *m_memberSyncTime
//action
QAction *m_addAreaAction
QAction *m_grpFlush
QAction *m_addDev
void createItemMenu()//构造函数中调用此函数
cpp文件:
[cpp] view plaincopy
void MainWindow::createItemMenu()
{
m_grpMenu = new QMenu(this)
m_memberMenu = new QMenu(this)
m_addAreaMenu = new QMenu(this)
connect(m_grpMenu,SIGNAL(triggered(QAction *)),
this,SLOT(onGrpMenuTriggered(QAction *)))
connect(m_memberMenu,SIGNAL(triggered(QAction *)),
this,SLOT(onMemberMenuTriggered(QAction *)))
connect(m_addAreaMenu,SIGNAL(triggered(QAction *)),
this,SLOT(onAddAreaMenuTriggered(QAction *)))
m_grpAddOneMember = new QAction(tr("增加子区域"),this)
m_grpAddOneMember->setIcon(QIcon(":/image/add.png"))
m_grpAddOneGrp = new QAction(tr("增加设备"),this)
m_grpAddOneGrp->setIcon(QIcon(":/image/add.png"))
m_grpDeleteGrp = new QAction(tr("删除区域"),this)
m_grpDeleteGrp->setIcon(QIcon(":/image/delete.png"))
m_memberDelete = new QAction(tr("删除设备"),this)
m_memberDelete->setIcon(QIcon(":/image/delete.png"))
m_memberMove = new QAction(tr("移动设备"),this)
m_memberMove->setIcon(QIcon(":/image/next.png"))
m_memberSyncTime = new QAction(tr("同步时间"),this)
m_memberSyncTime->setIcon(QIcon(":/image/move.png"))
m_addAreaAction = new QAction(tr("增加区域"),this)
m_addAreaAction->setIcon(QIcon(":/image/add.png"))
m_addDev = new QAction(tr("增加设备"),this)
m_addDev->setIcon(QIcon(":/image/add.png"))
m_grpFlush = new QAction(tr("刷新列表"),this)
m_grpFlush->setIcon(QIcon(":/image/update.png"))
}
//重构contextMenuEvent函数,记得#include <QContextMenuEvent>
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QTreeWidgetItem *item = ui->treeWidget->currentItem()
if(item == NULL)
{
qDebug()<<"空白处"
if (m_addAreaMenu->isEmpty())
{
//增加区域
m_addAreaMenu->addAction(m_addAreaAction)
m_addAreaMenu->addAction(m_addDev)
m_addAreaMenu->addAction(m_grpFlush)
}
//菜单出现的位置为当前鼠标的位置
m_addAreaMenu->exec(QCursor::pos())
}
else if(ui->treeWidget->currentItem()->childCount() >0)
{
if (m_grpMenu->isEmpty())
{
//增加子区域
m_grpMenu->addAction(m_grpAddOneMember)
//删除区域
m_grpMenu->addAction(m_grpDeleteGrp)
m_grpMenu->addAction(m_grpAddOneGrp)
}
m_grpMenu->exec(QCursor::pos())
}
else if(ui->treeWidget->currentItem()->childCount() == 0)
{
if (m_memberMenu->isEmpty())
{
//删除设备
m_memberMenu->addAction(m_memberDelete)
//移动设备
m_memberMenu->addAction(m_memberMove)
//同步时间
m_memberMenu->addAction(m_memberSyncTime)
}
m_memberMenu->exec(QCursor::pos())
}
event->accept()
}
void MainWindow::onGrpMenuTriggered(QAction *action)
{
if (action == m_grpAddOneMember)
{//增加子区域
}
else if (action == m_grpDeleteGrp)
{//删除区域
}
else if(action == m_grpAddOneGrp)
{
}
}
void MainWindow::onMemberMenuTriggered(QAction *action)
{
if (action == m_memberDelete)
{//删除设备
}
else if(action == m_memberMove)
{//移动设备
}
else if(action == m_memberSyncTime)
{//同步时间
}
}
void MainWindow::onAddAreaMenuTriggered(QAction *action)
{
if (action == m_addAreaAction)
{//增加区域
}
if(action == m_addDev)
{//增加设备
}
if(action == m_grpFlush)
{//刷新
}
}
树型结构的基本用法:
[cpp] view plaincopy
void MainWindow::createTreeWidget()
{
QIcon icon
icon.addPixmap(QPixmap(":/image/openStatus.png"), QIcon::Normal, QIcon::On)//节点打开状态
icon.addPixmap(QPixmap(":/image/closeStatus.png"), QIcon::Normal, QIcon::Off)//节点关闭状态
ui->treeWidget->setColumnCount(1)
QStringList headers
headers <<QObject::tr("管理")
ui->treeWidget->setHeaderLabels(headers)
QStringList rootName_1
rootName_1 <<QObject::tr("常用文件夹")
QTreeWidgetItem *rootItem_1 = new QTreeWidgetItem(ui->treeWidget, rootName_1)
// rootItem_1->setIcon(0, QIcon(":/image/add.png"))//增加静态图标
rootItem_1->setIcon(0,icon)
QStringList childName_1_1
childName_1_1 <<"所有未读"
QTreeWidgetItem *childItem_1_1 = new QTreeWidgetItem(rootItem_1, childName_1_1)
rootItem_1->addChild(childItem_1_1)
QStringList childName_1_2
childName_1_2 <<QObject::tr("置顶邮件")
QTreeWidgetItem *childItem_1_2 = new QTreeWidgetItem(rootItem_1, childName_1_2)
rootItem_1->addChild(childItem_1_2)
QStringList rootName_2
rootName_2 <<QObject::tr("我的邮箱")
QTreeWidgetItem *rootItem_2 = new QTreeWidgetItem(ui->treeWidget, rootName_2)
rootItem_2->setIcon(0,icon)
QStringList childName_2_1
childName_2_1 <<QObject::tr("收件箱")
QTreeWidgetItem *childItem_2_1 = new QTreeWidgetItem(rootItem_2, childName_2_1)
rootItem_2->addChild(childItem_2_1)
QStringList childName_2_2
childName_2_2 <<QObject::tr("草稿箱")
QTreeWidgetItem *childItem_2_2 = new QTreeWidgetItem(rootItem_2, childName_2_2)
rootItem_2->addChild(childItem_2_2)
QStringList childName_2_3
childName_2_3 <<QObject::tr("发件箱")
QTreeWidgetItem *childItem_2_3 = new QTreeWidgetItem(rootItem_2, childName_2_3)
rootItem_2->addChild(childItem_2_3)
ui->treeWidget->addTopLevelItem(rootItem_1)
ui->treeWidget->addTopLevelItem(rootItem_2)
QStringList childName_1_2_1
childName_1_2_1 <<"测试"
QTreeWidgetItem *childItem_1_2_1 = new QTreeWidgetItem(childItem_1_2, childName_1_2_1)
childItem_1_2->addChild(childItem_1_2_1)
}
另一篇TreeWidget 的用法,更加清晰明了的实现树状结构:http://blog.csdn.net/liukang325/article/details/13768523
补充:
childItem_1_2_1->setForeground(0,QBrush(QColor(Qt::blue)))
// 可将某一项的字体变颜色!
①执行“视图→工具栏→控件工具箱”命令,展开“其他控件”按钮,“控件工具箱”工具栏②单击工具栏上,在d出的下拉列表中,选择“ShockwaveFlashObject”选项,当鼠标变成细十字线,按住左键在工作区中拖出一个矩形(这就是后来的播放窗口)。当
③鼠标移到矩形框入右下角拖动方向的箭头,按住左键并拖动来调整矩形的大小的权利。在矩形框
④单击鼠标右键,在随后d出的快捷菜单中选择“属性”选项,打开“属性”对话框中,输入需要在“电影”插入Flash动画文件名后面的框和完整路径,然后选择关闭“属性”窗口。
【注】:为了方便移动演示,与保存在同一文件夹中的Flash动画文件的最佳表现,那么路径也可以使用相对路径。
[提示]:Flash动画文件名和完整路径需要插入最好的英文字符!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)