Qt之自定义菜单
1. Qt之自定義菜單(右鍵菜單)
在接觸Qt這段時間以來,經常遇到菜單項的問題(右鍵菜單、托盤菜單、按鈕菜單等),QMenu用于菜單欄,上下文菜單,彈出菜單等,利用QMenu+QAction就可以達到效果!
???右鍵菜單實現:通過重寫contextMenuEvent(QContextMenuEvent *event)事件,QMenu+QAction即可完美實現!
????對象:QTreeWidget
???實現方式: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"));}效果如下:
???二級菜單的實現(包括三級菜單或者更多)也類似,只需要使用QMenu的addMenu()方法即可!關于右鍵二級菜單的東西之前介紹過,詳情請參閱:QTableWidget詳解(樣式、右鍵菜單、表頭塌陷、多選等)。
2.?Qt之自定義菜單(托盤菜單)
繼右鍵菜單之后,再次探討托盤菜單。。。也許你對有些東西會感覺到莫名其妙,但大致思路不變,因為這些東西都是我根據實際項目所述。
??托盤菜單實現:通過QSystemTrayIcon+QMenu+QAction即可完美實現!
???實現方式:createActions用于創建菜單、菜單項,translateActions用于設置文本、實現多語化,translateAccount用于設置用戶空間配額。
void TrayMenu::createActions()
{
????help_menu = new QMenu();
????//創建菜單項
????action_show = new QAction(this);
????action_quit = new QAction(this);
????action_login_home = new QAction(this);
????action_account = new QAction(this);
????action_user_space = new QAction(this);
????action_help = new QAction(this);
????action_about = new QAction(this);
????action_check_update = new QAction(this);
????action_setting = new QAction(this);
???help_menu->setIcon(QIcon(":/icon/help"));
???action_show->setIcon(QIcon(":/icon/open"));
???action_login_home->setIcon(QIcon(":/icon/home"));
???action_account->setIcon(QIcon(":/icon/user"));
???action_help->setIcon(QIcon(":/icon/help"));
???action_about->setIcon(QIcon(":/icon/about"));
???action_check_update->setIcon(QIcon(":/icon/update"));
???action_setting->setIcon(QIcon(":/icon/set"));
???action_quit->setIcon(QIcon(":/icon/quit"));
????//添加菜單項
???help_menu->addAction(action_about);
???help_menu->addAction(action_help);
???help_menu->addAction(action_check_update);
???this->addAction(action_show);
???this->addAction(action_login_home);
???this->addSeparator();
???this->addAction(action_account);
???this->addAction(action_user_space);
???this->addSeparator();
???this->addAction(action_setting);
???this->addMenu(help_menu);
???this->addAction(action_quit);
????//設置信號連接
????connect(action_show, SIGNAL(triggered(bool)), this, SIGNAL(showWidget()));
????connect(action_quit, SIGNAL(triggered(bool)), this, SIGNAL(logoutWidget()));
????connect(action_setting, SIGNAL(triggered(bool)), this, SIGNAL(setUp()));
????connect(action_about, SIGNAL(triggered(bool)), this, SIGNAL(aboutUs()));
????connect(action_login_home, SIGNAL(triggered(bool)), MenuAction::getInstance(), SLOT(openLoginHome()));
????connect(action_help, SIGNAL(triggered(bool)), MenuAction::getInstance(), SLOT(openHelpMe()));
QObject::connect(action_check_update, SIGNAL(triggered(bool)), MenuAction::getInstance(), SLOT(openCheckUpdate()));
}
void TrayMenu::translateActions()
{
???help_menu->setTitle(tr("help"));
???action_show->setText(tr("open"));
???action_quit->setText(tr("quit"));
???action_login_home->setText(tr("login home"));
???this->translateAccount();
???action_help->setText(tr("instruction"));
???action_about->setText(tr("about us"));
???action_check_update->setText(tr("check update"));
???action_setting->setText(tr("setting"));
}
void TrayMenu::translateAccount()
{
???action_user_space->setText(tr("use:") + use_space + QString(" ?") + tr("total:") + total_space);
}
???托盤菜單項建立完成之后,在建立自己的托盤,包括:托盤圖標、托盤提示信息等。
QSystemTrayIcon *system_tray = new QSystemTrayIcon();
//放在托盤提示信息、托盤圖標
system_tray ->setToolTip(QString("我就是托盤"));
system_tray ->setIcon(QIcon(":/icon/login"));
TrayMenu *tray_menu = new TrayMenu();
system_tray->setContextMenu(tray_menu);
//點擊托盤執行的事件
connect(system_tray?, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
????connect(tray_menu, SIGNAL(showWidget()), this, SLOT(showNormal()));
//顯示托盤
system_tray->show();
//托盤顯示提示信息
system_tray->showMessage(QString("托盤標題"), QString("托盤顯示內容"));
效果如下:
??
更多參考:
- Qt之系統托盤(QSystemTrayIcon詳解)。
3.?Qt之自定義菜單(按鈕菜單)
?再次探討Qt的菜單,按鈕菜單也是很常用的東東,使用QPushButton(QToolButton)+QMenu+QAction即可完美實現!
???實現方式:createButton用于創建按鈕以及菜單,translateLanguage用于設置文本、實現多語化。
void WatermarksToolWidget::createButton()
{ ???
????remove_watermarks_button = new QPushButton();
????remove_menu = new QMenu();
????remove_selected_action = new QAction(remove_menu);
????remove_all_action = new QAction(remove_menu);
???remove_menu->addAction(remove_selected_action);
???remove_menu->addAction(remove_all_action);
???remove_watermarks_button->setMenu(remove_menu);
???//remove_watermarks_button->setStyleSheet("QPushButton::menu-indicator{image:None;}");
}
void WatermarksToolWidget::translateLanguage()
{
???remove_watermarks_button->setText(tr("remove"));
???remove_selected_action->setText(tr("remove selected watermarks"));
???remove_all_action->setText(tr("remove all watermarks"));
}
效果如下:
???兩者區別很明顯,第一個有下拉三角,第二個沒有。去掉下拉三角添加如下代碼即可: remove_watermarks_button->setStyleSheet("QPushButton::menu-indicator{image:None;}");
總結
- 上一篇: 用u盘怎么重置密码是多少次 多少次可以使
- 下一篇: 国内第六代战斗机曝光?