Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. As in Qt to make a combobox similar to the settings (⋮) in Android
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 681 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        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);
        
        mrjjM 1 Reply Last reply
        0
        • M Mikeeeeee

          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);
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mikeeeeee
            wrote on last edited by
            #5

            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()));
            
            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 )

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on last edited by
                  #8

                  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()));
                  
                  KillerSmathK 1 Reply Last reply
                  0
                  • M Mikeeeeee

                    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()));
                    
                    KillerSmathK Offline
                    KillerSmathK Offline
                    KillerSmath
                    wrote on last edited by KillerSmath
                    #9

                    @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.

                    @Computer Science Student - Brazil
                    Web Developer and Researcher
                    “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                    1 Reply Last reply
                    2
                    • M Offline
                      M Offline
                      Mikeeeeee
                      wrote on last edited by
                      #10

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

                      KillerSmathK 1 Reply Last reply
                      0
                      • M Mikeeeeee

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

                        KillerSmathK Offline
                        KillerSmathK Offline
                        KillerSmath
                        wrote on last edited by
                        #11

                        @Mikeeeeee
                        Use a simple stylesheet on your button

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

                        @Computer Science Student - Brazil
                        Web Developer and Researcher
                        “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                        1 Reply Last reply
                        2

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved