Qt 树视图中自定义数据的Mime类型
树状视图中的项包含类容器的实例。 我想在视图中实现拖放功能 根据要复制的数据的QT教程,我需要指定mime类型,然后编写Mimedata和dropMimeData函数 QT示例处理的是一个简单的字符串,因此我完全不知道如何在自定义对象的情况下实现这些函数 1) 在我的案例中,mime类型应该是什么 2) 如何为容器对象数据实现当前的mimedata函数Qt 树视图中自定义数据的Mime类型,qt,qt5,Qt,Qt5,树状视图中的项包含类容器的实例。 我想在视图中实现拖放功能 根据要复制的数据的QT教程,我需要指定mime类型,然后编写Mimedata和dropMimeData函数 QT示例处理的是一个简单的字符串,因此我完全不知道如何在自定义对象的情况下实现这些函数 1) 在我的案例中,mime类型应该是什么 2) 如何为容器对象数据实现当前的mimedata函数 ///////////////////////////////////////// class Container { priv
/////////////////////////////////////////
class Container
{
private:
std::string stdstrContainerName;
std::string stdstrPluginType;
int iSegments;
float fRadius;
public:
Container();
Container(std::string , std::string , int , float);
Container(const Container& obj);
~Container();
std::string GetName();
std::string GetType();
void SetName(std::string stdstrName);
};
Q_DECLARE_METATYPE( Container )
////////////////////////////////////////////////////////////
QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes)
const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (const QModelIndex &index, indexes) {
if (index.isValid()) {
QString text = data(index, Qt::DisplayRole).toString();
// I have a GetContainer function which returns the Container
//object and i can use the GetContainer instead of data function.
stream << text;
}
}
mimeData->setData("application/vnd.text.list", encodedData);
return mimeData;
}
//////////////////////////////////////////////////////////////////
bool DragDropListModel::dropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex
&parent)
{
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("application/vnd.text.list"))
return false;
if (column > 0)
return false;
int beginRow;
if (row != -1)
beginRow = row;
else if (parent.isValid())
beginRow = parent.row();
else
beginRow = rowCount(QModelIndex());
QByteArray encodedData = data->data("application/vnd.text.list");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QStringList newItems;
int rows = 0;
while (!stream.atEnd()) {
QString text;
stream >> text;
newItems << text;
++rows;
}
insertRows(beginRow, rows, QModelIndex());
foreach (const QString &text, newItems) {
QModelIndex idx = index(beginRow, 0, QModelIndex());
setData(idx, text);
beginRow++;
}
return true;
}
3) 如何为容器对象数据实现当前的dropmimedata函数
/////////////////////////////////////////
class Container
{
private:
std::string stdstrContainerName;
std::string stdstrPluginType;
int iSegments;
float fRadius;
public:
Container();
Container(std::string , std::string , int , float);
Container(const Container& obj);
~Container();
std::string GetName();
std::string GetType();
void SetName(std::string stdstrName);
};
Q_DECLARE_METATYPE( Container )
////////////////////////////////////////////////////////////
QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes)
const
{
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (const QModelIndex &index, indexes) {
if (index.isValid()) {
QString text = data(index, Qt::DisplayRole).toString();
// I have a GetContainer function which returns the Container
//object and i can use the GetContainer instead of data function.
stream << text;
}
}
mimeData->setData("application/vnd.text.list", encodedData);
return mimeData;
}
//////////////////////////////////////////////////////////////////
bool DragDropListModel::dropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex
&parent)
{
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("application/vnd.text.list"))
return false;
if (column > 0)
return false;
int beginRow;
if (row != -1)
beginRow = row;
else if (parent.isValid())
beginRow = parent.row();
else
beginRow = rowCount(QModelIndex());
QByteArray encodedData = data->data("application/vnd.text.list");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
QStringList newItems;
int rows = 0;
while (!stream.atEnd()) {
QString text;
stream >> text;
newItems << text;
++rows;
}
insertRows(beginRow, rows, QModelIndex());
foreach (const QString &text, newItems) {
QModelIndex idx = index(beginRow, 0, QModelIndex());
setData(idx, text);
beginRow++;
}
return true;
}
/////////////////////////////////////////
类容器
{
私人:
std::字符串stdstrContainerName;
std::字符串stdstrPluginType;
国际分段;
浮雕;
公众:
容器();
容器(std::string、std::string、int、float);
集装箱(集装箱和obj);
~Container();
std::string GetName();
std::string GetType();
void SetName(std::string stdstrName);
};
Q_DECLARE_元类型(容器)
////////////////////////////////////////////////////////////
QMimeData*DragDropListModel::mimeData(常量QModelIndexList和索引)
常数
{
QMimeData*mimeData=新QMimeData();
QByteArray编码数据;
QDataStream流(&encodedData,QIODevice::WriteOnly);
foreach(常量QModelIndex和索引,索引){
if(index.isValid()){
QString text=data(索引,Qt::DisplayRole).toString();
//我有一个GetContainer函数,它返回容器
//对象,我可以使用GetContainer而不是data函数。
流setData(“application/vnd.text.list”,encodedData);
返回mimeData;
}
//////////////////////////////////////////////////////////////////
bool DragDropListModel::dropMimeData(常量QMimeData*数据,
Qt::DropAction操作,int行,int列,常量QModelIndex
&(家长)
{
if(action==Qt::IgnoreAction)
返回true;
如果(!data->hasFormat(“application/vnd.text.list”))
返回false;
如果(列>0)
返回false;
int beginRow;
如果(行!=-1)
beginRow=行;
else if(parent.isValid())
beginRow=parent.row();
其他的
beginRow=rowCount(QModelIndex());
QByteArray encodedData=data->data(“application/vnd.text.list”);
QDataStream(encodedData和QIODevice::ReadOnly);
QStringList新项目;
int行=0;
而(!stream.atEnd()){
QString文本;
流>>文本;
newItems您可以添加自定义mime类型以指定要拖放的容器类型。有关详细信息,请参阅此
源构造的QDrag对象包含一个MIME类型列表,它用来表示数据(从最合适到最不合适的顺序排列),drop目标使用其中一个来访问数据
首先,尝试找到一个兼容的标准mime类型。这些类型是由指定的最常见的类型
如果您要查找的数据不在列表中,则可以标记自定义数据,并将数据序列化为QByteArray
以共享数据
QByteArray output;
// do whatever
mimeData->setData("my-awesome-mime-type", output);
现在,在自定义小部件中,不要忘记接受此mime类型的删除:
void Window::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasFormat("my-awesome-mime-type"))
event->acceptProposedAction();
}
您可以在此中找到完整的示例。您可以添加自定义mime类型以指定要拖放的容器类型。有关详细信息,请参阅此
源构造的QDrag对象包含一个MIME类型列表,它用来表示数据(从最合适到最不合适的顺序排列),drop目标使用其中一个来访问数据
首先,尝试找到一个兼容的标准mime类型。这些类型是由指定的最常见的类型
如果您要查找的数据不在列表中,则可以标记自定义数据,并将数据序列化为QByteArray
以共享数据
QByteArray output;
// do whatever
mimeData->setData("my-awesome-mime-type", output);
现在,在自定义小部件中,不要忘记接受此mime类型的删除:
void Window::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasFormat("my-awesome-mime-type"))
event->acceptProposedAction();
}
你可以在这里找到一个完整的例子