Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

As in Qt to make a combobox similar to the settings (⋮) in Android



  • Hi!
    As in QWidget to make a combobox similar to the settings (⋮) in Android


  • Lifetime Qt Champion

    Hi
    Im not sure how that looks or works..
    What is the difference from a normal combobox ?



  • So it is possible, but how to bind the slot to choice?

        QMenu* menu = new QMenu(this);
        menu->addAction(tr("Настройки"));
        menu->addAction(tr("О программе"));
        ui->pushButton->setMenu(menu);
    

  • Lifetime Qt Champion

    Hi
    https://doc.qt.io/qt-5/qmenu.html#addAction
    returns the action so you can connect to its triggered() signal



  • Did so, but the slot does not start:

        QMenu* menu = new QMenu(this);
        menu->addAction(tr("Настройки"));
        menu->addAction(tr("О программе"));
        ui->optionButton->setMenu(menu);
    
        connect(menu, SIGNAL(triggered()), this, SLOT(testSlot()));
    

  • Lifetime Qt Champion

    hi
    and what does connect return ?
    If you check the docs, you will see that it has a parameter.
    so i think you connect is invalid.

    connect(menu, SIGNAL(triggered(bool)), this, SLOT(testSlot(bool)));
    and make sure the testSlot do in fact take a bool. ( by adding it )



  • It works:

    connect(menu, SIGNAL(aboutToShow()), this, SLOT(testSlot()));
    

    It's not work:

        connect(menu, SIGNAL(triggered(tr("Настройки"))), this, SLOT(testSlot()));
    

    I need to get the signal right from the menu.



  • Ant this not works:

        QMenu* menu = new QMenu(this);
        QAction *action0 = new QAction(this);
        QAction *action1 = new QAction(this);
        action0->setText("Настройки");
        action1->setText("О программе");
        menu->addAction(action0);
        menu->addAction(action1);
        //menu->addAction(tr("Настройки"));
        //menu->addAction(tr("О программе"));
        ui->optionButton->setMenu(menu);
    
        connect(menu, SIGNAL(triggered(action0)), this, SLOT(testSlot()));
    


  • @Mikeeeeee said in As in Qt to make a combobox similar to the settings (⋮) in Android:

    Ant this not works:

        QMenu* menu = new QMenu(this);
        QAction *action0 = new QAction(this);
        QAction *action1 = new QAction(this);
        action0->setText("Настройки");
        action1->setText("О программе");
        menu->addAction(action0);
        menu->addAction(action1);
        //menu->addAction(tr("Настройки"));
        //menu->addAction(tr("О программе"));
        ui->optionButton->setMenu(menu);
    
        connect(menu, SIGNAL(triggered(action0)), this, SLOT(testSlot()));
    

    You should write the prototype of the signal.

    void QMenu::triggered(QAction *action)
    void QAction::triggered(bool checked = false)

    So, if you are trying to use these signal then you could follow the below examples

    connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(testSlot()));
    // OR
    connect(action0, SIGNAL(triggered(bool)), this, SLOT(testSlot()));
    connect(action1, SIGNAL(triggered(bool)), this, SLOT(testSlot()));
    

    Note: QMenu::triggered pass the triggered action as parameter while QAction::triggered is useful to connect a specific action to specific slots.



  • Thanks, it's working!
    How to remove the triangle on the button to the right of the text? So, to remained only the text.



  • @Mikeeeeee
    Use a simple stylesheet on your button

    ui->optionButton->setStyleSheet("QAbstractButton::menu-indicator{width:0}");
    

Log in to reply