QListWidget实现内部拖动排序功能
1.需求
将QListWidget有很多的任务,需要拖动任务,手动进行排序,并且需要保存拖动之后的顺序,软件重新打开要是修改之后的顺序;
(1)继承QListWidget,重写一个QListWidget自定义类
#ifndef DROPLISTWIDGET_H #define DROPLISTWIDGET_H #include <QListWidget> #include <QDropEvent> class DropListWidget : public QListWidget { Q_OBJECT public: DropListWidget(QWidget *parent); ~DropListWidget(); void dropEvent(QDropEvent *event); signals: void signalDropEvent(QListWidgetItem* pItem); private: }; #endif // DROPLISTWIDGET_H
#include "DropListWidget.h" DropListWidget::DropListWidget(QWidget *parent) { this->setParent(parent); } DropListWidget::~DropListWidget() { } void DropListWidget::dropEvent(QDropEvent *event) { QListWidget::dropEvent(event);//保留QListWidget拖拽功能 QListWidgetItem * pItem = currentItem(); if (pItem) { emit signalDropEvent(pItem);//发出信号,保存排序 } }
connect(ui.listWidget, &DropListWidget::signalDropEvent, this, &VideoCollsionTaskList::handleDropEvent);
(2)通过本地数据库保存拖动后的时间,拖到第一个,时间变为第二个时间减一天,拖到两个之间时,时间变为前后两个的平均值,拖到最后一个,则是将时间设置为当前时间,新插入的放在列表最后,时间是当前时间,这样就可以通过时间来排序,软件重新打开之后,按照时间正序去查询数据库,然后按照时间顺序添加到列表中。
void VideoCollsionTaskList::handleDropEvent(QListWidgetItem* pItem) { if (pItem==NULL) { return; } CollisionListItem * pCurrentTask =(CollisionListItem *) ui.listWidget->itemWidget(pItem); if (pCurrentTask==NULL) { return; } VideoCollisionTask cuTask=pCurrentTask->GetTaskInfo(); QString strCurrentTime = cuTask.createTime; int row=ui.listWidget->row(pItem); int rowCount = ui.listWidget->count(); if (rowCount==1)//只有一个不变 { return; } QListWidgetItem* pNext = NULL; QListWidgetItem* pPrevious = NULL; if (row==0)//拖到第一个 { pNext=ui.listWidget->item(1); if (pNext) { CollisionListItem * pNextTask=(CollisionListItem *) ui.listWidget->itemWidget(pNext); QString strNextTime = pNextTask->GetTaskInfo().createTime; strCurrentTime=QDateTime::fromString(strNextTime, "yyyyMMdd hh:mm:ss").addDays(-1).toString("yyyyMMdd hh:mm:ss");//减一天 } } else if (row==rowCount-1)//拖到最后一个 { pPrevious= ui.listWidget->item(row-1); if (pPrevious) { strCurrentTime = QDateTime::currentDateTime().toString("yyyyMMdd hh:mm:ss");//当前时间 } } else//中间 { QString strNextTime = ""; QString strPreviousTime = ""; pNext = ui.listWidget->item(row+1); if (pNext) { CollisionListItem * pNextTask = (CollisionListItem *)ui.listWidget->itemWidget(pNext); strNextTime = pNextTask->GetTaskInfo().createTime; } pPrevious = ui.listWidget->item(row - 1); if (pPrevious) { CollisionListItem * pPreviousTask = (CollisionListItem *)ui.listWidget->itemWidget(pPrevious); strPreviousTime = pPreviousTask->GetTaskInfo().createTime; } if (strNextTime!=""&&strPreviousTime!="") { quint64 time = (QDateTime::fromString(strNextTime, "yyyyMMdd hh:mm:ss").toSecsSinceEpoch()+ QDateTime::fromString(strPreviousTime, "yyyyMMdd hh:mm:ss").toSecsSinceEpoch())/2;//取均值 strCurrentTime = QDateTime::fromSecsSinceEpoch(time).toString("yyyyMMdd hh:mm:ss"); } } cuTask.createTime = strCurrentTime; pCurrentTask->SetTaskInfo(cuTask); QString strSql = QString("update CollisionTaskList set creatTime='%1' where taskName='%2'").arg(strCurrentTime).arg(cuTask.taskName); QString strMsg=""; if (LocalDb::instance()->ExcuateSql(strSql, strMsg) != 0) { LOG_ERROR("update CollisionTaskList task: %s time &s failed :%s", cuTask.taskName.toStdString().c_str(), strCurrentTime.toStdString().c_str(), strMsg.toStdString().c_str()); } }
自己开发了一个股票智能分析软件,功能很强大,需要的关注微信公众号:QStockView