Qt添加事件过滤器,设置拖放listWidget、TreeWidget、TableWidget控件。
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//设置控件可以拖放ui->listWidgetSourse->setAcceptDrops(true);ui->listWidgetSourse->setDragEnabled(true);ui->listWidgetSourse->setDefaultDropAction(Qt::CopyAction);ui->listWidgetSourse->setDragDropMode(QAbstractItemView::DragDrop);ui->listWidget->setAcceptDrops(true);ui->listWidget->setDragEnabled(true);ui->listWidget->setDefaultDropAction(Qt::CopyAction);ui->listWidget->setDragDropMode(QAbstractItemView::DragDrop);ui->treeWidget->setAcceptDrops(true);ui->treeWidget->setDragEnabled(true);ui->treeWidget->setDefaultDropAction(Qt::CopyAction);ui->treeWidget->setDragDropMode(QAbstractItemView::DragDrop);ui->tableWidget->setAcceptDrops(true);ui->tableWidget->setDragEnabled(true);ui->tableWidget->setDefaultDropAction(Qt::MoveAction);ui->tableWidget->setDragDropMode(QAbstractItemView::DragDrop);//为小部件添加事件过滤器ui->listWidgetSourse->installEventFilter(this);ui->listWidget->installEventFilter(this);ui->treeWidget->installEventFilter(this);ui->tableWidget->installEventFilter(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::refreshToUI(QGroupBox *currGroupBox)
{ui->checkBoxAcceptDrops->setChecked(m_itemView->acceptDrops());ui->checkBoxDropEnabled->setChecked(m_itemView->dragEnabled());ui->comboBox->setCurrentIndex((int)m_itemView->dragDropMode());int index = getGroupActionIndex(m_itemView->defaultDropAction());ui->comboBoxDefaultAction->setCurrentIndex(index);QFont fout = ui->groupBoxSourse->font();fout.setBold(false);ui->groupBox->setFont(fout);ui->groupBoxList->setFont(fout);ui->groupBoxSourse->setFont(fout);ui->groupBoxTree->setFont(fout);fout.setBold(true);currGroupBox->setFont(fout);
}int MainWindow::getGroupActionIndex(Qt::DropAction actionType)
{switch (actionType) {case Qt::CopyAction :return 0;case Qt::MoveAction :return 1;case Qt::LinkAction :return 2;case Qt::ActionMask :return 3;default:return 0;}
}Qt::DropAction MainWindow::getGroupActionTyp(int index)
{switch (index) {case 0:return Qt::CopyAction;case 1:return Qt::MoveAction;case 2:return Qt::LinkAction;case 3:return Qt::ActionMask;default:return Qt::CopyAction;}
}//设置返回值,如果将返回值设置为eventFilter(watched,event);,程序直接奔溃,目前还没有找到原因
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{if(event->type() != QEvent::KeyPress){return false;// return eventFilter(watched,event);}QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event);if(keyEvent->key() != Qt::Key_Delete){return false;// return eventFilter(watched,event);}if(watched == ui->listWidgetSourse){//判断是否是listWidgetSourse委托delete ui->listWidgetSourse->takeItem(ui->listWidgetSourse->currentRow());//删除当前行}else if(watched == ui->listWidget){delete ui->listWidget->takeItem(ui->listWidget->currentRow());//删除当前行}else if(watched == ui->treeWidget){QTreeWidgetItem * treeItem = ui->treeWidget->currentItem();//获取当前索引项。tree中需要判断这个tree到底有没有父项if(treeItem->parent() != nullptr){//判断父索引是否为空treeItem->parent()->removeChild(treeItem);}else{int index = ui->treeWidget->indexOfTopLevelItem(treeItem);ui->treeWidget->takeTopLevelItem(index);}delete treeItem;}else if(watched == ui->tableWidget){delete ui->tableWidget->takeItem(ui->tableWidget->currentRow(),ui->tableWidget->currentColumn());}return eventFilter(watched,event);//return true;
}void MainWindow::on_radioButton_clicked()
{m_itemView = ui->listWidgetSourse;refreshToUI(ui->groupBoxSourse);
}void MainWindow::on_radioButtonList_clicked()
{m_itemView = ui->listWidget;refreshToUI(ui->groupBoxList);
}void MainWindow::on_radioButtonTree_clicked()
{m_itemView = ui->treeWidget;refreshToUI(ui->groupBoxTree);
}void MainWindow::on_radioButtonTable_clicked()
{m_itemView = ui->tableWidget;refreshToUI(ui->groupBox);
}