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.mzph.cn/news/84184.shtml

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

相关文章

Prometheus-Rules 实战

文章目录 1 node rules2 nginx rule2.1 Nginx 4xx 错误率太多2.2 Nginx 5xx 错误率太多2.3 Nginx 延迟高 3 mysql rule3.1 MySQL 宕机3.2 实例连接数过多3.3 MySQL高线程运行3.4 MySQL 从服务器 IO 线程没有运行3.5 MySQL 从服务器 SQL 线程没有运行3.6 MySQL复制滞后3.7 慢查询…

天津专升本文化课考试计算机应用基础考试大纲(2023年9月修订)

天津市高等院校“高职升本科”招生统一考试计算机应用基础考试大纲&#xff08;2023年9月修订&#xff09; 一、考试性质 天津市高等院校“高职升本科”招生统一考试是由合格的高职高专毕业生参加的选拔性 考试。高等院校根据考生的成绩&#xff0c;按照已确定的招生计划&am…

transformer系列2---transformer架构详细解析

transformer详细解析 Encoder1 输入1.1 Embedding 词嵌入1.1.1 Embedding 定义1.1.2 几种编码方式对比1.1.3 实现代码 1.2 位置编码1.2.1 使用位置编码原因1.2.2 位置编码方式1.2.3 位置编码代码 2 注意力 Attention2.1 自注意力self-attention2.1.1 QKV含义2.1.2 自注意力公式…

Innodb底层原理与Mysql日志机制

MySQL内部组件结构 Server层 主要包括连接器、词法分析器、优化器、执行器等&#xff0c;涵盖 MySQL 的大多数核心服务功能&#xff0c;以及所有的内置函数&#xff08;如日期、时间、数学和加密函数等&#xff09;&#xff0c;所有跨存储引擎的功能都在这一层实现&#xff0c…

【Vue】路由与Node.js下载安装及环境配置教程

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是Java方文山&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的专栏《Vue快速入门》。&#x1f3af;&#x1f3af; &…

无涯教程-JavaScript - COUNT函数

描述 COUNT函数计算包含数字的单元格的数量,并计算参数列表中的数字。使用COUNT函数获取在数字范围或数字数组中的数字字段中的条目数。 语法 COUNT (value1, [value2] ...)争论 Argument描述Required/Optionalvalue1The first item, cell reference, or range within whic…

React TypeScript | 快速了解 antd 的使用

1. 安装&#xff1a; 就像安装其他插件库一样&#xff0c;在项目文件夹下执行&#xff1a; npm install antd --save如果你安装了 yarn&#xff0c;也可以执行&#xff1a; yarn add antd2. 引用 import { Button, Tooltip } from "antd"; import "antd/dis…

第三、四、五场面试

第三场 共享屏幕做题&#xff08;三道简单题&#xff09; 替换空格成%20&#xff08;双指针&#xff09; 删除升序链表中的重复元素&#xff08;指针&#xff09;有效的括号&#xff08;栈&#xff09; 第四场、第五场 自我介绍 项目拷打 整个项目架构rpc模块的情况分析的数…

云渲染是怎么做到不排队的

云渲染排队严重影响了用户的使用体验&#xff0c;还会导致用户的流失&#xff0c;因此解决渲染不排队的问题对于云渲染平台来说是非常重要的。而作为云渲染平台&#xff0c;自己的机器基本都是固定的&#xff0c;哪如何解决用户渲染排队的问题呢&#xff1f;我们一起来看看全国…

JOSEF约瑟 剩余电流继电器PFR-5 PFE-W-20 国产化改造ZLR-G81 ZCT-45

系列型号&#xff1a; PFR-003剩余电流继电器 PFR-03剩余电流继电器 PFR-5剩余电流继电器 PFR-W-105互感器 PFR-W-140互感器 PFR-W-20互感器 PFR-W-210互感器 PFR-W-30互感器 PFR-W-35互感器 PFR-W-70互感器 一、用途 PFR剩余电流继电器&#xff08;以下简称继电器…

蓝桥杯 题库 简单 每日十题 day3

01 约数个数 题目描述 本题为填空题&#xff0c;只需要算出结果后&#xff0c;在代码中使用输出语句将所填结果输出即可。 1200000 有多少个约数&#xff08;只计算正约数&#xff09;。 解题思路 枚举&#xff0c;从1开始一直到1200000本身都作为1200000的除数&#xff0c;…

机器学习——奇异值分解(未完)

开坑&#xff0c;刚看完书&#xff0c;已经有些窒息了 先把坑挖了&#xff0c;再慢慢填&#xff0c;避免自己划水跳过 我爱线代&#xff0c;线代爱我&#xff0c;阿弥陀佛 为什么要学奇异值分解&#xff1f; 因为书本倒数第二章专门提到的&#xff0c;想必一定很重要&#xff…

GitHub无法访问的解决方法

每次刚装完系统我们访问GitHub就会出现无法访问的情况&#xff0c;此时只需要修改host文件将可访问的dns解析地址写入进去即可。 查询DNS 使用dns监测查询工具 https://tool.chinaz.com/dns https://dnsdaquan.com/ 输入无法访问的IP github.com 进行检测 查询到可访问的i…

iOS“超级签名”绕过App Store作弊解决方案

一直以来&#xff0c;iOS端游戏作弊问题都是游戏行业的一大痛点。在当下游戏多端互通的潮流下&#xff0c;游戏作为一个整体&#xff0c;无论哪一端出现安全问题&#xff0c;都会造成更加严重的影响。因此&#xff0c;iOS端游戏安全保护也同样十分重要。 iOS独特的闭源生态&am…

HOOPS Visualize 2023 SP2 U1 Crack-HOOPS Visualize

HOOPS Visualize 是一个以工程为中心的高性能图形库&#xff0c;用于在桌面、移动和 AR/VR 设备上渲染 3D CAD 模型。该 3D 图形库具有线程安全的 C 和 C# 接口以及 OpenGL 和 DirectX 驱动程序&#xff0c;并由响应迅速的专业图形专家提供支持。通过访问最新的 3D GPU 功能&am…

函数模板的概念和意义

问题 C 中有几种交换变量的方法&#xff1f; 交换变量的方法 定义宏代码块 优点&#xff1a;代码复用&#xff0c;适合所有类型缺点&#xff1a;编译器不知道宏的存在&#xff0c;缺少类型检查 定义函数 优点&#xff1a;真正的函数调用&#xff0c;编译器对类型进行检查缺…

【人工智能】企业如何使用 AI与人工智能的定义、研究价值、发展阶段的深刻讨论

前言 人工智能&#xff08;Artificial Intelligence&#xff09;&#xff0c;英文缩写为AI。 它是研究、开发用于模拟、延伸和扩展人的智能的理论、方法、技术及应用系统的一门新的技术科学。人工智能是新一轮科技革命和产业变革的重要驱动力量。 &#x1f4d5;作者简介&#x…

爬虫项目(四):抓取网页所有图片

文章目录 一、书籍推荐二、完整代码三、运行结果 一、书籍推荐 推荐本人书籍《Python网络爬虫入门到实战》 &#xff0c;详细介绍见&#x1f449;&#xff1a; 《Python网络爬虫入门到实战》 书籍介绍 二、完整代码 原理&#xff1a;抓取该链接中所有的图片格式。基于seleni…

SpringBoot+MyBatis flex实现简单增删改查

一&#xff1a;创建SpringBoot项目 SpringBoot版本选择2.7.15 勾选相关的选项&#xff0c;并点击Create 项目创建完成 二.pom文件添加相关的依赖 <dependencies><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starte…

企业如何找媒体发稿能收录且不被拒稿,媒介盒子无偿分享

媒平台像头条、百家号、微信、微博、搜狐等平台&#xff0c;都支持全自助发稿&#xff0c;拥有庞大的用户群体。也正是因为这些平台的发展&#xff0c;衍生出了一大批自媒体KOL&#xff0c;影响力与传统媒体不相上下甚至更胜。 媒体宣发是企业营销的必要途径之一。软文是成本低…