1

I have a method to delete the list of files on the list widget:

void MainWindow::on_listWidget_clicked(const QModelIndex &index)
{        
    qDeleteAll(ui->listWidget->selectedItems());
}

But I want to implement a right click button where it gives an option to delete it. I'm not sure how to proceed.

demonplus
  • 5,613
  • 12
  • 49
  • 68
user5603723
  • 195
  • 2
  • 11

3 Answers3

2

You need to inherit QListWidget and catch mouse click event

mylistwidget.h :

    #ifndef MYLISTWIDGET_H
    #define MYLISTWIDGET_H

    #include <QListWidget>

    class MyListWidget : public QListWidget
    {
        Q_OBJECT
    public:
        MyListWidget(QWidget *parent = 0);
        ~MyListWidget();
    private:
        void mousePressEvent(QMouseEvent *event);
    signals:
        void rightClick(QPoint* pos);
    };

    #endif // MYLISTWIDGET_H

mylistwidget.cpp:

    #include "mylistwidget.h"

    #include <QMouseEvent>

    MyListWidget::MyListWidget(QWidget *parent) :
        QListWidget(parent)
    {

    }

    MyListWidget::~MyListWidget()
    {

    }

    void MyListWidget::mousePressEvent(QMouseEvent *event)
    {
        if(event->button() == Qt::RightButton){
            emit rightClick(&event->pos());
        } else {
            QListWidget::mousePressEvent(event);
        }
    }

create object and connect to a slot:

MyListWidget* listWidget = new MyListWidget(this);
connect(listWidget,SIGNAL(rightClick(QPoint*)),
        this,SLOT(onRightClick(QPoint*)));

get item at position in the slot:

void onRightClick(QPoint *pos)
{
    QListWidgetItem* item = listWidget->itemAt(pos);
}

do whatever you like with the item :)

Angel.Risteski
  • 516
  • 6
  • 20
2

On Windows the right click may not cause a QMouseEvent at all. On my Linux system only the pressEvent is fired but no releaseEvent.

Your can implement the `void contextMenuEvent(QContextMenuEvent *e)' which is fired on all systems.

Like this:

/*virtual*/ void YourListWidgetDerivedClass::contextMenuEvent(QContextMenuEvent * e)
{
    // if you only want the mouse context events (like right click)
    if(e->reason != QContextMenuEvent::Mouse) return;

    // get the item unter the mouse cursor
    QListWidgetItem * clickedItem = itemAt(e->pos());

    // do what you like here
    // e.g. show a dialog to ask whether the item should be deleted
    // or show a widget with delete button (you can position it freely with setGeometry(...))
    // etc.
}

If you change context menu policy of your list using setContextMenuPolicy to Qt::CustomContextMenu, the signal customContextMenuRequested() is emitted if one needs that behavior. But the approach above works with the default property setting.

Aaron
  • 1,181
  • 7
  • 15
0
  1. If you want a right-click context menu, use this: How to add a custom right-click menu to a webpage?

  2. If you simply want to detect a right-click mouse event use this: How can I capture the right-click event in JavaScript? (But note that not everybody has a right-click or knows how to use it)


function rightclick() {
    var rightclick;
    var e = window.event;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
    alert(rightclick); // true or false, you can trap right click here by if comparison
}
Community
  • 1
  • 1
CoderPi
  • 12,985
  • 4
  • 34
  • 62