QRect
的表示很独特, 存放的是左上角坐标和右下角坐标 (准确地说, 是 int x1; int y1; int x2; int y2;
). 对 QRect
取上下左右都是闭区间上的边界, 比如QRect rect(QPoint(0, 2), QSize(8, 8));
rect.left(); // 0
rect.right(); // 7 : 0 + 8 - 1
rect.top(); // 2
rect.bottom(); // 9 : 2 + 8 - 1
list = range(0, 8)
list[0: len(list) / 2] // [0, 1, 2, 3]
list[len(list) / 2: len(list)] // [4, 5, 6, 7]
QRect
"切片", 求一个矩形的左半边和右半边, 代码则看起来像QRect rect(QPoint(0, 2), QSize(8, 8));
QRect leftHalf(rect);
leftHalf.setRight(leftHalf.left() + leftHalf.width() / 2 - 1);
QRect rightHalf(rect);
rightHalf.setLeft(rightHalf.left() + rightHalf.width() / 2);
至于 Nokia 的开发者们自己是不是搞清楚了
QRect
我也不太清楚, 不过 QPainter::drawRect(QRect const&)
这个函数是看起来有 bug 的样子, 比如下面这个例子#include <QApplication>
#include <QToolButton>
#include <QBrush>
#include <QPainter>
struct TestButton
: public QToolButton
{
void paintEvent(QPaintEvent*)
{
QPainter painter(this);
painter.setBrush(Qt::NoBrush);
painter.setPen(QPen(Qt::white, 1));
painter.drawRect(rect().adjusted(3, 3, -3, -3));
painter.setPen(QPen(Qt::blue, 1));
painter.drawRect(rect().adjusted(2, 2, -2, -2));
painter.setPen(QPen(Qt::green, 1));
painter.drawRect(rect().adjusted(1, 1, -1, -1));
painter.setPen(QPen(Qt::red, 1));
painter.drawRect(rect());
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
TestButton button;
button.show();
return app.exec();
}