警惕QRect::top()函数,以及QSize

目录

正常的情形(height > 0)

“反常的”情形(height < 0)


正常的情形(height > 0)

在前面的博客《自己对QLineF::angle()的理解 》里,我提到了QWidget的Y轴指向向下。这样的情况同样发生在QRect等几何形状上。QRect::top()的字面意思是矩形上沿的纵坐标。我们往往会认为其返回值等于矩形四个顶点里纵坐标最大的那个值。然而,由于Qt对Y方向的规定,当QRect::height()为正时,top返回的其实是较小的值。所谓的“正常情形”,指的是height > 0. QPolygon::boundingRect()函数返回的就是这种正常的矩形。来看下面的例子:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRect>
#include <QPolygon>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QPoint pnt1(ui->spinBox->value(), ui->spinBox_2->value()),
            pnt2(ui->spinBox_3->value(), ui->spinBox_4->value()),
            pnt3(ui->spinBox_5->value(), ui->spinBox_6->value()),
            pnt4(ui->spinBox_7->value(), ui->spinBox_8->value());

    QPolygon plgn;
    plgn<<pnt1<<pnt2<<pnt3<<pnt4;

    QRect rct = plgn.boundingRect();

    int i = rct.top();
    ui->spinBox_9->setValue(i);
}

效果:

对于由(0,0), (0,1), (1,0), (1,1)围成的矩形,top = 0

对于由(0,-1), (0,1), (1,-1), (1,1)围成的矩形,top = -1

可见,top返回的是Y方向较小的点的纵坐标。

 

沿着这个思路走下去,考虑如何运用QRect的构造函数。QRect有多种构造函数,其中一种的语法是这样的

QRect(QPoint, QSize())

qSize::height 的含义值得注意。当height为正时,说明QRect::bottom > QRect:top

来看下面的代码:

可见,当height为正时,说明QRect::bottom > QRect:top

另外,有人注意到bottom == 9, height == 9而不是10.QT的文档对此给出了解释:

 

int QRect::bottom() const

Returns the y-coordinate of the rectangle's bottom edge.

Note that for historical reasons this function returns top() + height() - 1; use y() + height() to retrieve the true y-coordinate.

 

“反常的”情形(height < 0)

根据qt的文档,setHeight()函数除了更新height()之外,还会更新bottom()。但是top()是不会被更新的。同样,setBottom()函数只会引起bottom和height的变化,不会更新top。

总的来说,只要不调用setTop,setLeft, setTopLeft, topleft的位置是不变的。

看下面的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRect>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
     int iLeft = ui->sb1->value(), iWidth = ui->sb3->value(),
             iTop = ui->sb2->value(), iHeight = ui->sb4->value();
     QRect rct;
     rct.setLeft(iLeft);
     rct.setWidth(iWidth);
     rct.setTop(iTop);
     rct.setHeight(iHeight);

     QString qstr = QString("left = %1, top = %2, right = %3, bottom = %4, width = %5, height = %6")
             .arg(rct.left()).arg(rct.top()).arg(rct.right()).arg(rct.bottom()).arg(rct.width()).arg(rct.height());
     ui->lineEdit->setText(qstr);

}

向spinBox输入数值,然后计算矩形的6个属性,并在底下的文本框中打印:

 

再看下面的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRect>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
     int iLeft = ui->sb1->value(), iRight = ui->sb3->value(),
             iTop = ui->sb2->value(), iBottom = ui->sb4->value();
     QRect rct;
     rct.setLeft(iLeft);
     rct.setRight(iRight);
     rct.setTop(iTop);
     rct.setBottom(iBottom);

     QString qstr = QString("left = %1, top = %2, right = %3, bottom = %4, width = %5, height = %6")
             .arg(rct.left()).arg(rct.top()).arg(rct.right()).arg(rct.bottom()).arg(rct.width()).arg(rct.height());
     ui->lineEdit->setText(qstr);

}

 

 

总之,只要不调用setTop,setLeft, setTopLeft, topleft的位置是不变的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值