tags:

views:

1965

answers:

2

I have a few combo-boxes and double spin boxes on my QT Dialog. Now I need a "ResetToDefault" item on a menu that comes up when you right click on the widget (spin box or combo box). How do i get it. Is there some way I can have a custom menu that comes up on right click or Is there a way i can add items to the menu that comes on right click.

kindly tell me a way to achieve this.

Regards, Arjun

+3  A: 

First, for Qt4, the simplest way is to create an action to reset the data, and add it the the widget using the addAction method (or use the designer). Then, set the contextMenuPolicy attribute to Qt::ActionsContextMenu. The context menu will appear and the action will be triggered.

Code example:

QAction *reset_act = new QAction("Reset to default");
mywidget->addAction(reset_act);
mywidget->setContextMenuPolicy(Qt::ActionsContextMenu);
// here connect the 'triggered' signal to some slot

For Qt3, you might have to intercept the context menu event, and thus inherit the QSpinBox and others. Or maybe you can intercept the context menu event from the main window, detect if it occurred above the widget supposed to have a context menu (using the QWidget::childAt method) and show it there. But you'll have to test.

PierreBdR
thanks for the input, if there is a context menu that is already appearing, can i add items to it?
AJ
If the context menu appears by using the action list, then adding an action will automatically add your entry after the others. Otherwise, this is more tricky.
PierreBdR
The context menu for the editable combo box and the spinbox comes from the QLineEdit class and that is not created from the widget's actions.
David Dibben
Is there anything that needs to be done explicitly to delete an QAction item. Since I have done a new, I am not sure where I should call the delete. Also, it works fine, but crashes at the end during destruction. So i guess I am missing something
AJ
A: 

For Qt4, you can do this for an editable QComboBox by using your own QLineEdit. Create a derived QLineEdit class which implements the contextMenuEvent

class MyLineEdit : public QLineEdit
{
    Q_OBJECT
public:

    MyLineEdit(QWidget* parent = 0) : QLineEdit(parent){}

    void contextMenuEvent(QContextMenuEvent *event)
    {
        QPointer<QMenu> menu = createStandardContextMenu();
        //add your actions here
        menu->exec(event->globalPos());
        delete menu;
    }

};

Then, use the setLineEdit function of QComboBox to set the line edit

MyLineEdit* edit = new MyLineEdit();
comboBox->setLineEdit(edit);

The combo box will now use your line edit class. The createStandardContextMenu function creates the normal context menu and you can add any new actions to it that you like in the contextMenuEvent handler before it is shown.

If the QComboBox is not editable then it doesn't have a default context menu so using the Qt::ActionsContextMenu method is much simpler.

QAbstractSpinBox also has a setLineEdit function so you can use a similar technique. Although, for some reason the setLineEdit function is protected on QAbstractSpinBox but public on QLineEdit.

David Dibben