日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Qt之自定义菜单

發(fā)布時(shí)間:2024/9/27 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt之自定义菜单 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. Qt之自定義菜單(右鍵菜單)

在接觸Qt這段時(shí)間以來(lái),經(jīng)常遇到菜單項(xiàng)的問(wèn)題(右鍵菜單、托盤(pán)菜單、按鈕菜單等),QMenu用于菜單欄,上下文菜單,彈出菜單等,利用QMenu+QAction就可以達(dá)到效果!

???右鍵菜單實(shí)現(xiàn):通過(guò)重寫(xiě)contextMenuEvent(QContextMenuEvent *event)事件,QMenu+QAction即可完美實(shí)現(xiàn)!

????對(duì)象:QTreeWidget

???實(shí)現(xiàn)方式:createActions用于創(chuàng)建菜單、菜單項(xiàng),contextMenuEvent用于實(shí)現(xiàn)菜單的顯示,translateLanguage用于實(shí)現(xiàn)菜單的文本(此方法主要設(shè)置多語(yǔ)化使用)

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"));}

效果如下:

???二級(jí)菜單的實(shí)現(xiàn)(包括三級(jí)菜單或者更多)也類似,只需要使用QMenu的addMenu()方法即可!關(guān)于右鍵二級(jí)菜單的東西之前介紹過(guò),詳情請(qǐng)參閱:QTableWidget詳解(樣式、右鍵菜單、表頭塌陷、多選等)。

2.?Qt之自定義菜單(托盤(pán)菜單)

繼右鍵菜單之后,再次探討托盤(pán)菜單。。。也許你對(duì)有些東西會(huì)感覺(jué)到莫名其妙,但大致思路不變,因?yàn)檫@些東西都是我根據(jù)實(shí)際項(xiàng)目所述。

??托盤(pán)菜單實(shí)現(xiàn):通過(guò)QSystemTrayIcon+QMenu+QAction即可完美實(shí)現(xiàn)!

???實(shí)現(xiàn)方式:createActions用于創(chuàng)建菜單、菜單項(xiàng),translateActions用于設(shè)置文本、實(shí)現(xiàn)多語(yǔ)化,translateAccount用于設(shè)置用戶空間配額。

void TrayMenu::createActions()

{

????help_menu = new QMenu();

????//創(chuàng)建菜單項(xiàng)

????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"));

????//添加菜單項(xiàng)

???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);

????//設(shè)置信號(hào)連接

????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);

}

???托盤(pán)菜單項(xiàng)建立完成之后,在建立自己的托盤(pán),包括:托盤(pán)圖標(biāo)、托盤(pán)提示信息等。

QSystemTrayIcon *system_tray = new QSystemTrayIcon();

//放在托盤(pán)提示信息、托盤(pán)圖標(biāo)

system_tray ->setToolTip(QString("我就是托盤(pán)"));
system_tray ->setIcon(QIcon(":/icon/login"));

TrayMenu *tray_menu = new TrayMenu();
system_tray->setContextMenu(tray_menu);

//點(diǎn)擊托盤(pán)執(zhí)行的事件
connect(system_tray?, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
????connect(tray_menu, SIGNAL(showWidget()), this, SLOT(showNormal()));

//顯示托盤(pán)
system_tray->show();

//托盤(pán)顯示提示信息

system_tray->showMessage(QString("托盤(pán)標(biāo)題"), QString("托盤(pán)顯示內(nèi)容"));

效果如下:

??

更多參考:

  • Qt之系統(tǒng)托盤(pán)(QSystemTrayIcon詳解)。

3.?Qt之自定義菜單(按鈕菜單)

?再次探討Qt的菜單,按鈕菜單也是很常用的東東,使用QPushButton(QToolButton)+QMenu+QAction即可完美實(shí)現(xiàn)!

???實(shí)現(xiàn)方式:createButton用于創(chuàng)建按鈕以及菜單,translateLanguage用于設(shè)置文本、實(shí)現(xiàn)多語(yǔ)化。

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"));

}

效果如下:

???兩者區(qū)別很明顯,第一個(gè)有下拉三角,第二個(gè)沒(méi)有。去掉下拉三角添加如下代碼即可: remove_watermarks_button->setStyleSheet("QPushButton::menu-indicator{image:None;}");

總結(jié)

以上是生活随笔為你收集整理的Qt之自定义菜单的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。