怎么向treewidget中添加
1个回答
展开全部
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)));
// 可将某一项的字体变颜色!
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)));
// 可将某一项的字体变颜色!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询