QT Qtreewidget操作 右键菜单响应等


想完成树节点的右键菜单操作,查阅了很多资料后,终于找到了一种解决方法。但是不敢保证这是好方法,如果有更好的方法,可以留言,共同进步。


qtcreator创建了一个mainwindow工程。

界面添加一个treewidget,命名tree1;


mainwindow.h代码:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:

    void on_tree1_doubleClicked(const QModelIndex &index);

    void on_tree1_customContextMenuRequested(const QPoint &pos);

    void on_1_1_menu();

    void on_2_menu();

    void on_3_menu();
private:
    Ui::MainWindow *ui;

private:
    QMenu *m_pmenu;
    QAction *m_pact1_1;
    QAction *m_pact2;
    QAction *m_pact3;
    QMenu *menu1;
};

#endif // MAINWINDOW_H
mainwindow.cpp代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    ui->tree1->setColumnCount(1); //设置列数
    ui->tree1->setHeaderLabel(tr("图像选择")); //设置头的标题

    QTreeWidgetItem *imageItem1 =
            new QTreeWidgetItem(ui->tree1,QStringList(QString("图像1")));
    imageItem1->setIcon(0,QIcon("xxx.png"));
    QTreeWidgetItem *imageItem1_1 =
            new QTreeWidgetItem(imageItem1,QStringList(QString("First"))); //子节点1
    imageItem1->addChild(imageItem1_1); //添加子节点

    QTreeWidgetItem *imageItem2 =
            new QTreeWidgetItem(ui->tree1,QStringList(QString("图像2")));
    QTreeWidgetItem *imageItem2_1 =
            new QTreeWidgetItem(imageItem2,QStringList(QString("Second"))); //子节点1
    QTreeWidgetItem *imageItem2_2 =
            new QTreeWidgetItem(imageItem2,QStringList(QString("Third"))); //子节点2
    imageItem2->addChild(imageItem2_1);  //添加子节点
    imageItem2->addChild(imageItem2_2);

    ui->tree1->expandAll(); //结点全部展开

    ////右键菜单的操作,包括一级菜单,二级菜单
    m_pmenu = new QMenu( );//tree的右键菜单


    menu1 = m_pmenu->addMenu("菜单项1");//->addAction("1-1");
    m_pact1_1 = menu1->addAction("1-1");
    menu1->addAction("1-2");
    m_pact2 = m_pmenu->addMenu("菜单项2")->addAction("2-2");
    m_pact3 = m_pmenu->addAction("菜单项3");
    connect(m_pact1_1,SIGNAL(triggered()),this,SLOT(on_1_1_menu()));
    connect(m_pact2,SIGNAL(triggered()),this,SLOT(on_2_menu()));
    connect(m_pact3,SIGNAL(triggered()),this,SLOT(on_3_menu()));

}

MainWindow::~MainWindow()
{
    delete ui;
//    delete m_pmenu;
    delete m_pact1_1;
    delete m_pact2;
    delete menu1;
    delete m_pact3;
    delete m_pmenu;
}

void MainWindow::on_tree1_doubleClicked(const QModelIndex &index)
{
    QString str = index.data(Qt::DisplayRole).toString();
    QMessageBox::information(this,"tips",str);
}

//右键菜单
void MainWindow::on_tree1_customContextMenuRequested(const QPoint &pos)
{
    ///树控件右键菜单显示
    if (ui->tree1->itemAt(pos))
    {
        m_pmenu->exec(QCursor::pos()); //光标显示位置
    }
}

void MainWindow::on_1_1_menu()
{
    QMessageBox::information(NULL,"Tip","1-1 menu");
}

void MainWindow::on_2_menu()
{
    QMessageBox::information(NULL,"Tip","2 menu");
}

void MainWindow::on_3_menu()
{
    QMessageBox::information(NULL,"Tip","3 menu");
}



你可能感兴趣的:(Qt,qt,treewidget,右键菜单响应)