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. [SOLVED] Combobox menu question

[SOLVED] Combobox menu question

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 9.6k 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    when i right click the QComboBox, is there a way for me to add the "delete field" to the menu?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Yes, there is. The dropdown list that you get is a QAbstractItemView. You can supply your own view, or just your own menu if you want. Use QComboBox::view() to gain access to it.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #3

        hi Andre. do you have a link to the docs for me to read or an example? thank you.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I don't have a readymade example for you, I'm sorry.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kalster
            wrote on last edited by
            #5

            the combobox right click menu items are not displayed when i have the following code in it. how to get the "delete me" menu item displayed in the menu without the default menu items from disappearing?

            @ QAction *reset_act = new QAction("delete me",this);
            ui->userName->addAction(reset_act);
            ui->userName->setContextMenuPolicy(Qt::ActionsContextMenu);@

            1 Reply Last reply
            1
            • K Offline
              K Offline
              kalster
              wrote on last edited by
              #6

              can someone please solve this topic. thank you.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                [quote author="kalster" date="1319348374"]can someone please solve this topic. thank you.[/quote]

                You can add "[Solved]" to the topic yourself, just hit the edit link to the right of the first post in the thread and adjust the title. You also may add a tag "solved".

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  @Kalster:

                  Do you mean that you solved the issue, or that you still want somebody to look at it?

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalster
                    wrote on last edited by
                    #9

                    i meant that i still want someone to look at it. i really need an example for this topic.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      Here is a code snippet that should get you going. At first, i thought you wanted a context menu on the actual list dropping down, but after re-reading, I figure that you want to extend the menu on the line edit part. Here is some code doing that. Note that it only works for editable comboboxes. For non-editable ones, it will crash.

                      The header that goed with it is trivial, so not shown.

                      @

                      MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                      {
                      ui->setupUi(this);
                      qDebug() << "Current context menu policy:" << ui->comboBox->contextMenuPolicy();

                      ui->comboBox->setContextMenuPolicy(Qt::CustomContextMenu);
                      
                      m_deleteAction = new QAction(tr("Delete item"), this);
                      connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
                      
                      connect(ui->comboBox, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
                      

                      }

                      MainWindow::~MainWindow()
                      {
                      delete ui;
                      }

                      void MainWindow::deleteItem()
                      {
                      qDebug() << "we should delete this item";
                      }

                      void MainWindow::contextMenuRequest(QPoint pnt)
                      {
                      QMenu* menu = ui->comboBox->lineEdit()->createStandardContextMenu();
                      menu->addSeparator();
                      menu->addAction(m_deleteAction);
                      menu->popup(pnt);
                      connect(menu, SIGNAL(aboutToHide()), menu, SLOT(deleteLater()));
                      }
                      @

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kalster
                        wrote on last edited by
                        #11

                        thank you Andre. the code works perfectly :)

                        i hope this topic helps others. i had a minor problem. the menu was displayed at the top left corner of the screen. see the below code for the fix.

                        replace the first line of code with the second line of code.
                        @menu->popup(pnt);
                        menu->popup(QCursor::pos());@

                        Edit: I am using vista

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          Code worked for me without modification (windows 7), but I am glad you got it sorted.

                          1 Reply Last reply
                          0

                          • Login

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