qt 实现右键菜单
? 右鍵菜單實(shí)現(xiàn):通過重寫contextMenuEvent(QContextMenuEvent *event)事件,QMenu+QAction即可完美實(shí)現(xiàn)!
實(shí)現(xiàn)方式:createActions用于創(chuàng)建菜單、菜單項(xiàng),contextMenuEvent用于實(shí)現(xiàn)菜單的顯示,translateLanguage用于實(shí)現(xiàn)菜單的文本(此方法主要設(shè)置多語化使用)
void ImageTree::createActions() {//創(chuàng)建菜單、菜單項(xiàng)pop_menu = new QMenu();add_images_action = new QAction(this);?add_folder_action = new QAction(this);remove_selected_action = new QAction(this);remove_all_action = new QAction(this);//連接信號(hào)與槽connect(add_images_action, &QAction::triggered, this, &ImageTree::selectImages);connect(add_folder_action, &QAction::triggered, this, &ImageTree::selectFolder);connect(remove_selected_action, &QAction::triggered, this, &ImageTree::removeSelectedImages);connect(remove_all_action, &QAction::triggered, this, &ImageTree::removeAllImages); }void ImageTree::contextMenuEvent(QContextMenuEvent *event) {//清除原有菜單pop_menu->clear();pop_menu->addAction(add_images_action);pop_menu->addAction(add_folder_action);pop_menu->addAction(remove_selected_action);pop_menu->addAction(remove_all_action);//菜單出現(xiàn)的位置為當(dāng)前鼠標(biāo)的位置pop_menu->exec(QCursor::pos());event->accept(); }void ImageTree::translateLanguage() {add_images_action->setText(tr("add images"));add_folder_action->setText(tr("add folder"));remove_selected_action->setText(tr("remove selected images"));remove_all_action->setText(tr("remove all images")); }總結(jié)
- 上一篇: LeetCode 198 打家劫舍
- 下一篇: [CCF] 201612-2 工资计算