Qt重写QTreeWidget实现拖拽

介绍

此文章记录QTreeWidget的重写进度,暂时停滞使用,重写了QTreeWidget的拖拽功能,和绘制功能,自定义了数据结构,增加复制,粘贴,删除,准备实现动态刷新数据支持千万数据动态刷新,重写了部分代码,重写了滑块拖拽但是有bug。

效果展示

在这里插入图片描述
在这里插入图片描述

实现功能

  1. 实现了自定义节点类来存储数据。
  2. item采用Label来实现富文本显示(数据量大不建议使用)。
  3. 重写了QTreeWidget拖拽,滚动,绘制。
  4. 拖拽实现了节点与节点之间的互相拖拽,仅移动。
  5. 实现了节点的递归遍历,查找,插入,删除。
  6. 实现了滚动刷新数据,大量数据也不卡,但是滚动条刷新数据稍微复杂未完成,且有bug。

代码

#ifndef MYTREEWIDGET_H
#define MYTREEWIDGET_H#include <QWidget>
#include <QtWidgets>
#include <QDebug>class MyTreeWidgetItem;
class TreeNode;
class MyLabel;// 自定义节点类
class TreeNode {
public:QString nodeText;QString nodeItemTest;QList<TreeNode*> children;TreeNode* parent = nullptr;MyTreeWidgetItem* item;// 重载==运算符以判断nodeText是否相等bool operator==(const TreeNode& other) const {return nodeText == other.nodeText;}
};class MyTreeWidgetItem :public QTreeWidgetItem
{
public:MyTreeWidgetItem();void setNodeText(QString str);QString getNodeText();QString m_Text;MyLabel* m_Label;int indexOfChild(QTreeWidgetItem *child){int childCount = this->childCount();for (int i = 0; i < childCount; ++i) {if (this->child(i) == child) {return i;}}return -1;}
};class MyLabel : public QLabel
{Q_OBJECT
public:MyLabel(QString text, QWidget *parent = nullptr);bool m_IsSelect = false;bool m_IsLeave = true;QString enterStyle;QString leaveStyle;QString selectStyle;
protected:bool eventFilter(QObject *watched, QEvent *event);
};class MyTreeWidget : public QTreeWidget
{Q_OBJECT
public:MyTreeWidget(QWidget *parent = nullptr);QList<TreeNode*> m_ListData;void wait(int ms); //等待// 删除具有特定文本的节点及其子节点bool deleteNodeByText(const QString& targetText) {for (int i = 0; i < m_ListData.size(); ++i) {TreeNode* node = m_ListData.at(i);if (node->nodeText == targetText) {// 找到目标节点,删除其子节点qDeleteAll(node->children);delete node;m_ListData.removeAt(i);return true; // 成功删除节点}}return false; // 未找到匹配的节点}//删除指定父节点下的子节点bool deleteChildNode(TreeNode* node, const QString& nodeText) {for (int i = 0; i < node->children.size(); ++i) {if (node->children.at(i)->nodeText == nodeText) {node->children.removeAt(i);return true;}}return false;}//根据nodeText递归查找该对象TreeNode* recursiveFindNodeByText(const QString& targetText, TreeNode* currentNode) {if (currentNode->nodeText == targetText) {return currentNode;}for (TreeNode* child : currentNode->children) {TreeNode* result = recursiveFindNodeByText(targetText, child);if (result) {return result;}}return nullptr; // 未找到匹配的节点}//根据nodeText递归查找该对象TreeNode* findNodeByText(const QString& targetText, const QList<TreeNode*>& nodeList) {for (TreeNode* node : nodeList) {TreeNode* result = recursiveFindNodeByText(targetText, node);if (result) {return result;}}return nullptr; // 未找到匹配的节点}//插入字节点 包括数据更新TreeNode* insertChileItem(TreeNode* parentNode, int row, QString itemText);//追加子节点TreeNode* appendChileItem(TreeNode* parentNode, QString itemText);//插入顶部节点TreeNode* insertTopItem(int row, QString itemText, bool isInster = true);//void updateItem(int row, TreeNode* node);//追加顶部节点TreeNode* appendTopItem(QString itemText);//删除顶部的item 以及数据列表void removeTopItem(TreeNode* node);//一个更新nodetext的变量保证每次更新节点都会变化int number = 0;double m_NodeSize = 0;double m_ScrollBarValue = 0;bool m_BarValueChangedTemp = false;bool m_WheelEventTemp = false;//递归插入void RecursionInsert(TreeNode* ParentNode, TreeNode* childNode);//展开所有子节点void expandAllItems(QTreeWidget* treeWidget, QTreeWidgetItem* item);void onScrollBarValueChanged(int value);
protected:void mouseMoveEvent(QMouseEvent *event);void mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void startDrag(Qt::DropActions supportedActions);void dragEnterEvent(QDragEnterEvent *event);void dragMoveEvent(QDragMoveEvent *event);void dropEvent(QDropEvent *event);void paintEvent(QPaintEvent* event);void wheelEvent(QWheelEvent *event);
private:TreeNode* m_CopyNode = nullptr; //当前拷贝的节点QPoint m_DragMovePos; //拖拽移动坐标QPoint m_CurPos; //点击保存坐标int m_LastScrollBarValue = 0;QMenu* m;MyTreeWidgetItem* m_CurrDragItem = nullptr; //保存当前正在拖拽的对象QScrollBar *m_VerticalScrollBar;
};#endif // MYTREEWIDGET_H
/******************************************************************************** Copyright [2023] <键鼠自动化>* All rights reserved.** version:     1.0* author:      lx* brief:       自定义item,重写绘制和拖拽,以及建立数据结构
*******************************************************************************/
#include "mytreewidget.h"MyTreeWidget::MyTreeWidget(QWidget *parent): QTreeWidget(parent)
{setDragEnabled(true);setAcceptDrops(true);setDragDropMode(DragDropMode::InternalMove);setHeaderHidden(true);m_VerticalScrollBar = this->verticalScrollBar();connect(m_VerticalScrollBar, &QScrollBar::valueChanged, this, &MyTreeWidget::onScrollBarValueChanged);m = new QMenu(this);QAction* action1 = new QAction("删除");QAction* action2 = new QAction("复制");QAction* action3 = new QAction("粘贴");connect(action1, &QAction::triggered, [=]() {MyTreeWidgetItem* targetItem = dynamic_cast<MyTreeWidgetItem*>(itemAt(m_CurPos));if (targetItem) {TreeNode* targetNode = findNodeByText(targetItem->getNodeText(), m_ListData);if (targetNode->parent == nullptr) {this->removeTopItem(targetNode);}else {targetNode->parent->item->removeChild(targetNode->item);deleteChildNode(targetNode->parent, targetNode->nodeText);}}qDebug() << "1231231";});connect(action2, &QAction::triggered, [=](){MyTreeWidgetItem* targetItem = dynamic_cast<MyTreeWidgetItem*>(itemAt(m_CurPos));if (targetItem) {m_CopyNode = findNodeByText(targetItem->getNodeText(), m_ListData);}});connect(action3, &QAction::triggered, [=]() {MyTreeWidgetItem* targetItem = dynamic_cast<MyTreeWidgetItem*>(itemAt(m_CurPos));if (targetItem && m_CopyNode != nullptr) {TreeNode* targetNode = findNodeByText(targetItem->getNodeText(), m_ListData);if (targetNode->parent == nullptr) {int targetRow = this->indexOfTopLevelItem(targetNode->item);if (m_CopyNode->children.isEmpty()) {this->insertTopItem(targetRow + 1, m_CopyNode->nodeItemTest);}else {TreeNode* newParentNode = this->insertTopItem(targetRow + 1, m_CopyNode->nodeItemTest);for (int i = 0 ; i < m_CopyNode->children.size(); ++i) {this->RecursionInsert(newParentNode, m_CopyNode->children.at(i));}}}else {int targetRow = targetNode->parent->item->indexOfChild(targetNode->item);if (m_CopyNode->children.isEmpty()) {this->insertChileItem(targetNode->parent, targetRow + 1, m_CopyNode->nodeItemTest);}else {TreeNode* newParentNode = this->insertChileItem(targetNode->parent, targetRow + 1, m_CopyNode->nodeItemTest);for (int i = 0 ; i < m_CopyNode->children.size(); ++i) {this->RecursionInsert(newParentNode, m_CopyNode->children.at(i));}}}m_CopyNode = nullptr;}});m->addAction(action1);m->addAction(action2);m->addAction(action3);
}void MyTreeWidget::wait(int ms)
{QElapsedTimer t;t.start();while (t.elapsed() < ms)QCoreApplication::processEvents();
}TreeNode *MyTreeWidget::insertChileItem(TreeNode *parentNode, int row, QString itemText)
{number++;QTreeWidgetItem* parentItem = parentNode->item; //获取父节点MyTreeWidgetItem* childItem = new MyTreeWidgetItem(); //创建子节点childItem->setNodeText(QString("child%1").arg(number));MyLabel* label = new MyLabel(itemText); //创建labelchildItem->m_Label = label;TreeNode* childData = new TreeNode; //创建子节点对象数据childData->parent = parentNode;childData->item = childItem;childData->nodeText = QString("child%1").arg(number);childData->nodeItemTest = itemText;parentNode->children.insert(row, childData); //父节点对象数据插入子节点parentItem->insertChild(row, childItem); //父节点item插入子节点itemsetItemWidget(childItem, 0, label); //item转换lablereturn childData;
}TreeNode *MyTreeWidget::appendChileItem(TreeNode *parentNode, QString itemText)
{number++;QTreeWidgetItem* parentItem = parentNode->item;MyTreeWidgetItem* childItem = new MyTreeWidgetItem();childItem->setNodeText(QString("child%1").arg(number));MyLabel* label = new MyLabel(itemText);childItem->m_Label = label;TreeNode* childData = new TreeNode;childData->parent = parentNode;childData->item = childItem;childData->nodeText = QString("child%1").arg(number);childData->nodeItemTest = itemText;parentNode->children.append(childData);parentItem->addChild(childItem);setItemWidget(childItem, 0, label);return childData;
}TreeNode* MyTreeWidget::insertTopItem(int row, QString itemText, bool isInster)
{number++;MyTreeWidgetItem* parentItem = new MyTreeWidgetItem();parentItem->setNodeText(QString("parent%1").arg(number));TreeNode* parentData = new TreeNode;parentData->item = parentItem;parentData->nodeText = QString("parent%1").arg(number);parentData->nodeItemTest = itemText;if (isInster) {MyLabel* label = new MyLabel(itemText);parentItem->m_Label = label;insertTopLevelItem(row, parentItem);setItemWidget(parentItem, 0, label);}m_ListData.insert(row, parentData);return parentData;
}void MyTreeWidget::updateItem(int row, TreeNode *node)
{qDebug() << row << node->nodeItemTest << "aaaa";MyLabel* label = new MyLabel(node->nodeItemTest);node->item->m_Label = label;insertTopLevelItem(row, node->item);
//    setItemWidget(node->item, 0, label);
}TreeNode *MyTreeWidget::appendTopItem(QString itemText)
{number++;MyTreeWidgetItem* parentItem = new MyTreeWidgetItem();parentItem->setNodeText(QString("parent%1").arg(number));MyLabel* label = new MyLabel(itemText);parentItem->m_Label = label;TreeNode* parentData = new TreeNode;parentData->item = parentItem;parentData->nodeText = QString("parent%1").arg(number);parentData->nodeItemTest = itemText;addTopLevelItem(parentItem);setItemWidget(parentItem, 0, label);m_ListData.append(parentData);return parentData;
}void MyTreeWidget::removeTopItem(TreeNode *node)
{int index = indexOfTopLevelItem(node->item);takeTopLevelItem(index);deleteNodeByText(node->nodeText);
}void MyTreeWidget::RecursionInsert(TreeNode *ParentNode, TreeNode *childNode)
{TreeNode* node = this->appendChileItem(ParentNode, childNode->nodeItemTest);for (int i = 0 ; i < childNode->children.size(); ++i) {this->RecursionInsert(node, childNode->children.at(i));}
}void MyTreeWidget::expandAllItems(QTreeWidget *treeWidget, QTreeWidgetItem *item)
{if (item) {treeWidget->expandItem(item); // 展开当前项// 递归展开子项for (int i = 0; i < item->childCount(); ++i) {expandAllItems(treeWidget, item->child(i));}}
}void MyTreeWidget::onScrollBarValueChanged(int value)
{//m_BarValueChangedTemp用于该函数在没有结束的时候又进来了如果不调用takeTopLevelItem就没事,如果没有m_BarValueChangedTemp在该函数未结束还会进入导致崩溃int step = 3; //滚动一次刷新三条数据if (value >= m_VerticalScrollBar->maximum()) {for (int index = 0; index < 1; ++index) {int itemCount = this->topLevelItemCount();MyTreeWidgetItem* bottomItem = dynamic_cast<MyTreeWidgetItem*>(this->topLevelItem(itemCount - 1));TreeNode* bottomNode = findNodeByText(bottomItem->getNodeText(), m_ListData);int bottomIndex = m_ListData.indexOf(bottomNode);if (bottomIndex + 100 < m_ListData.size()) {step = 1000;}else {step = (m_ListData.size() - 1) - bottomIndex;}for (int i = 0 ; i < step; ++i) {//尾部追加显示//获取当前底部的数据bottomIndex++;updateItem(itemCount, m_ListData.at(bottomIndex));itemCount++;}for (int i = 0 ; i < step; ++i) {takeTopLevelItem(0);}}qDebug() << QDateTime::currentDateTime() << "222222222222222222";m_VerticalScrollBar->setValue(value-5);}/*  if (m_BarValueChangedTemp) {qDebug() << "onScrollBarValueChanged未结束就进入";return;}if (m_WheelEventTemp) {qDebug() << "wheelEvent正在滚动中";return;}m_BarValueChangedTemp = true;qDebug() << value << m_LastScrollBarValue<<  this->topLevelItemCount();int step = 3; //滚动一次刷新三条数据if (m_LastScrollBarValue > value) {// 上拉int number = m_LastScrollBarValue - value;for (int index = 0; index < number; ++index) {MyTreeWidgetItem* topItem = dynamic_cast<MyTreeWidgetItem*>(this->topLevelItem(0));TreeNode* topNode = findNodeByText(topItem->getNodeText(), m_ListData);int topIndex = m_ListData.indexOf(topNode);if (topIndex - 3 > 0) {step = 3;}else {step = topIndex;}for (int i = step; i > 0; --i) {//尾部追加显示//获取当前底部的数据topIndex--;updateItem(0, m_ListData.at(topIndex));}for (int i = 0 ; i < step; ++i) {takeTopLevelItem(this->topLevelItemCount() - 1);}}}else {int number = value - m_LastScrollBarValue;qDebug() << QDateTime::currentDateTime() << "111111111111111";for (int index = 0; index < number; ++index) {int itemCount = this->topLevelItemCount();MyTreeWidgetItem* bottomItem = dynamic_cast<MyTreeWidgetItem*>(this->topLevelItem(itemCount - 1));TreeNode* bottomNode = findNodeByText(bottomItem->getNodeText(), m_ListData);int bottomIndex = m_ListData.indexOf(bottomNode);if (bottomIndex + 3 < m_ListData.size()) {step = 3;}else {step = (m_ListData.size() - 1) - bottomIndex;}for (int i = 0 ; i < step; ++i) {//尾部追加显示//获取当前底部的数据bottomIndex++;updateItem(itemCount, m_ListData.at(bottomIndex));itemCount++;}for (int i = 0 ; i < step; ++i) {takeTopLevelItem(0);}}qDebug() << QDateTime::currentDateTime() << "222222222222222222";}m_ScrollBarValue = value;m_LastScrollBarValue = value;m_VerticalScrollBar->setValue(value);m_BarValueChangedTemp = false;*/
}void MyTreeWidget::mouseMoveEvent(QMouseEvent *event)
{QTreeWidget::mouseMoveEvent(event);
}void MyTreeWidget::mousePressEvent(QMouseEvent *event)
{QTreeWidget::mousePressEvent(event);m_CurPos = event->pos();if (event->buttons() & Qt::RightButton) {m->move((QCursor::pos()));m->show();}}void MyTreeWidget::mouseReleaseEvent(QMouseEvent *event)
{QTreeWidget::mouseReleaseEvent(event);
}void MyTreeWidget::startDrag(Qt::DropActions supportedActions)
{//拖拽重写此函数下发当前拖拽对象的nodetextQ_UNUSED(supportedActions);MyTreeWidgetItem* currItem = dynamic_cast<MyTreeWidgetItem*>(this->currentItem());if (currItem) {QString text = currItem->getNodeText();QMimeData* mimeData = new QMimeData;mimeData->setData("application/labelData", text.toUtf8());QDrag* drag = new QDrag(this);drag->setMimeData(mimeData);m_CurrDragItem = currItem;drag->start(Qt::MoveAction);}
}void MyTreeWidget::dragEnterEvent(QDragEnterEvent *event)
{QTreeWidget::dragEnterEvent(event);// 检查拖拽数据格式if (event->mimeData()->hasFormat("application/labelData")) {event->acceptProposedAction();}
}void MyTreeWidget::dragMoveEvent(QDragMoveEvent *event)
{QTreeWidget::dragMoveEvent(event);// 检查拖拽数据格式if (event->mimeData()->hasFormat("application/labelData")) {event->acceptProposedAction();m_DragMovePos = event->pos();viewport()->update();}}void debugTreeData(TreeNode* node, int depth = 0) {QString kongge;for (int i = 0; i < depth; ++i) {kongge.append("----");}qDebug() << kongge << node->nodeItemTest << node->nodeText;for (int i = 0; i < node->children.size(); ++i) {debugTreeData(node->children.at(i), depth + 1); // 递归时深度加1}
}void MyTreeWidget::dropEvent(QDropEvent *event)
{// 检查拖拽数据格式if (event->mimeData()->hasFormat("application/labelData")) {QString itemData = event->mimeData()->data("application/labelData");MyTreeWidgetItem* targetItem = dynamic_cast<MyTreeWidgetItem*>(itemAt(event->pos()));m_CurrDragItem = nullptr;if (!targetItem) {return;}qDebug() << itemData << "-------------------" << targetItem->getNodeText();TreeNode* targetNode = findNodeByText(targetItem->getNodeText(), m_ListData);TreeNode* sourceNode = findNodeByText(itemData, m_ListData);if (sourceNode && targetNode) {// 找到了匹配的节点,可以对其进行操作qDebug() << "找到了匹配的节点,可以对其进行操作" << targetNode->nodeText << sourceNode->nodeText;if (sourceNode->parent == nullptr && targetNode->parent == nullptr) { //父与父//当前是父节点与父节点直接拖拽int targetRow = this->indexOfTopLevelItem(targetNode->item);int sourceRow = this->indexOfTopLevelItem(sourceNode->item);if (targetRow != sourceRow) {qDebug() << "1111111111111111" << targetRow << sourceRow;//在目标源下插入一行if (sourceNode->children.isEmpty()) {this->insertTopItem(targetRow + 1, sourceNode->nodeItemTest);//删除来源itemthis->removeTopItem(sourceNode);}else if (!sourceNode->children.isEmpty()) {//如果来源里面有子节点就需要递归插入,先查入头节点TreeNode* newParentNode = this->insertTopItem(targetRow + 1, sourceNode->nodeItemTest);for (int i = 0 ; i < sourceNode->children.size(); ++i) {this->RecursionInsert(newParentNode, sourceNode->children.at(i));}//删除来源itemthis->removeTopItem(sourceNode);deleteNodeByText(sourceNode->nodeText);}else {qDebug() << "未知动作!!!!!!!!!!!!!!!!!!!!!!!!";}}}else if (sourceNode->parent != nullptr && targetNode->parent != nullptr) { //子与子//双方都是子节点int targetRow = targetNode->parent->item->indexOfChild(targetNode->item);int sourceRow = sourceNode->parent->item->indexOfChild(sourceNode->item);qDebug() << "44444444444444" << targetRow << sourceRow << sourceNode->nodeItemTest << targetNode->nodeItemTest;if (recursiveFindNodeByText(targetNode->nodeText, sourceNode) != nullptr) {//来源节点中不能有目标节点return;}if (sourceNode->children.isEmpty()) {this->insertChileItem(targetNode->parent, targetRow + 1, sourceNode->nodeItemTest);sourceNode->parent->item->removeChild(sourceNode->item);deleteChildNode(sourceNode->parent, sourceNode->nodeText);}else if (!sourceNode->children.isEmpty()) {TreeNode* newParentNode = this->insertChileItem(targetNode->parent, targetRow + 1, sourceNode->nodeItemTest);for (int i = 0 ; i < sourceNode->children.size(); ++i) {this->RecursionInsert(newParentNode, sourceNode->children.at(i));}//删除来源itemsourceNode->parent->item->removeChild(sourceNode->item);deleteChildNode(sourceNode->parent, sourceNode->nodeText);}}else if (sourceNode->parent != nullptr && targetNode->parent == nullptr) { //子与父//来源是子节点,指向端是父节点int targetRow = this->indexOfTopLevelItem(targetNode->item);int sourceRow = sourceNode->parent->item->indexOfChild(sourceNode->item);qDebug() << "222222222222222222" << targetRow << sourceRow << sourceNode->parent->nodeItemTest << sourceNode->nodeItemTest;if (sourceNode->children.isEmpty()) {this->insertTopItem(targetRow + 1, sourceNode->nodeItemTest);sourceNode->parent->item->removeChild(sourceNode->item);deleteChildNode(sourceNode->parent, sourceNode->nodeText);}else if (!sourceNode->children.isEmpty()) {TreeNode* newParentNode = this->insertTopItem(targetRow + 1, sourceNode->nodeItemTest);for (int i = 0 ; i < sourceNode->children.size(); ++i) {this->RecursionInsert(newParentNode, sourceNode->children.at(i));}//删除来源itemsourceNode->parent->item->removeChild(sourceNode->item);deleteChildNode(sourceNode->parent, sourceNode->nodeText);}}else if (sourceNode->parent == nullptr && sourceNode->children.isEmpty() && targetNode->parent != nullptr) { //空父 与子//来源父节点 且没有子节点 且指向端是子节点int targetRow = targetNode->parent->item->indexOfChild(targetNode->item);int sourceRow = this->indexOfTopLevelItem(sourceNode->item);qDebug() << "333333333333" << targetRow << sourceRow << sourceNode->nodeItemTest << targetNode->nodeItemTest;this->insertChileItem(targetNode->parent, targetRow + 1, sourceNode->nodeItemTest);this->removeTopItem(sourceNode);}else if (sourceNode->parent == nullptr && !sourceNode->children.isEmpty() && targetNode->parent != nullptr) { //父多子 与子//来源父节点 有子节点 且指向端是子节点if (recursiveFindNodeByText(targetNode->nodeText, sourceNode) != nullptr) {//来源节点中不能有目标节点return;}int targetRow = targetNode->parent->item->indexOfChild(targetNode->item);int sourceRow = this->indexOfTopLevelItem(sourceNode->item);qDebug() << "5555555555555" << targetRow << sourceRow << sourceNode->nodeItemTest << targetNode->nodeItemTest;TreeNode* newParentNode = this->insertChileItem(targetNode->parent, targetRow + 1, sourceNode->nodeItemTest);for (int i = 0 ; i < sourceNode->children.size(); ++i) {this->RecursionInsert(newParentNode, sourceNode->children.at(i));}//删除来源itemthis->removeTopItem(sourceNode);deleteNodeByText(sourceNode->nodeText);}} else {// 未找到匹配的节点qDebug() << "未找到匹配的节点2";}}//    for (int i = 0; i < m_ListData.size(); ++i) {//        debugTreeData(m_ListData.at(i));//    }
}void MyTreeWidget::paintEvent(QPaintEvent *event)
{// qDebug() << "123123123123";QTreeWidget::paintEvent(event);QPainter painter(viewport());if (m_CurrDragItem != nullptr) {QModelIndex hoveredIndex = indexAt(m_DragMovePos);MyTreeWidgetItem* mcurrMoveItem = dynamic_cast<MyTreeWidgetItem*>(itemFromIndex(hoveredIndex));if (mcurrMoveItem) {QRect rect = visualRect(hoveredIndex); // 获取悬停项的矩形区域QString labelText = mcurrMoveItem->m_Label->text();qDebug()<< "---------------" << labelText << rect << "-----------------";painter.setPen(QPen(QColor(34, 142, 243), 2)); // 设置线条颜色和粗细// 绘制线条painter.drawLine(rect.x() + 6, rect.y() + rect.height(), rect.x() + rect.width() - 6, rect.y() + rect.height()); // 绘制底部线条// 绘制头部小圆圈painter.setBrush(QBrush(QColor(34, 142, 243))); // 设置圆圈颜色painter.drawEllipse(QPoint(rect.x() + 2, rect.y() + rect.height()), 2, 2); // 绘制小圆圈// 绘制尾部小圆圈painter.drawEllipse(QPoint(rect.x() + rect.width()-2, rect.y() + rect.height()), 2, 2); // 绘制小圆圈}}
}void MyTreeWidget::wheelEvent(QWheelEvent *event)
{if (m_WheelEventTemp) {qDebug() << "wheelEvent未结束就进入";return;}if (m_VerticalScrollBar->isSliderDown()) {qDebug() << "正在拖拽滚动条";return;}m_WheelEventTemp = true;qDebug() << "1111111111111111111" << event->angleDelta().y() <<this->topLevelItemCount() << m_VerticalScrollBar->isSliderDown();int number = qAbs(event->angleDelta().y()) / 120;int step = 3; //滚动一次刷新三条数据for (int i = 0; i < number; ++i) {if (event->angleDelta().y() > 0) {MyTreeWidgetItem* topItem = dynamic_cast<MyTreeWidgetItem*>(this->topLevelItem(0));TreeNode* topNode = findNodeByText(topItem->getNodeText(), m_ListData);int topIndex = m_ListData.indexOf(topNode);if (topIndex - 3 > 0) {step = 3;}else {step = topIndex;}for (int i = step; i > 0; --i) {//尾部追加显示//获取当前底部的数据topIndex--;updateItem(0, m_ListData.at(topIndex));}for (int i = 0 ; i < step; ++i) {takeTopLevelItem(this->topLevelItemCount() - 1);}if (m_ScrollBarValue > 0) {m_ScrollBarValue -= double(m_VerticalScrollBar->maximum()) / double(m_ListData.size() / (step == 0 ? 1 : step));qDebug() << "向上滚动" << topIndex <<m_ScrollBarValue;m_VerticalScrollBar->setValue(m_ScrollBarValue);}}else {int itemCount = this->topLevelItemCount();MyTreeWidgetItem* bottomItem = dynamic_cast<MyTreeWidgetItem*>(this->topLevelItem(itemCount - 1));TreeNode* bottomNode = findNodeByText(bottomItem->getNodeText(), m_ListData);int bottomIndex = m_ListData.indexOf(bottomNode);if (bottomIndex+3 < m_ListData.size()) {step = 3;}else {step = (m_ListData.size() - 1) - bottomIndex;}for (int i = 0 ; i < step; ++i) {//尾部追加显示//获取当前底部的数据bottomIndex++;updateItem(itemCount, m_ListData.at(bottomIndex));itemCount++;}for (int i = 0 ; i < step; ++i) {takeTopLevelItem(0);}if (m_VerticalScrollBar->value() < m_VerticalScrollBar->maximum()) {m_ScrollBarValue += double(m_VerticalScrollBar->maximum()) / double(m_ListData.size() / (step == 0 ? 1 : step));qDebug() << "向下滚动" << bottomIndex <<m_ScrollBarValue;m_VerticalScrollBar->setValue(m_ScrollBarValue);}}}m_WheelEventTemp = false;
}MyTreeWidgetItem::MyTreeWidgetItem()
{}void MyTreeWidgetItem::setNodeText(QString str)
{m_Text = str;
}QString MyTreeWidgetItem::getNodeText()
{return m_Text;
}MyLabel::MyLabel(QString text, QWidget *parent): QLabel(parent)
{setText(text);installEventFilter(this);enterStyle = "background-color: rgb(225, 243, 255);";leaveStyle = "background-color: white;";selectStyle = "background-color: rgb(204, 232, 255);";
}bool MyLabel::eventFilter(QObject *watched, QEvent *event)
{return QWidget::eventFilter(watched, event);
}
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);m_MyTreeWidget = new MyTreeWidget();//    if (1) {qDebug() << QDateTime::currentDateTime() << "111111111111111";for (int i = 0; i < 100000; ++i) {if (i >= 100) {m_MyTreeWidget->insertTopItem(i, QString("父节点%1").arg(i+1), false);}else {m_MyTreeWidget->insertTopItem(i, QString("父节点%1").arg(i+1));}}qDebug() << QDateTime::currentDateTime() << "2222222222222";//    for (int i = 0; i < 500; ++i) {//        m_MyTreeWidget->insertChileItem(m_MyTreeWidget->m_ListData.at(0), m_MyTreeWidget->m_ListData.at(0)->item->childCount(),QString("子节点%1").arg(i+1));//    }//    for (int i = 0; i < 500; ++i) {//        m_MyTreeWidget->insertChileItem(m_MyTreeWidget->m_ListData.at(1), m_MyTreeWidget->m_ListData.at(1)->item->childCount(),QString("子节点%1").arg(i+1));//    }//    for (int i = 0; i < 500; ++i) {//        m_MyTreeWidget->insertChileItem(m_MyTreeWidget->m_ListData.at(2), m_MyTreeWidget->m_ListData.at(2)->item->childCount(),QString("子节点%1").arg(i+1));//    }//    for (int i = 0; i < 500; ++i) {//        m_MyTreeWidget->insertChileItem(m_MyTreeWidget->m_ListData.at(3), m_MyTreeWidget->m_ListData.at(3)->item->childCount(),QString("子节点%1").arg(i+1));//    }ui->gridLayout->addWidget(m_MyTreeWidget);// 在您的代码中调用此函数来展开所有项//m_MyTreeWidget->expandAllItems(m_MyTreeWidget, m_MyTreeWidget->invisibleRootItem());
}Widget::~Widget()
{delete ui;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/138417.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Docker 容器数据卷

是什么 卷就是目录或文件&#xff0c;存在于一个或多个容器中&#xff0c;由docker挂载到容器&#xff0c;但不属于联合文件系统&#xff0c;因此能够绕过Union File System提供一些用于持续存储或共享数据的特性&#xff1a;卷的设计目的就是数据的持久化&#xff0c;完全独立…

[设计模式] 浅谈SOLID设计原则

目录 单一职责原则开闭原则里氏替换原则接口隔离原则依赖倒转原则 SOLID是一个缩写词&#xff0c;代表以下五种设计原则 单一职责原则 Single Responsibility Principle, SRP开闭原则 Open-Closed Principle, OCP里氏替换原则 Liskov Substitution Principle, LSP接口隔离原则 …

Linux 中的make/makefile

一&#xff1a;背景 make是一个命令工具&#xff0c;是一个解释makefifile中指令的命令工具&#xff0c;一般来说&#xff0c;大多数的IDE都有这个命令&#xff0c;比如&#xff1a;Delphi的make&#xff0c;Visual C的nmake&#xff0c;Linux下GNU的make。可见&#xff0c;mak…

【Xilinx】基于MPSoC的OpenAMP实现(一)

【Xilinx】基于MPSoC的OpenAMP实现&#xff08;一&#xff09; 一、开发环境1、开发思路2、下载官方bsp包 二、编译Linux1、配置petalinux环境变量2、创建工程3、进入目录4、设置缓存目录&#xff08;重点&#xff1a;可离线编译&#xff0c;加快编译速度&#xff09;5、配置u-…

JMeter断言之JSON断言

JSON断言 若服务器返回的Response Body为JSON格式的数据&#xff0c;使用JSON断言来判断测试结果是较好的选择。 首先需要根据JSON Path从返回的JSON数据中提取需要判断的实际结果&#xff0c;再设置预期结果&#xff0c;两者进行比较得出断言结果。 下面首先介绍JSON与JSON…

springboot01

目录 新建Maven工程&#xff0c;什么都不选 ​pom.xml加上 新建包top.cjz.controller 新建类HelloController ​新建类HelloApplication ​运行浏览器访问 新建Maven工程&#xff0c;什么都不选 pom.xml加上 <!--springboot工程需要继承的父工程--> <parent…

第六次面试、第一次复试

第六面&#xff1a; hr迟到&#xff0c;说是搞错了以为线下&#xff0c;我打电话过去才开始&#xff0c;问我想电话面还是视频&#xff0c;果断电话面 自我介绍 介绍了一下公司的工作 ................. 项目拷打&#xff1a; grpc数据如何传输的如何调用两个接口如何获取…

如何使用 Humata.ai:快速理解和总结文献

链接&#xff1a; Humata 简介 Humata.ai 是一个人工智能驱动的文献阅读助手&#xff0c;可以帮助用户快速理解和总结文献。它可以提取文献的关键信息&#xff0c;并以简洁易懂的语言生成摘要。此外&#xff0c;Humata.ai 还可以回答用户关于文献的问题&#xff0c;帮助用户…

用户与权限管理

文章目录 用户与权限管理1. 用户管理1.1 MYSQL用户1.2 登录MySQL服务器1.3 创建用户1.4 修改用户1.5 删除用户1.6 修改密码1. 修改当前用户密码2. 修改其他用户密码 1.7 MYSQL8密码管理 2. 权限管理2.1 权限列表2.2 授予权限的原则2.3 授予权限2.4 查看权限2.5 收回权限 3. 权限…

python快速实现带界面可点击的简易计算器

这篇文章将带你探索如何使用Python创建一个直观且实用的带界面计算器。我们将深入介绍如何利用Python的图形用户界面库&#xff0c;特别是Tkinter&#xff0c;来构建一个友好的用户界面&#xff0c;让你能够轻松进行数学运算。无论你是初学者还是有一定编程经验&#xff0c;本文…

基于Face++网络爬虫+人脸融合算法智能发型推荐程序——深度学习算法应用(含Python及打包exe工程源码)+爬虫数据集

目录 前言总体设计系统整体结构图系统流程图 运行环境Python环境Pycharm 环境 模块实现1. Face.APl调用1&#xff09;Face.APl介绍2&#xff09;调用API 2. 数据爬取1&#xff09;网络数据爬取步骤2&#xff09;爬虫实现 3. 模型构建4. 用户界面设计1&#xff09;需要调用的库文…

竞赛 基于深度学习的人脸表情识别

文章目录 0 前言1 技术介绍1.1 技术概括1.2 目前表情识别实现技术 2 实现效果3 深度学习表情识别实现过程3.1 网络架构3.2 数据3.3 实现流程3.4 部分实现代码 4 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 基于深度学习的人脸表情识别 该项目较…

学习记忆——宫殿篇——记忆宫殿——记忆桩——单间+客厅+厨房+厕所+书房+院子

文章目录 单间客厅厨房厕所书房院子 单间 水壶 水龙头 香皂 果汁机 电视 门空间 花 红酒 葡萄 不锈钢 白毛沙发 彩色垫子 吉他 皮椅 挂画 风扇 糖抱枕 盒子 花土 水晶腿 衣柜 笔 三环相框 水壶 壁挂 台灯 被 网球拍 足球 抽屉 闹钟 蝴蝶 心 斑马 三轮车 音响 椅子 碗 玩偶 烟灰…

【记录】Python 之于 C/C++ 区别

记录本人在 Python 上经常写错的一些地方&#xff08;C/C 写多了&#xff0c;再写 Python 有点切换不过来&#xff09; 逻辑判断符号用 and、or、!可以直接 10 < num < 30 比较大小分支语句&#xff1a;if、elif、else使用 、-&#xff0c;Python 中不支持 、- - 这两个…

BOA服务器移植

BOA服务器移植 1、源码下载 http://www.boa.org/ News! (last updated 23 February 2005) Latest Released Version (0.94.13) here (signature here) --- 下载地址1.1 boa简介&#xff1a; 其可执行代码只有大约60KB左右&#xff0c;Boa是一个单任务的HTTP服务器&#xff…

MySQL 高级语句 Part1(进阶查询语句+MySQL数据库函数+连接查询)

高级语句 第一部分 一、MySQL进阶查询语句1.1 select ----显示表格中一个或数个字段的所有数据记录1.2 distinct ----不显示重复的数据记录1.3 where ----有条件查询1.4 and or ----且 或1.5 in----显示已知的值的数据记录1.6 between----显示两个值范围内的数据记录1.7 通配符…

spring boot +vue 博客系统,开源的资源网站

spring boot vue 博客系统&#xff0c;开源的资源网站&#xff08;Aurora前后端分离博客) 体验地址&#xff1a;http://blog.tlzcf.vip/ 相关技术 前端&#xff1a; 样式来自于&#xff1a;hexo的aurora主题基础框架&#xff1a;vue3(前台) vue2(后台)状态管理&#xff1a;…

高云FPGA系列教程(10):letter-shell移植

文章目录 letter-shell简介letter-shell源码获取letter-shell移植函数和变量应用示例 本文是高云FPGA系列教程的第10篇文章。 shell&#xff0c;中文是外壳的意思&#xff0c;就是操作系统的外壳。通过shell命令可以操作和控制操作系统&#xff0c;比如Linux中的Shell命令就包括…

C# 流Stream详解(3)——FileStream源码

【FileStream】 构造函数 如果创建一个FileStream&#xff0c;常见的参数例如路径Path、操作方式FileMode、权限FileAccess。 这里说下FileShare和SafeFileHandle。 我们知道在读取文件时&#xff0c;通常会有两个诉求&#xff1a;一是如何更快的读取文件内容&#xff1b;二…

安装社区版本OB

获取一键安装包 https://www.oceanbase.com/softwarecenter 离线安装 [admintest001 ~]$ tar -xzf oceanbase-all-in-one-*.tar.gz [admintest001 ~]$ cd oceanbase-all-in-one/bin/ [admintest001 bin]$ ./install.sh [admintest001 bin]$ source ~/.oceanbase-all-in-one/…