QT5 QGraphicsView获得已选中的所有QGraphicsItem,对选中的QGraphicsItem的X坐标进行排序
时间: 2023-08-26 18:28:16 浏览: 69
你可以使用QGraphicsScene的selectedItems()函数来获取所有选中的QGraphicsItem对象。然后,你可以使用QGraphicsItem的pos()函数获取每个QGraphicsItem的位置。最后,你可以使用std::sort函数对所有QGraphicsItem的位置进行排序。以下是一个示例代码:
```cpp
QList<QGraphicsItem*> selectedItems = scene->selectedItems();
QList<qreal> xPositions;
foreach(QGraphicsItem* item, selectedItems) {
xPositions << item->pos().x();
}
std::sort(xPositions.begin(), xPositions.end());
foreach(qreal xPos, xPositions) {
// do something with sorted x positions
}
```
注意:这里使用了QList容器类来存储X坐标。如果你需要对y坐标进行排序,只需将pos().x()替换为pos().y()即可。
相关问题
设置QGraphicsItem在QGraphicsView的坐标
要将QGraphicsItem放置在QGraphicsView中的特定坐标上,可以使用以下步骤:
1. 创建一个QGraphicsView对象和一个QGraphicsScene对象:
```python
view = QGraphicsView()
scene = QGraphicsScene()
view.setScene(scene)
```
2. 创建一个QGraphicsItem对象(例如QGraphicsRectItem)并设置其位置和尺寸:
```python
item = QGraphicsRectItem(0, 0, 100, 100) # 参数分别为x、、width、height
```
3. 将QGraphicsItem添加到QGraphicsScene中:
```python
scene.addItem(item)
```
4. 设置QGraphicsView的坐标系统为Scene坐标系:
```python
view.setRenderHint(QPainter.Antialiasing)
view.setRenderHint(QPainter.TextAntialiasing)
view.setRenderHint(QPainter.SmoothPixmapTransform)
view.setRenderHint(QPainter.HighQualityAntialiasing)
view.setRenderHint(QPainter.NonCosmeticDefaultPen)
view.setDragMode(QGraphicsView.ScrollHandDrag)
view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
view.setScene(scene)
```
5. 设置QGraphicsView的大小和位置:
```python
view.setGeometry(x, y, width, height) # 参数分别为x、y、width、height
```
这样,QGraphicsItem将会在指定坐标上显示在QGraphicsView中。记得在设置坐标之前,确保已经设置好了QGraphicsView的大小和位置。
qt qgraphicsview 根据两点坐标和弧度画圆弧
要在 Qt 的 QGraphicsView 中画圆弧,可以使用 QGraphicsPathItem 和 QPainterPath 类。下面是一个示例代码,根据两点坐标和弧度画圆弧:
```cpp
#include <math.h>
#include <QPointF>
#include <QPainterPath>
#include <QGraphicsPathItem>
class ArcItem : public QGraphicsPathItem
{
public:
ArcItem(QPointF start, QPointF end, double angle, QGraphicsItem* parent = nullptr)
: QGraphicsPathItem(parent), m_start(start), m_end(end), m_angle(angle)
{
drawArc();
}
private:
void drawArc()
{
QPainterPath path;
QPointF center = getCenter();
qreal radius = getRadius();
qreal startAngle = getStartAngle();
qreal endAngle = getEndAngle();
path.arcTo(center.x() - radius, center.y() - radius, radius * 2, radius * 2, startAngle, endAngle);
setPath(path);
}
QPointF getCenter() const
{
qreal x = (m_start.x() + m_end.x()) / 2;
qreal y = (m_start.y() + m_end.y()) / 2;
return QPointF(x, y);
}
qreal getRadius() const
{
qreal dx = m_start.x() - m_end.x();
qreal dy = m_start.y() - m_end.y();
return sqrt(dx * dx + dy * dy) / 2;
}
qreal getStartAngle() const
{
qreal dx = m_start.x() - getCenter().x();
qreal dy = m_start.y() - getCenter().y();
qreal angle = atan2(dy, dx) * 180 / M_PI;
return angle;
}
qreal getEndAngle() const
{
return m_angle * 180 / M_PI;
}
private:
QPointF m_start;
QPointF m_end;
double m_angle;
};
```
其中,start 和 end 分别是圆弧的起点和终点,angle 是圆弧的弧度。在构造函数中,我们调用 drawArc 方法画出圆弧,它使用 QPainterPath 类来构造圆弧路径。getCenter、getRadius、getStartAngle 和 getEndAngle 方法分别计算圆弧的圆心、半径、起始角度和结束角度。
使用 ArcItem 类时,可以这样调用:
```cpp
QPointF start(0, 0);
QPointF end(100, 0);
double angle = M_PI / 2;
ArcItem* arcItem = new ArcItem(start, end, angle);
scene->addItem(arcItem);
```
这样就可以在 QGraphicsScene 上画出一个起点为 (0, 0)、终点为 (100, 0)、弧度为 pi / 2 的圆弧了。