qt中用treewidget创建横向表格,实现两个treewidget能相互拖动item,并且只能拖动到到根节点的子节点同级节点
时间: 2024-02-02 17:05:09 浏览: 293
Qt模仿visionpro实现在树中显示算子输入输出关系显示,通过重新QTreewidget实现,拖动item算子输入输出关系
5星 · 资源好评率100%
您可以通过以下步骤实现此功能:
1. 创建两个QTreeWidget并设置其属性,其中一个作为源树,另一个作为目标树。
2. 设置两个QTreeWidget的拖放模式为DragDrop,并设置拖放行为为MoveAction,这样就可以实现拖放item。
3. 实现源树的拖放事件,以在拖动item时获取其数据并在目标树上创建新的item。
4. 为目标树的每个根节点创建一个drop indicator,以指示在哪里放置拖动的item。
5. 将每个根节点的drop indicator限制为只能接受来自源树的item,以确保拖动到同级节点的子节点。
下面是一个简单的示例代码,演示了如何实现此功能:
```
#include <QtWidgets>
class TreeWidget : public QWidget
{
public:
TreeWidget(QWidget* parent = nullptr) : QWidget(parent)
{
// Create source tree
m_sourceTree = new QTreeWidget(this);
m_sourceTree->setHeaderHidden(true);
m_sourceTree->setDragDropMode(QAbstractItemView::DragDrop);
m_sourceTree->setDropIndicatorShown(true);
m_sourceTree->setDefaultDropAction(Qt::MoveAction);
// Create target tree
m_targetTree = new QTreeWidget(this);
m_targetTree->setHeaderHidden(true);
m_targetTree->setDragDropMode(QAbstractItemView::DragDrop);
m_targetTree->setDropIndicatorShown(true);
m_targetTree->setDefaultDropAction(Qt::MoveAction);
// Add items to source tree
QTreeWidgetItem* sourceRoot = new QTreeWidgetItem(m_sourceTree);
sourceRoot->setText(0, "Root");
for (int i = 0; i < 5; ++i) {
QTreeWidgetItem* item = new QTreeWidgetItem(sourceRoot);
item->setText(0, QString("Item %1").arg(i));
}
// Add items to target tree
QTreeWidgetItem* targetRoot = new QTreeWidgetItem(m_targetTree);
targetRoot->setText(0, "Root");
// Set layout
QHBoxLayout* layout = new QHBoxLayout(this);
layout->addWidget(m_sourceTree);
layout->addWidget(m_targetTree);
// Connect signals and slots
connect(m_sourceTree, &QTreeWidget::itemMoved, this, &TreeWidget::onItemMoved);
connect(m_targetTree, &QTreeWidget::itemEntered, this, &TreeWidget::onItemEntered);
connect(m_targetTree, &QTreeWidget::itemExited, this, &TreeWidget::onItemExited);
}
private:
QTreeWidget* m_sourceTree;
QTreeWidget* m_targetTree;
QTreeWidgetItem* m_targetRoot;
QTreeWidgetItem* m_currentItem;
int m_currentIndex;
void onItemMoved(QTreeWidgetItem* item, QTreeWidgetItem* parent, int index)
{
if (parent == nullptr) {
// Item was removed from source tree
m_currentItem = item;
m_currentIndex = index;
}
else {
// Item was added to target tree
if (m_currentItem != nullptr) {
// Create new item in target tree
QTreeWidgetItem* newItem = new QTreeWidgetItem(parent, item);
newItem->setText(0, item->text(0));
// Remove item from source tree
QTreeWidgetItem* sourceParent = m_currentItem->parent();
if (sourceParent != nullptr) {
sourceParent->takeChild(m_currentIndex);
}
else {
m_sourceTree->takeTopLevelItem(m_currentIndex);
}
m_currentItem = nullptr;
}
}
}
void onItemEntered(QTreeWidgetItem* item, int column)
{
if (item->childCount() == 0) {
// Create drop indicator for root node
m_targetRoot = item;
QRect rect = m_targetTree->visualItemRect(item);
rect.setLeft(0);
rect.setWidth(m_targetTree->viewport()->width());
m_targetTree->setDropIndicatorShown(true);
m_targetTree->setDropIndicatorRect(rect);
m_targetTree->setAcceptDrops(true);
}
}
void onItemExited(QTreeWidgetItem* item, int column)
{
if (item == m_targetRoot) {
// Remove drop indicator for root node
m_targetTree->setDropIndicatorShown(false);
m_targetTree->setAcceptDrops(false);
m_targetRoot = nullptr;
}
}
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
TreeWidget treeWidget;
treeWidget.show();
return app.exec();
}
```
在此示例中,我们创建了两个QTreeWidget,设置了它们的拖放模式和拖放行为,并将它们添加到一个水平布局中。源树添加了一些item,而目标树只添加了一个根节点。我们还连接了一些信号和槽函数,以便在拖放item时获取其数据并在目标树上创建新item。为了限制drop indicator的作用范围,我们使用了onItemEntered和onItemExited函数来确定目标树上的根节点,并将其限制为只能接受来自源树的item。
您可以根据需要修改此示例代码以满足您的具体需求。
阅读全文