qt 中 如何用 QGraphicsRectItem 坐标转化 sence 坐标
时间: 2024-03-17 21:40:57 浏览: 21
在Qt中,可以使用QGraphicsRectItem的mapToScene()函数将其坐标转换为场景坐标。示例代码如下:
```cpp
QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 100, 100); // 创建一个矩形项
QPointF rectPos = rectItem->pos(); // 获取矩形项在父项坐标系中的位置
QPointF scenePos = rectItem->mapToScene(rectPos); // 将矩形项的位置转换为场景坐标系中的位置
```
在这个示例中,我们首先创建了一个QGraphicsRectItem矩形项,并获取了它在父项坐标系中的位置。然后,我们使用mapToScene()函数将其位置转换为场景坐标系中的位置。最终,我们得到了矩形项在场景中的位置,存储在scenePos变量中。
相关问题
qt 中绘制多个 QGraphicsRectItem 不能相互重叠
在 Qt 中,可以通过设置 QGraphicsItem 的属性来实现不重叠。具体来说,可以设置 QGraphicsItem 的 flag 为 QGraphicsItem::ItemIsMovable 和 QGraphicsItem::ItemSendsGeometryChanges,然后重载 QGraphicsItem 的 itemChange() 函数,在该函数中判断是否与其他 QGraphicsItem 重叠,如果重叠则将该 QGraphicsItem 移回到原来的位置。代码示例如下:
```cpp
class MyItem : public QGraphicsRectItem
{
public:
MyItem(QGraphicsItem* parent = nullptr) : QGraphicsRectItem(parent)
{
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemSendsGeometryChanges);
}
QVariant itemChange(GraphicsItemChange change, const QVariant& value) override
{
if (change == QGraphicsItem::ItemPositionChange && scene())
{
// 判断是否与其他 QGraphicsItem 重叠
QList<QGraphicsItem*> items = scene()->items(pos(), Qt::IntersectsItemShape, Qt::AscendingOrder);
for (QGraphicsItem* item : items)
{
if (item != this && collidesWithItem(item))
{
// 移回到原来的位置
return QGraphicsRectItem::itemChange(change, value);
}
}
}
return QGraphicsRectItem::itemChange(change, value);
}
};
```
使用时,创建多个 MyItem 对象并添加到 QGraphicsScene 中即可。
qt QGraphicsView建立坐标系画直方图
可以使用 QGraphicsScene 和 QGraphicsRectItem 来实现直方图的绘制。首先,需要创建一个 QGraphicsScene 对象,并将其设置为 QGraphicsView 的场景。然后,可以使用 QGraphicsRectItem 来绘制直方图的每个条形。具体实现可以参考以下代码:
```cpp
QGraphicsScene* scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
// 绘制坐标系
QPen pen(Qt::black);
QGraphicsLineItem* xAxis = scene->addLine(0, 0, 200, 0, pen);
QGraphicsLineItem* yAxis = scene->addLine(0, 0, 0, 200, pen);
// 绘制直方图
QVector<int> data = {10, 20, 30, 40, 50};
int barWidth = 20;
int x = 0;
for (int i = 0; i < data.size(); ++i) {
int height = data[i];
QGraphicsRectItem* bar = scene->addRect(x, -height, barWidth, height, pen);
x += barWidth + 10;
}
```
这段代码会在 QGraphicsView 中绘制一个简单的直方图,其中 data 数组表示每个条形的高度,barWidth 表示每个条形的宽度,x 表示每个条形的 x 坐标。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)