QT implementation drag-and-drop files (with examples, and illustrated, very clear)

Source: Internet
Author: User

Transferred from: http://my.oschina.net/voler/blog/345722

Catalogue [-]

    • 0. Source code
    • 1. Simple File drag and drop
    • 2. Drag and drop complex files
    • 3. Use the button to complete the transfer of the list data
    • 4. Achieve results by dragging and dropping files
    • 1. Simple File drag and drop
    • 1. In general, the edit box can receive the file name directly
    • 2. Procedures
    • 2. Drag and drop complex files
    • 1. The key code is as follows
    • 2. Procedures
    • 3. Use the button to complete the transfer of the list data
    • 1. Key code
    • 2. Procedures
    • 4. Achieve results by dragging and dropping files
    • 1. Key code
    • 2. (Drag and drop by mouse)
0. source code 1. Simple File drag and drop

Https://github.com/leichaojian/qt/tree/master/drop1

2. Drag and drop complex files

Https://github.com/leichaojian/qt/tree/master/drop

3. Use the button to complete the transfer of the list data

Https://github.com/leichaojian/qt/tree/master/drop3

4. Achieve results by dragging and dropping files

Https://github.com/leichaojian/qt/tree/master/drop4

1. Drag and drop simple files 1. In general, the edit box can receive the file name directly

?
12345678 mainwindow::mainwindow ( Qwidget *parent):      Qmainwindow (parent),      ui ( new  ui::mainwindow) {      ui-> Setupui ( this      textedit =  new  qtextedit;      setcentralwidget (textEdit);

2. Procedures

2. Drag and drop complex files

If we want to get the file information of the response in the main frame, we must implement the file drag-and-drop operation ourselves.

1. The key code is as follows

?
1234567891011121314151617181920212223 //当用户拖动文件到窗口部件上时候,就会触发dragEnterEvent事件voidMainWindow::dragEnterEvent(QDragEnterEvent *event){    //如果为文件,则支持拖放    if(event->mimeData()->hasFormat("text/uri-list"))        event->acceptProposedAction();}//当用户放下这个文件后,就会触发dropEvent事件voidMainWindow::dropEvent(QDropEvent *event){    //注意:这里如果有多文件存在,意思是用户一下子拖动了多个文件,而不是拖动一个目录    //如果想读取整个目录,则在不同的操作平台下,自己编写函数实现读取整个目录文件名    QList<QUrl> urls = event->mimeData()->urls();    if(urls.isEmpty())        return;    //往文本框中追加文件名    foreach(QUrl url, urls) {        QString file_name = url.toLocalFile();        textEdit->append(file_name);    }}

2. Procedures

3. Use the button to complete the transfer of the list data 1. Key code

?
12345678910 voidDialog::moveCurrentItem(QListWidget *source,                     QListWidget *target){    if(source->currentItem()) {        QListWidgetItem *newItem = source->currentItem()->clone();        target->addItem(newItem);        target->setCurrentItem(newItem);        deletesource->currentItem();    }}

2. Procedures

4. Drag and drop the file to achieve the effect 1. Key code

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970 voidProjectListWidget::mousePressEvent(QMouseEvent *event){    //获取鼠标按下时候的坐标    if(event->button() == Qt::LeftButton)        startPos = event->pos();    QListWidget::mousePressEvent(event);}voidProjectListWidget::mouseMoveEvent(QMouseEvent *event){    if(event->buttons() & Qt::LeftButton) {        intdistance = (event->pos() - startPos).manhattanLength();        //如果长度大于推荐的拖动起始距离,则认为是拖动(存在用户手抖的情况)        if(distance >= QApplication::startDragDistance())            performDrag();    }    QListWidget::mouseMoveEvent(event);}//有文件拖动到窗口上时,触发此dragEnterEvent事件voidProjectListWidget::dragEnterEvent(QDragEnterEvent *event){    //当为同一个应用程序的一部分时,event->source()返回启动这个拖动窗口部件的指针    ProjectListWidget *source =            qobject_cast<ProjectListWidget *>(event->source());    if(source && source != this) {        //确认是一个移动的操作        event->setDropAction(Qt::MoveAction);        event->accept();    }}voidProjectListWidget::dragMoveEvent(QDragMoveEvent *event){    ProjectListWidget *source =            qobject_cast<ProjectListWidget *>(event->source());    if (source && source != this) {        event->setDropAction(Qt::MoveAction);        event->accept();    }}voidProjectListWidget::dropEvent(QDropEvent *event){    ProjectListWidget *source =            qobject_cast<ProjectListWidget *>(event->source());    if(source && source != this) {        //得到数据并增加到列表中        addItem(event->mimeData()->text());        event->setDropAction(Qt::MoveAction);        event->accept();    }}voidProjectListWidget::performDrag(){    //当前被拖动的选项    QListWidgetItem *item = currentItem();    if(item) {        QMimeData *mimeData = newQMimeData;        mimeData->setText(item->text());        QDrag *drag = newQDrag(this);        drag->setMimeData(mimeData);        drag->setPixmap(QPixmap(":/images/person.png"));        //进行拖动操作        if(drag->exec(Qt::MoveAction) == Qt::MoveAction)            deleteitem;    }}

2. (Drag and drop by mouse)

http://blog.csdn.net/emdfans/article/details/41721955

QT implementation drag-and-drop files (with examples, and illustrated, very clear)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.