用qt c++写一个带ui界面继承qwidget的类,类名为TreeWidgetItemDrag,ui界面有两个treewidget,treewidget为横向的表格,实现两个表格能互相拖动qtreewidgetitem到对方表格中,同时移除自己表格中拖动的treewidgetitem
时间: 2024-01-21 07:17:45 浏览: 123
基于Qt和C++实现的FlatUI辅助类之各种炫酷的控件集合+源码
好的,以下是一个简单的实现:
TreeWidgetItemDrag.h
```cpp
#ifndef TREEWIDGETITEMDRAG_H
#define TREEWIDGETITEMDRAG_H
#include <QWidget>
#include <QTreeWidget>
namespace Ui {
class TreeWidgetItemDrag;
}
class TreeWidgetItemDrag : public QWidget
{
Q_OBJECT
public:
explicit TreeWidgetItemDrag(QWidget *parent = nullptr);
~TreeWidgetItemDrag();
private:
Ui::TreeWidgetItemDrag *ui;
void setupTreeWidget(QTreeWidget *treeWidget);
private slots:
void on_treeWidget_1_itemDragged(QTreeWidgetItem *item);
void on_treeWidget_2_itemDragged(QTreeWidgetItem *item);
};
#endif // TREEWIDGETITEMDRAG_H
```
TreeWidgetItemDrag.cpp
```cpp
#include "TreeWidgetItemDrag.h"
#include "ui_Treewidgetitemdrag.h"
#include <QDebug>
TreeWidgetItemDrag::TreeWidgetItemDrag(QWidget *parent) :
QWidget(parent),
ui(new Ui::TreeWidgetItemDrag)
{
ui->setupUi(this);
setupTreeWidget(ui->treeWidget_1);
setupTreeWidget(ui->treeWidget_2);
}
TreeWidgetItemDrag::~TreeWidgetItemDrag()
{
delete ui;
}
void TreeWidgetItemDrag::setupTreeWidget(QTreeWidget *treeWidget)
{
// 设置treewidget为横向的表格
treeWidget->setColumnCount(3);
treeWidget->setHeaderLabels(QStringList() << "Column 1" << "Column 2" << "Column 3");
// 允许treewidget的items拖动
treeWidget->setDragEnabled(true);
// 设置treewidget接受拖放事件
treeWidget->setAcceptDrops(true);
// 设置treewidget在拖动item时,显示的内容
treeWidget->setDragDropMode(QAbstractItemView::DragDrop);
// 设置treewidget在释放item时,移动item
treeWidget->setDefaultDropAction(Qt::MoveAction);
// 设置treewidget的items不能被编辑
treeWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
// 允许treewidget的items在内部移动
treeWidget->setMovement(QTreeWidget::InternalMove);
}
void TreeWidgetItemDrag::on_treeWidget_1_itemDragged(QTreeWidgetItem *item)
{
ui->treeWidget_1->takeTopLevelItem(ui->treeWidget_1->indexOfTopLevelItem(item));
ui->treeWidget_2->addTopLevelItem(item);
}
void TreeWidgetItemDrag::on_treeWidget_2_itemDragged(QTreeWidgetItem *item)
{
ui->treeWidget_2->takeTopLevelItem(ui->treeWidget_2->indexOfTopLevelItem(item));
ui->treeWidget_1->addTopLevelItem(item);
}
```
其中ui界面可以使用Qt Designer进行设计,也可以手写代码。这里使用Qt Designer设计的ui文件为:
TreeWidgetItemDrag.ui
```xml
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TreeWidgetItemDrag</class>
<widget class="QWidget" name="TreeWidgetItemDrag">
<property name="windowTitle">
<string>Tree Widget Item Drag</string>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Drag and drop items between two tree widgets:</string>
</property>
</widget>
</item>
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QTreeWidget" name="treeWidget_1">
<property name="maximumWidth">
<number>16777215</number>
</property>
</widget>
<widget class="QTreeWidget" name="treeWidget_2">
<property name="maximumWidth">
<number>16777215</number>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
```
在主窗口中,只需要实例化TreeWidgetItemDrag类并将其添加到主窗口中即可:
MainWindow.cpp
```cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "TreeWidgetItemDrag.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 实例化TreeWidgetItemDrag类并将其添加到主窗口中
ui->centralWidget->layout()->addWidget(new TreeWidgetItemDrag(this));
}
MainWindow::~MainWindow()
{
delete ui;
}
```
这样,就可以实现两个treewidget之间的拖动了。
阅读全文