[SOLVED] Combobox menu question
when i right click the QComboBox, is there a way for me to add the “delete field” to the menu?
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?
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) :
- ui(new Ui::MainWindow)
- qDebug() << "Current context menu policy:" << ui->comboBox->contextMenuPolicy();
- m_deleteAction = new QAction(tr("Delete item"), this);
- connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
- delete ui;
- void MainWindow::deleteItem()
- qDebug() << "we should delete this item";
- void MainWindow::contextMenuRequest(QPoint pnt)
- QMenu* menu = ui->comboBox->lineEdit()->createStandardContextMenu();
- connect(menu, SIGNAL(aboutToHide()), menu, SLOT(deleteLater()));
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.
Edit: I am using vista