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.
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 :)