gpt4 book ai didi

qt - QTreeWidgetItem 中图标的位置

转载 作者:行者123 更新时间:2023-12-01 22:29:42 28 4
gpt4 key购买 nike

我的QTreeWidget有一个列。它的项目有一个复选框、一个图标和文本。如果用户在某个项目内单击,我想知道该图标是否被单击。如何在 QTreeWidgetItem 中找到图标的位置和大小?

更新添加:这是我的最终解决方案的代码,根据 webclectic 的要求。

首先,我对 QItemDelegate 进行了子类化,以便可以访问 QTreeWidgetItem 的每个部分的坐标(复选框、图标和文本)。这是头文件:

#include <QItemDelegate>

class MyItemDelegate : public QItemDelegate
{
Q_OBJECT

public:
explicit MyItemDelegate (MyTreeWidget *parent)
: QItemDelegate (parent), ParentView (parent) { }
~MyItemDelegate() { }

void GetRects (const QModelIndex &index, QRect& CheckBox, QRect& Icon, QRect& Text) const ;

private:
MyTreeWidget* ParentView ;
} ;

这是源文件:

void MyItemDelegate::GetRects (const QModelIndex &index, QRect& CheckBox, QRect& Icon, QRect& Text) const
{
QStyleOptionViewItem option = ParentView -> viewOptions() ;
CheckBox = rect (option, index, Qt::CheckStateRole) ;
Icon = rect (option, index, Qt::DecorationRole) ;
Text = rect (option, index, Qt::DisplayRole) ;

doLayout (option, &CheckBox, &Icon, &Text, true) ;

QRect VisualRect = ParentView -> visualRect (index) ;
CheckBox.translate (VisualRect.topLeft()) ;
Icon.translate (VisualRect.topLeft()) ;
Text.translate (VisualRect.topLeft()) ;
}

然后我向 MyTreeWidget 添加了一个 MyItemDelegate* 成员,并将其设置为项目 View 的委托(delegate)。在标题中:

class MyTreeWidget : public QTreeWidget
{
...
MyItemDelegate* Delegate ;
...
} ;

在来源中:

MyTreeWidget::MyTreeWidget (QObject* parent)
{
...
Delegate = new MyItemDelegate (this) ;
setItemDelegate (ItemDelegate) ;
}

现在,获取QTreeWidgetItem各部分的坐标:

  QTreeWidgetItem* item ;
...
QModelIndex ModelIndex = indexFromItem (item) ;
QRect CheckBoxRect, IconRect, TextRect ;
ItemDelegate -> GetRects (ModelIndex, &CheckBoxRect, &IconRect, &TextRect) ;

最佳答案

不幸的是,没有简单的方法可以实现您想要的目标。问题是 QTreeWidget 负责绘制其项目,因此项目本身没有有关其元素在 View 中的位置的信息。

首先,您必须子类化QTreeWidget并重新实现mousePressEvent(如果您愿意,也可以实现mouseReleaseEvent)。在事件内部,您应该计算图标的位置并进行相应的处理。

示例代码(但未经测试)如下:

void mousePressEvent(QMouseEvent *event)
{
QModelIndex clickedIndex = indexAt(event->pos());
// make sure the event was on a valid item
if (clickedIndex.isValid() == false)
return;

// Get the tree widget's x position
int treeX = header()->sectionViewportPosition(0);

// Get the x coordinate of the root item. It is required in order to calculate
// the identation of the item
int rootX = visualRect(rootIndex()).x();

// Get the rectangle of the viewport occupied by the pressed item
QRect vrect = visualRect(clickedIndex);

// Now we can easily calculate the x coordinate of the item
int itemX = treeX + vrect.x() - rootX;

// The item is a checkbox, then an icon and finally the text.

// 1. Get the rect surrounding the checkbox
QRect checkboxRect = QRect(itemX,
vrect.y(),
style()->pixelMetric(QStyle::PM_IndicatorWidth),
vrect.height());

// 2. Get the rect surrounding the icon
QRect iconRect = QRect(itemX + checkboxRect.width(),
vrect.y(),
iconSize().width(),
vrect.height());

// 3. Finally get the rect surrounding the text
QRect textRect = QRect(itemX + checkboxRect.width() + iconRect.width(),
vrect.y(),
vrect.width() - checkboxRect.width() - iconRect.width(),
vrect.height());

// Now check where the press event took place and handle it correspondingly

if(checkboxRect.contains(event->pos()))
{
qDebug() << "Checkbox pressed";
QTreeWidget::mousePressEvent(event);
return;
}
else if (iconRect.contains(event->pos()))
{
qDebug() << "Icon pressed";
QTreeWidget::mousePressEvent(event);
return;
}
else
{
qDebug() << "Text pressed";
QTreeWidget::mousePressEvent(event);
return;
}
}

我再说一遍,代码未经测试,但您知道如何实现您想要的。

关于qt - QTreeWidgetItem 中图标的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8274123/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com