zoukankan      html  css  js  c++  java
  • QT 文件拖放事件dropEvent和dragEnterEvent

    重载以下两个函数,可以实现将文本文件拖放进文本编辑器

    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;
        }
        foreach(QUrl url,urls)
        {
           QString fileName = url.toLocalFile();
    
    
    //    QString fileName = urls.first().toLocalFile();
            if (fileName.isEmpty())
            {
                return;
            }
            QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
            bool hasOpened = false;//是否已经打开该文件
            foreach (QMdiSubWindow *window, mdiArea->subWindowList())
            {
                MyTextEdit *subChild = qobject_cast<MyTextEdit *>(window->widget());
                if( subChild->curFilePath == canonicalFilePath)
                {
                    hasOpened = true;
                    mdiArea->setActiveSubWindow(window);
                    continue;
                }
            }
            if(!hasOpened)
            {
                MyTextEdit *m_textEdit = new MyTextEdit;
                connect(m_textEdit,SIGNAL(isClose()),this,SLOT(updateStatue()));
                mdiArea->addSubWindow(m_textEdit);
                if(m_textEdit->do_file_Load(fileName))
                {
                   m_textEdit->setVisible(true);
                   second_statusLabel->setText(tr("打开成功"));
                }
                else
                    m_textEdit->close();
    
    
                QSettings settings("QT","AnTextEdit");
                //当我们创建一个Qsettings的对象时,我们需要传递给它两个参数,
                //第一个是你公司或者组织的名称,第二个事你的应用程序的名称
                QStringList files = settings.value("recentFiles").toStringList();
                files.removeAll(fileName);
                files.prepend(fileName);//在最前端插入
                while(files.size() > MaxRecentFiles)
                    files.removeLast();
                settings.setValue("recentFiles",files);
                updateRecentFiles();
                iniConnect();//初始化关联
            }
        }
    }

    http://blog.csdn.net/liuguangzhou123/article/details/7362083

  • 相关阅读:
    poj3083(Children of the Candy Corn)
    poj3278(Catch That Cow)
    poj2996(Help Me with the Game)
    poj2993(Emag eht htiw Em Pleh)
    js 对多sheet Excel赋值操作
    学习进度总结(三)
    学习进度总结(二)
    学习进度总结(一)
    《人月神话》阅读笔记(1)
    Android studio的安装与使用
  • 原文地址:https://www.cnblogs.com/findumars/p/5636482.html
Copyright © 2011-2022 走看看