本文主要是介绍右击 QTreeWidgetItem,弹出选择菜单,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先看效果:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QTreeWidget>
#include <QMenu>
#include <QAction>
#include <QVBoxLayout>
#include <QDebug>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE/* 注册自己的结构体 */
struct userData_t
{QString sz_name;QString sz_address;userData_t(QString n, QString addr);userData_t();
};
Q_DECLARE_METATYPE(userData_t);class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:void updateView(std::vector<userData_t> &showData);private slots://响应右击void userClicked(QPoint pos);private:Ui::MainWindow *ui;QTreeWidget *m_tree;};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"userData_t::userData_t(QString n, QString addr) : sz_name(n), sz_address(addr)
{qRegisterMetaType<userData_t>("userData_t");
}userData_t::userData_t() : sz_name("NA"), sz_address("NA")
{qRegisterMetaType<userData_t>("userData_t");
}MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);m_tree = new QTreeWidget();m_tree->setColumnCount(1);m_tree->setHeaderLabel("--demo---");m_tree->setContextMenuPolicy(Qt::CustomContextMenu);connect(m_tree, &QTreeWidget::customContextMenuRequested,this,&MainWindow::userClicked);QVBoxLayout *lay = new QVBoxLayout();lay->addWidget(m_tree);lay->setMargin(0);lay->setSpacing(0);ui->groupBox->setLayout(lay);#if 1//测试用做一个假数据std::vector<userData_t> tVec;tVec.push_back(userData_t("月读", "00110010"));tVec.push_back(userData_t("天照", "00110011"));tVec.push_back(userData_t("地爆天星", "00111010"));tVec.push_back(userData_t("神罗天征", "10111010"));tVec.push_back(userData_t("葵花宝典", "10111010"));updateView(tVec);
#endif
}MainWindow::~MainWindow()
{delete ui;
}响应右击 /
void MainWindow::userClicked(QPoint pos)
{QTreeWidgetItem* curItem= m_tree->itemAt(pos);if(curItem==NULL) return;QVariantMap varMap = curItem->data(0,Qt::UserRole).toMap();userData_t ret = varMap["DATA"].value<userData_t>(); //获取数据QAction *placeAct = new QAction("开始学习");connect(placeAct, &QAction::triggered, this, [&](){qDebug() << QString("【技能:%1】 ---- 【 ID:%2】").arg(ret.sz_name).arg(ret.sz_address);});QAction *deleteAct = new QAction("下次再说");connect(deleteAct, &QAction::triggered, this, [&](){qDebug() << "算了";});QMenu menu(this);menu.addAction(placeAct);menu.addAction(deleteAct);menu.exec( m_tree->mapToGlobal(pos) );
}更新视图 /
void MainWindow::updateView(std::vector<userData_t> &showData)
{m_tree->clear();m_tree->setColumnCount(1);QTreeWidgetItem *firstTreeItem = new QTreeWidgetItem();m_tree->addTopLevelItem(firstTreeItem);firstTreeItem->setText(0, "0.0");firstTreeItem->setBackgroundColor(0, QColor::fromRgb(222,236,252));for (size_t i = 0; i < showData.size(); i++){QTreeWidgetItem *blkChild = new QTreeWidgetItem();blkChild->setText(0, showData[i].sz_name);//设置userDataQVariantMap data;data["DATA"] = QVariant::fromValue<userData_t>(showData[i]);blkChild->setData(0, Qt::UserRole, data);blkChild->setBackgroundColor(0, QColor::fromRgb(255,255,240));firstTreeItem->addChild(blkChild);}m_tree->expandAll();
}
这篇关于右击 QTreeWidgetItem,弹出选择菜单的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!