QT开发中文网

C++如何设置QGraphicsRectItem的坐标远点为左下角

默认情况下,QGraphicsRectItem的坐标原点为左上角。如果需要将其设置为左下角,可以通过以下两种方式实现:

  1. 重写QGraphicsRectItem的boundingRect()函数,调整矩形的y轴坐标:
class MyRectItem : public QGraphicsRectItem {
public:
    MyRectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = nullptr)
        : QGraphicsRectItem(x, y, width, height, parent) {}

    QRectF boundingRect() const override {
        return QRectF(rect().x(), -rect().y(), rect().width(), rect().height());
    }
};
  1. 在QGraphicsView中设置坐标系原点:
QGraphicsView view;
view.setRenderHint(QPainter::Antialiasing); // 抗锯齿
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 关闭垂直滚动条
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 关闭水平滚动条
view.setDragMode(QGraphicsView::ScrollHandDrag); // 拖拽模式

QGraphicsScene scene;
view.setScene(&scene);

// 设置坐标系原点为左下角
scene.setSceneRect(QRectF(0, -view.height(), view.width(), view.height()));

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=89

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?