QTreeWidget (两层)点击右键菜单删除节点 [百度空间]

http://apps.hi.baidu.com/share/detail/32584829

 

 

 

void MainWindow::show_contextmenu(const QPoint &pos)
{
    m_currentItem = ui->treeWidget->itemAt(pos);
    if( m_currentItem == 0)
        return;
    QMenu *cmenu = new QMenu(ui->treeWidget);
    QAction *deleAction = cmenu->addAction("Delete");
    QAction *openAction = cmenu->addAction("Open");
    if(m_currentItem->parent() == 0)
    {
        QAction *inviteAction = cmenu->addAction("Invide");
        connect(inviteAction, SIGNAL(triggered(bool)), this, SLOT(onInvide()));
    }
    connect(deleAction, SIGNAL(triggered(bool)), this, SLOT(onDele()));
    connect(openAction, SIGNAL(triggered(bool)), this, SLOT(onOpen()));
    cmenu->exec(QCursor::pos());

}

//在树形结点上,右键菜单删除 需要操作数据库
void MainWindow::onDele()
{
    //删除的是问卷结点
    if(m_currentItem->parent() == 0)
    {
        int i = ui->treeWidget->indexOfTopLevelItem(m_currentItem);
        m_currentItem->takeChildren();
        ui->treeWidget->takeTopLevelItem(i);
    }
    else//删除用户结点
    {
        m_currentItem->parent()->removeChild(m_currentItem);
    }


    //数据库操作
}

你可能感兴趣的:(QTreeWidget (两层)点击右键菜单删除节点 [百度空间])