Qt工作笔记-在QTreeView上实现模型数据的拖拽
生活随笔
收集整理的這篇文章主要介紹了
Qt工作笔记-在QTreeView上实现模型数据的拖拽
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
運行截圖如下:
邏輯是:
1.QTreeView開發拖拽功能。
2.在自定義模型中重寫
flags(const QModelIndex &index) const
mimeData(const QModelIndexList &indexes) const
實現拖的功能
3.在自定義模型中重寫
dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
mimeTypes() const
實現放的功能
代碼如下:
mymodel.h
#ifndef MYMODEL_H #define MYMODEL_H#include <QObject> #include <QStandardItemModel>class MyModel : public QStandardItemModel {Q_OBJECT public:explicit MyModel(QStandardItemModel *parent = 0);Qt::ItemFlags flags(const QModelIndex &index) const;QMimeData *mimeData(const QModelIndexList &indexes) const;bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent);QStringList mimeTypes() const;signals:public slots:private:int m_rowCount; };#endif // MYMODEL_Hwidget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget> class MyModel;namespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private:Ui::Widget *ui;MyModel *m_model1;MyModel *m_model2; };#endif // WIDGET_Hmain.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }mymodel.cpp
#include "mymodel.h" #include <QDebug> #include <QMimeData> #include <QModelIndexList>MyModel::MyModel(QStandardItemModel *parent): QStandardItemModel(parent) {m_rowCount=0; }Qt::ItemFlags MyModel::flags(const QModelIndex &index) const {Qt::ItemFlags flags = QAbstractItemModel::flags(index);flags = flags | Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled;return flags; }QMimeData *MyModel::mimeData(const QModelIndexList &indexes) const {if (indexes.count() <= 0)return 0;QMimeData *data = new QMimeData;data->setData("drag","鼠標拖拽");data->setData("name",item(indexes.at(0).row(),0)->text().toLocal8Bit());data->setData("score",item(indexes.at(0).row(),1)->text().toLocal8Bit());return data; }bool MyModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {QString st=QString::fromLocal8Bit(data->data("name"));QString st1=QString::fromLocal8Bit(data->data("score"));this->setItem(m_rowCount,0,new QStandardItem(st));this->setItem(m_rowCount,1,new QStandardItem(st1));m_rowCount++; }QStringList MyModel::mimeTypes() const {QStringList types;types << "drag";return types; }widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QDebug> #include "mymodel.h"Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);this->setWindowTitle("CSDN IT1995");m_model1=new MyModel;m_model1->setColumnCount(2);m_model1->setRowCount(4);m_model1->setItem(0,0,new QStandardItem("球球"));m_model1->setItem(0,1,new QStandardItem("99"));m_model1->setItem(1,0,new QStandardItem("豬小明"));m_model1->setItem(1,1,new QStandardItem("59"));m_model1->setItem(2,0,new QStandardItem("鍋蓋"));m_model1->setItem(2,1,new QStandardItem("111"));m_model1->setItem(3,0,new QStandardItem("閏土"));m_model1->setItem(3,1,new QStandardItem("999"));m_model1->setHorizontalHeaderItem(0,new QStandardItem("姓名"));m_model1->setHorizontalHeaderItem(1,new QStandardItem("成績"));ui->treeView->setModel(m_model1);ui->treeView->setDragDropMode(QAbstractItemView::DragDrop);m_model2=new MyModel;m_model2->setColumnCount(2);m_model2->setHorizontalHeaderItem(0,new QStandardItem("姓名"));m_model2->setHorizontalHeaderItem(1,new QStandardItem("成績"));ui->treeView_2->setModel(m_model2);ui->treeView_2->setDragDropMode(QAbstractItemView::DropOnly);}Widget::~Widget() {delete ui; }總結
以上是生活随笔為你收集整理的Qt工作笔记-在QTreeView上实现模型数据的拖拽的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux工作笔记-查看tcp与udp端
- 下一篇: Python学习笔记-猜数字游戏(学习语