October 21, 2011

kalster kalster
Lab Rat
315 posts

[SOLVED] Combobox menu question

 

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

11 replies

October 21, 2011

Andre Andre
Robot Herder
6393 posts

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.

October 21, 2011

kalster kalster
Lab Rat
315 posts

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

October 21, 2011

Andre Andre
Robot Herder
6393 posts

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

October 21, 2011

kalster kalster
Lab Rat
315 posts

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?

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

October 23, 2011

kalster kalster
Lab Rat
315 posts

can someone please solve this topic. thank you.

October 23, 2011

Volker Volker
Ant Farmer
5428 posts
kalster wrote:
can someone please solve this topic. thank you.

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

October 23, 2011

Andre Andre
Robot Herder
6393 posts

@Kalster:

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

October 24, 2011

kalster kalster
Lab Rat
315 posts

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

October 24, 2011

Andre Andre
Robot Herder
6393 posts

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.

  1. MainWindow::MainWindow(QWidget *parent) :
  2.     QMainWindow(parent),
  3.     ui(new Ui::MainWindow)
  4. {
  5.     ui->setupUi(this);
  6.     qDebug() << "Current context menu policy:" << ui->comboBox->contextMenuPolicy();
  7.  
  8.     ui->comboBox->setContextMenuPolicy(Qt::CustomContextMenu);
  9.  
  10.     m_deleteAction = new QAction(tr("Delete item"), this);
  11.     connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
  12.  
  13.     connect(ui->comboBox, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
  14. }
  15.  
  16. MainWindow::~MainWindow()
  17. {
  18.     delete ui;
  19. }
  20.  
  21. void MainWindow::deleteItem()
  22. {
  23.     qDebug() << "we should delete this item";
  24. }
  25.  
  26. void MainWindow::contextMenuRequest(QPoint pnt)
  27. {
  28.     QMenu* menu = ui->comboBox->lineEdit()->createStandardContextMenu();
  29.     menu->addSeparator();
  30.     menu->addAction(m_deleteAction);
  31.     menu->popup(pnt);
  32.     connect(menu, SIGNAL(aboutToHide()), menu, SLOT(deleteLater()));
  33. }

October 25, 2011

kalster kalster
Lab Rat
315 posts

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.

  1. menu->popup(pnt);
  2. menu->popup(QCursor::pos());

Edit: I am using vista

October 25, 2011

Andre Andre
Robot Herder
6393 posts

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

 
  ‹‹ QModelIndex invalid when returning from a dialog      eventfilter crashes ››

You must log in to post a reply. Not a member yet? Register here!