重载以下两个函数,可以实现将文本文件拖放进文本编辑器
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