QRect textRect = painter.boundingRect(rect(), Qt::AlignCenter, text);改成左右居中,上下為距離下方1/4
时间: 2023-07-15 12:15:42 浏览: 46
可以尝试修改为以下代码:
```
QRect textRect = painter.boundingRect(rect(), Qt::AlignHCenter | Qt::AlignBottom, text);
int y = rect().bottom() - rect().height() / 4 - textRect.height();
textRect.moveTop(y);
```
这里使用了 `Qt::AlignHCenter` 来让文本左右居中,使用 `Qt::AlignBottom` 来让文本底部与矩形底部对齐。然后通过计算使得文本垂直方向上距离矩形底部为矩形高度的 1/4。最后将计算出的位置应用到 `textRect` 上。
相关问题
QRect textRect = painter.boundingRect(rect(), Qt::AlignCenter, text);改成左右居中,上下偏下
你可以将 `Qt::AlignCenter` 改成 `Qt::AlignHCenter | Qt::AlignBottom`,即左右居中、上下偏下对齐方式。修改后的代码如下:
```
QRect textRect = painter.boundingRect(rect(), Qt::AlignHCenter | Qt::AlignBottom, text);
```
这样就可以实现你所需的效果了。
QString text1 = "5"; QRect textRect1 = painter.boundingRect(rect(), Qt::AlignHCenter | Qt::AlignBottom, text1); painter.drawText(textRect1, text1);使text1向上移動50
你可以使用`QRect::translate()`函数将`textRect1`向上移动50个像素,然后再使用`painter.drawText()`函数绘制文本。代码示例如下:
```
QString text1 = "5";
QRect textRect1 = painter.boundingRect(rect(), Qt::AlignHCenter | Qt::AlignBottom, text1);
textRect1.translate(0, -50); // 向上移动50个像素
painter.drawText(textRect1, text1);
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)