小总结
void SwitchDemo::dragEnterEvent(QDragEnterEvent* event)
{
//判断拖拽文件类型,文件名 接收该动作
if (event->mimeData()->hasFormat("text/uri-list"))
{
event->acceptProposedAction();
}
}
void SwitchDemo::dropEvent(QDropEvent* event)
{
if (!event->mimeData()->hasUrls())
{
return;
}
emit userDropFile(event->mimeData()->urls()[0].toLocalFile());//发送userDropFile信号
}
void SwitchDemo::doOpenFile(const QString& path)
{
std::cout << (path.toLocal8Bit()).toStdString ()<< std::endl;
if (!path.isNull())
{
QFile file(path); //建立QFile对象,只读方式打开
if (!file.open(QIODevice::ReadOnly))
{
qDebug("打开文件失败!");
return;
}
QTextStream in(&file); //建立文本流对象,与QFile对象file连接
ui.textEdit->setText(in.readAll());
file.close();//关闭文本流
}
setWindowTitle("logPad - " + path);
}