本文共 1389 字,大约阅读时间需要 4 分钟。
实现方式:createActions用于创建菜单、菜单项,contextMenuEvent用于实现菜单的显示,translateLanguage用于实现菜单的文本(此方法主要设置多语化使用)
void ImageTree::createActions(){ //创建菜单、菜单项 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); //连接信号与槽 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); //菜单出现的位置为当前鼠标的位置 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"));}
转载地址:http://qvqxi.baihongyu.com/