painter->setClipRect(opt.rect.translated(-opt.rect.topLeft())); 这一句的作用是什么
时间: 2024-03-29 20:39:13 浏览: 20
这段代码是用于设置绘图区域的裁剪矩形,其作用是:限制 QPainter 绘制的区域只在 `opt.rect` 矩形内。具体来说,`opt.rect` 是一个矩形,`opt.rect.topLeft()` 返回矩形左上角的坐标,`-opt.rect.topLeft()` 返回一个与 `opt.rect` 左上角坐标相反的偏移量。`translated()` 方法是将 `opt.rect` 矩形平移(移动)指定的偏移量,得到一个新的矩形,这个新的矩形左上角的坐标就是原矩形的左上角坐标减去指定的偏移量,因此 `opt.rect.translated(-opt.rect.topLeft())` 得到的是一个与 `opt.rect` 矩形大小相同,但左上角坐标为原来的相反数的矩形。
然后,`painter->setClipRect()` 方法将这个矩形作为裁剪区域设置给 QPainter,这意味着在绘制图形时,只有在 `opt.rect` 矩形内的部分才会被绘制出来,超出这个矩形的部分将被裁剪掉,不会被显示出来。这样可以确保绘制的图形只出现在指定的矩形区域内,而不会超出或遮挡其他控件。
相关问题
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { if (index.column() == 0) { QTreeWidget* treeWidget = qobject_cast<QTreeWidget*>(index.data(Qt::UserRole).value<void*>()); if (treeWidget) { painter->save(); // 设置绘制区域 QRect rect = option.rect.adjusted(2, 2, -2, -2); painter->setClipRect(rect); painter->translate(rect.topLeft()); // 绘制树形控件 treeWidget->resize(rect.size()); treeWidget->render(painter, QPoint(), QRegion(), QWidget::DrawChildren); painter->restore(); return; } } QStyledItemDelegate::paint(painter, option, index); }
这段代码是一个自定义的委托类的 `paint` 函数,用于在 `QTableWidget` 控件中第一列绘制嵌套的 `QTreeWidget` 控件。
在该函数中,首先判断当前绘制的是第一列,并尝试将 `Qt::UserRole` 角色的数据转换成 `QTreeWidget` 类型的指针,如果转换成功则说明当前单元格需要绘制 `QTreeWidget`,接下来就可以绘制 `QTreeWidget` 控件了。
如果转换失败,则说明当前单元格不需要绘制 `QTreeWidget` 控件,直接调用基类的 `QStyledItemDelegate::paint` 函数进行绘制即可。
需要注意的是,该委托类的 `paint` 函数中使用了 `qobject_cast` 函数进行类型转换,因此需要在类定义中使用 `Q_OBJECT` 宏进行声明,否则会导致转换失败。
优化一下下面这段代码 if(this->isShowLockRect()) { for (auto rect : m_lockRects) { painter->setBrush(QBrush()); painter->setPen(QPen(Qt::red,80)); QPointF point = this->mapFromScene(rect.at(0),rect.at(1)); painter->drawRect(point.x(), -point.y(), abs(rect.at(2) - rect.at(0)), abs(rect.at(3) - rect.at(1))); } }
有几个地方可以对上述代码进行优化:
1. 避免重复的函数调用:将 `this->isShowLockRect()` 的结果存储在一个变量中,避免在每次循环迭代中重复调用该方法。
2. 减少重复的设置操作:将画笔和画刷的属性设置移到循环之外,并且只在需要绘制矩形时进行设置。
3. 使用矩形的成员函数 `width()` 和 `height()`,避免多次访问矩形的坐标点。
优化后的代码如下所示:
```cpp
bool showLockRect = this->isShowLockRect();
if (showLockRect) {
painter->setBrush(QBrush());
painter->setPen(QPen(Qt::red, 80));
}
for (const auto& rect : m_lockRects) {
if (showLockRect) {
QPointF startPoint = this->mapFromScene(rect.topLeft());
qreal width = rect.width();
qreal height = rect.height();
painter->drawRect(startPoint.x(), -startPoint.y(), width, height);
}
}
```
这样做的好处是,避免了重复的函数调用和设置操作,并且使用矩形的成员函数进行宽高计算,使代码更加简洁和高效。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)