How to make QMenu Item checkable in QT
Asked Answered
T

2

13

How to give Qmenu item checkable using QT

QMenu *preferenceMenu = new QMenu();
preferenceMenu  = editMenu->addMenu(tr("&Preferences"));

QMenu *Mode1 = new QMenu();
Mode1  = preferenceMenu->addMenu(tr("&Mode 1"));
Mode1->addAction(new QAction(tr("&Menu1"), this));

QMenu *Mode2 = new QMenu();
Mode2  = preferenceMenu->addMenu(tr("&Mode 2"));
Mode2->addAction(new QAction(tr("&Menu2"), this));
Mode2->addAction(new QAction(tr("&Menu3"), this));

On QAction I called slot "slotActionTriggered(QAction* actionSelected)"

void csTitleBar::slotActionTriggered(QAction* actionSelected)
{
   actionSelected->setChecked(true);
}

How to show small TICK in the selected Menu# so that user can know which is selected Currently I am able to change to all Menu#, but I need to show a small tick on menu so that the selected one can be easly Identified

Typography answered 10/7, 2017 at 7:32 Comment(3)
See QAction::setCheckable and QAction::setChecked.Harrod
I tried both but its not giving any effect, whether I need to keep some image for that?Typography
Please edit your question to show what you've tried.Harrod
F
17

Small example:

enter image description here

cmainwindow.h

#ifndef CMAINWINDOW_H
#define CMAINWINDOW_H

#include <QMainWindow>
#include <QPointer>

class CMainWindow : public QMainWindow
{
   Q_OBJECT

public:
   CMainWindow(QWidget *parent = 0);
   ~CMainWindow();

private slots:
   void slot_SomethingChecked();

private:
   QPointer<QAction> m_p_Act_Button1 = nullptr;
   QPointer<QAction> m_p_Act_Button2 = nullptr;
};

#endif // CMAINWINDOW_H

cmainwindow.cpp

#include "cmainwindow.h"
#include <QtWidgets>
#include <QDebug>

CMainWindow::CMainWindow(QWidget *parent)
   : QMainWindow(parent)
{
   m_p_Act_Button1 = new QAction("Super Button 1", this);
   m_p_Act_Button1->setCheckable(true);
   m_p_Act_Button1->setChecked(true);
   connect(m_p_Act_Button1, SIGNAL(triggered()), this, SLOT(slot_SomethingChecked()));

   m_p_Act_Button2 = new QAction("Super Button 2", this);
   m_p_Act_Button2->setCheckable(true);
   m_p_Act_Button2->setChecked(true);
   connect(m_p_Act_Button2, SIGNAL(triggered()), this, SLOT(slot_SomethingChecked()));

   QMenu *p_menu = menuBar()->addMenu("My Menu");
   p_menu->addAction(m_p_Act_Button1);
   p_menu->addAction(m_p_Act_Button2);
}

CMainWindow::~CMainWindow() { }

void CMainWindow::slot_SomethingChecked()
{
   if(!m_p_Act_Button1 || !m_p_Act_Button2) {return;}

   qDebug() << "Hi";
   if(m_p_Act_Button1->isChecked())
   {
      qDebug() << "The action 1 is now checked";
   }
   else
   {
      qDebug() << "The action 1 is now unchecked";
   }

   if(m_p_Act_Button2->isChecked())
   {
      qDebug() << "The action 2 is now checked";
   }
   else
   {
      qDebug() << "The action 2 is now unchecked";
   }

}
Fishery answered 10/7, 2017 at 10:10 Comment(0)
S
0

Modern connection syntax is preferable. Checkable items can also be used on context menu as follow :

void MyWidget::initContextMenu()
{
     if( _contextMenu)
     {
          delete _contextMenu;
     }

     _contextMenu = new QMenu(this);
     auto action1 = new QAction("Action Item 1", &_contextMenu);
     action1->setCheckable(true);
     action1->setChecked(getMyProperty1());
     connect(action1 , &QAction::triggered, this, &MyWidget::setMyProperty1);
     _contextMenu->AddAction( action1 );
}

void MyWidget::contextMenuEvent(QContextMenuEvent *event)
{        
    initContextMenu();      
    _contextMenu.popup(
            this->viewport()->mapToGlobal(
                event->pos()));         
}

bool MyWidget::setMyProperty1(bool state)
{
    _myProperty1 = state;
    emit MyPropertyChanged(_myProperty);
}
Sello answered 13/6, 2022 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.