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

}

效果如下:

Qt之自定义菜单(右键菜单)

Qt之自定义菜单(右键菜单)

   二级菜单的实现(包括三级菜单或者更多)也类似,只需要使用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之自定义菜单(托盘菜单)

更多参考:

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

}

效果如下:

Qt之自定义菜单(按钮菜单)

Qt之自定义菜单(按钮菜单)

   两者区别很明显,第一个有下拉三角,第二个没有。去掉下拉三角添加如下代码即可: remove_watermarks_button->setStyleSheet("QPushButton::menu-indicator{image:None;}");

自定义QLineEdit的右键菜单,可以通过重写QLineEdit的contextMenuEvent()函数来实现。下面是一个示例代码: ```cpp CustomLineEdit::CustomLineEdit(QWidget *parent) : QLineEdit(parent) { setContextMenuPolicy(Qt::CustomContextMenu); connect(this, &CustomLineEdit::customContextMenuRequested, this, &CustomLineEdit::showCustomContextMenu); } void CustomLineEdit::showCustomContextMenu(const QPoint &pos) { QMenu menu(this); QAction *action = menu.addAction("Custom Action"); connect(action, &QAction::triggered, this, &CustomLineEdit::onCustomActionTriggered); menu.exec(mapToGlobal(pos)); } void CustomLineEdit::onCustomActionTriggered() { // 处理自定义操作 } ``` 在上面的代码中,我们首先通过setContextMenuPolicy()函数将QLineEdit的上下文菜单策略设置为Qt::CustomContextMenu,然后在构造函数中连接customContextMenuRequested信号到我们自己的槽函数showCustomContextMenu()。 在showCustomContextMenu()函数中,我们创建一个QMenu对象,并添加我们自己的自定义操作。这里我们只添加了一个名为"Custom Action"的操作,当用户点击这个操作时,会触发onCustomActionTriggered()槽函数。 最后,我们在showCustomContextMenu()函数中调用menu.exec()函数来显示菜单。由于我们需要将菜单显示在鼠标点击的位置,所以需要使用mapToGlobal()函数将QPoint对象转换为全局坐标系。 在onCustomActionTriggered()函数中,我们可以处理我们自己的自定义操作。这里我们只是简单地打印一条消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值