gpt4 book ai didi

c++ - 将文件拖放到 QDialog 内的 QTreeWidget 中

转载 作者:行者123 更新时间:2023-11-30 03:28:29 37 4
gpt4 key购买 nike

我已经在网上搜索了好几天,但找不到任何可以帮助解决我的具体问题的东西。我正在尝试设置此对话框以接受要放入名为 filesTreeWidget 的 QTreeWidget 中的文件,但我一直在网上搜索的所有内容似乎都没有什么不同。我对 QT 和 C++ 也很陌生,所以我确信这没有帮助。感谢您的帮助

标题

class FileIQ : public QDialog
{
Q_OBJECT

protected:
void dropEvent(QDropEvent *event);
void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dragLeaveEvent(QDragLeaveEvent *event);
}

Cpp

 FileIQ::FileIQ(QWidget *parent, DR::EnginePtr engine)
: QDialog(parent)
, ui(new Ui::FileIQ)
, engine_(engine)
{
ui->filesTreeWidget->setAcceptDrops(true);
ui->filesTreeWidget->setDropIndicatorShown(true);
setAcceptDrops(true);
}

void FileIQ::dropEvent(QDropEvent *event)
{
foreach(const QUrl &url, event->mimeData()->urls()) {
QString filename = url.toLocalFile();
qDebug() << "Dropped file:" << filename;
QTreeWidgetItem *item = new QTreeWidgetItem(ui->filesTreeWidget);
item->setText(0, filename);
}

}


void FileIQ::dragEnterEvent(QDragEnterEvent *event)
{
event->accept();
}

void FileIQ::dragMoveEvent(QDragMoveEvent * event)
{
event->accept();
}

void FileIQ::dragLeaveEvent(QDragLeaveEvent * event)
{
event->accept();
}

最佳答案

首先,正确的做法是在 QTreeWidget 中实现拖放,而不是在 QDialog 中。为此,我们必须创建一个继承自 QTreeWidget 的类,并且我们必须实现以下 protected 方法:

bool QTreeWidget::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)

Handles the data supplied by a drag and drop operation that ended with the given action in the index in the given parent item.

The default implementation returns true if the drop was successfully handled by decoding the mime data and inserting it into the model; otherwise it returns false.

QStringList QTreeWidget::mimeTypes() const

Returns a list of MIME types that can be used to describe a list of treewidget items.

Qt::DropActions QTreeWidget::supportedDropActions() const

Returns the drop actions supported by this view.

从上面我们实现了这个类:

#ifndef TREEWIDGET_H
#define TREEWIDGET_H

#include <QDropEvent>
#include <QTreeWidget>
#include <QMimeData>
#include <QFileInfo>

class FilesTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
FilesTreeWidget(QWidget *parent= Q_NULLPTR):
QTreeWidget(parent)
{
setAcceptDrops(true);
setDropIndicatorShown(true);
setColumnCount(2);
}

protected:
bool dropMimeData(QTreeWidgetItem *parent, int /*index*/, const QMimeData *data, Qt::DropAction /*action*/)
{

for(const QUrl url: data->urls()) {
const QFileInfo info( url.toLocalFile());
if(info.isFile()){
QTreeWidgetItem *item;
if (parent){
item = new QTreeWidgetItem(parent);
parent->setExpanded(true);
}
else
item = new QTreeWidgetItem(this);
item->setText(0, info.fileName());
item->setText(1, info.filePath());
}
}
return true;
}

QStringList mimeTypes () const
{
return QStringList()<<"text/uri-list";
}

Qt::DropActions supportedDropActions () const
{
return Qt::CopyAction;
}

};

#endif // TREEWIDGET_H

完整的例子可以在下面的link中找到.如果您已经有 Qt Designer 分配的 QTreeWidget,最简单的解决方案是 promote the Qt Designer QTreeWidget使用新类。

输出:

enter image description here

关于c++ - 将文件拖放到 QDialog 内的 QTreeWidget 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46551444/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com