paip.提升用户体验------c++ qt 拖曳使用总结..
作者Attilax
1.头文件添加dragEnterEvent,dropEvent 俩个事件
------------------
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
2. 实现CPP
-------------
原理:
setAcceptDrops(true); // [[1]]: 使label可接受拖放操作
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
-------------------cpp--------------------
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setAcceptDrops(true);
//setAcceptDrops(true); // [[1]]: 使label可接受拖放操作
}
//ati c920
#include <QDragEnterEvent>
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/uri-list"))
event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty())
return;
QString fileName = urls.first().toLocalFile();
// if (fileName.isEmpty())
// return;
ui->lineEdit->setText(fileName);
// QMessageBox::about(NULL, "About", fileName);
// traveDir(fileName);
}
//c920 end