0019:Qt常用类 - QRect

1 开发环境

在介绍内容之前,先说明一下开发环境,如下图:
在这里插入图片描述在这里插入图片描述
Qt版本:Qt5.3.2;
Qt开发工具:Qt Creater 3.2.1;
Qt构建工具:Desktop Qt 5.3 MinGW 32bit;
Qt开发平台:Windows 7 64bit。

2 QRect

QRect类代表一个矩形区域,实现在QtCore库,是一个QPoint对象和一个QSize对象的组合,QPoint对象是它的左上角的坐标,QSize对象则是它的大小。首先,将QRect中的常用接口整理出来,分为初始化、获取属性、设置属性、移动、计算、友元函数(操作符重载)共6类,这样方便学习和理解。

//
//初始化
//
QRect();
QRect(const QPoint &topLeft, const QPoint &bottomRight);
QRect(const QPoint &topLeft, const QSize &size);
QRect(int x, int y, int width, int height);
bool             isEmpty() const;                                //w < 0 or  h < 0
bool             isNull() const;                                 //w = 0 and h = 0
bool             isValid() const;                                //w > 0 and h > 0
QRect            normalized() const;                             //调换left和right,或调换top和bottom,使Valid

//
//获取属性
//
int              left() const;
int              right() const;
int              top() const;
int              bottom() const;
QPoint           topLeft() const;
QPoint           topRight() const;
QPoint           bottomLeft() const;
QPoint           bottomRight() const;
QPoint           center() const;
int              x() const;
int              y() const;
int              width() const;
int              height() const;
QSize            size() const;
void             getCoords(int *x1, int *y1, int *x2, int *y2) const;
void             getRect(int *x, int *y, int *width, int *height) const;

//
//设置属性
//
void             setLeft(int x);
void             setRight(int x);
void             setTop(int y);
void             setBottom(int y);
void             setCoords(int x1, int y1, int x2, int y2);
void             setTopLeft(const QPoint &position);
void             setTopRight(const QPoint &position);
void             setBottomLeft(const QPoint &position);
void             setBottomRight(const QPoint &position);
void             setX(int x);
void             setY(int y);
void             setWidth(int width);
void             setHeight(int height);
void             setSize(const QSize &size);
void             setRect(int x, int y, int width, int height);

//
//移动
//
void             moveLeft(int x);                                //左边    移动到x
void             moveRight(int x);                               //右边    移动到x
void             moveTop(int y);                                 //上边    移动到y
void             moveBottom(int y);                              //下边    移动到y
void             moveTopLeft(const QPoint &position);            //左上角  移动到position
void             moveTopRight(const QPoint &position);           //右上角  移动到position
void             moveBottomLeft(const QPoint &position);         //左下角  移动到position
void             moveBottomRight(const QPoint &position);        //右下角  移动到position
void             moveCenter(const QPoint &position);             //中心点  移动到position
void             moveTo(int x, int y);                           //左上角  移动到(x, y)
void             moveTo(const QPoint &position);                 //左上角  移动到(x, y)
void             translate(int dx, int dy);                      //Rect    偏移量(dx, dy), 改变自身
void             translate(const QPoint &offset);                //Rect    偏移量(offset.x(), offset.y()), 改变自身
QRect            translated(int dx, int dy) const;               //Rect    偏移量(dx, dy), 不改变自身
QRect            translated(const QPoint &offset) const;         //Rect    偏移量(offset.x(), offset.y()), 不改变自身

//
//计算
//
void             adjust(int dx1, int dy1, int dx2, int dy2);     //调整Rect, 参数分别对应l/t/r/b的偏移量, 改变自身
QRect            adjusted(int dx1, int dy1, int dx2, int dy2) const;//调整Rect, 参数分别对应l/t/r/b的偏移量, 不改变自身
QRect            marginsAdded(const QMargins &margins) const;    //扩展Rect, 不改变自身
QRect            marginsRemoved(const QMargins &margins) const;  //收缩Rect, 不改变自身
QRect &          operator+=(const QMargins &margins);            //同marginsAdded
QRect &          operator-=(const QMargins &margins);            //同marginsReomved
bool             contains(int x, int y, bool proper) const;      //proper: true - 不包含边界, false - 包含边界
bool             contains(const QPoint &rectangle, bool proper = false) const;//proper: 同上
bool             contains(int x, int y) const;                   //proper is true
bool             contains(const QRect &rectangle, bool proper = false) const;

//
//友元函数:操作符重载
//
bool             operator!=(const QRect &r1, const QRect &r2);
bool             operator==(const QRect &r1, const QRect &r2);
QRect            operator+(const QMargins &margins, const QRect &rectangle);//同marginsAdded
QRect            operator+(const QRect &rectangle, const QMargins &margins);//同marginsAdded
QRect            operator-(const QRect &lhs, const QMargins &rhs);//同marginsRemoved

下面,是上述接口的使用例程。

//
//初始化
//
//QRect();
//QRect(const QPoint &topLeft, const QPoint &bottomRight);
//QRect(const QPoint &topLeft, const QSize &size);
//QRect(int x, int y, int width, int height);
//bool             isEmpty() const;                                //w <= 0  or   h <= 0
//bool             isNull() const;                                 //w =  0  and  h = 0
//bool             isValid() const;                                //w >  0  and  h > 0
/*
 * null rect is: l = 0, r = -1, t = 0, b = -1, w = 0, h = 0.
 * 判断Rect是否可用:
 * if (rect.isValid())
 * {
 *     //w > 0 and h > 0, 所以rect是可用的...
 * }
 */
QRect rcNull;
qDebug("rcNull: l = %d, r = %d, t = %d, b = %d, w = %d, h = %d.",
    rcNull.left(), rcNull.right(), rcNull.top(), rcNull.bottom(), rcNull.width(), rcNull.height());
qDebug("rcNull is null? %s.", rcNull.isNull() ? "yes" : "no");
qDebug("rcNull is empty? %s.", rcNull.isEmpty() ? "yes" : "no");
QRect rcEmpty1(0, 0, 0, 10), rcEmpty2(0, 0, -1, 10);
qDebug("rcEmpty1 is empty? %s.", rcEmpty1.isEmpty() ? "yes" : "no");
qDebug("rcEmpty2 is empty? %s.", rcEmpty2.isEmpty() ? "yes" : "no");

//
//获取属性
//
//int              left() const;
//int              right() const;
//int              top() const;
//int              bottom() const;
//QPoint           topLeft() const;
//QPoint           topRight() const;
//QPoint           bottomLeft() const;
//QPoint           bottomRight() const;
//QPoint           center() const;
//int              x() const;
//int              y() const;
//int              width() const;
//int              height() const;
//QSize            size() const;
//void             getCoords(int *x1, int *y1, int *x2, int *y2) const;
//void             getRect(int *x, int *y, int *width, int *height) const;
qDebug("QRect的获取属性类的接口,很好理解...");

//
//设置属性
//
//void             setLeft(int x);
//void             setRight(int x);
//void             setTop(int y);
//void             setBottom(int y);
//void             setCoords(int x1, int y1, int x2, int y2);
//void             setTopLeft(const QPoint &position);
//void             setTopRight(const QPoint &position);
//void             setBottomLeft(const QPoint &position);
//void             setBottomRight(const QPoint &position);
//void             setX(int x);
//void             setY(int y);
//void             setWidth(int width);
//void             setHeight(int height);
//void             setSize(const QSize &size);
//void             setRect(int x, int y, int width, int height);
qDebug("QRect的设置属性类的接口,很好理解...");

//
//移动
//
//void             moveLeft(int x);
//void             moveRight(int x);
//void             moveTop(int y);
//void             moveBottom(int y);
//void             moveTopLeft(const QPoint &position);
//void             moveTopRight(const QPoint &position);
//void             moveBottomLeft(const QPoint &position);
//void             moveBottomRight(const QPoint &position);
//void             moveCenter(const QPoint &position);
//void             moveTo(int x, int y);
//void             moveTo(const QPoint &position);
//void             translate(int dx, int dy);
//void             translate(const QPoint &offset);
//QRect            translated(int dx, int dy) const;
//QRect            translated(const QPoint &offset) const;
QRect rcMv(QPoint(0, 0), QPoint(10, 10));
qDebug("rcMv: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d, c = (%04d, %04d).",
    rcMv.left(), rcMv.right(), rcMv.top(), rcMv.bottom(), rcMv.width(), rcMv.height(),
    rcMv.center().x(), rcMv.center().y());
QRect rcML = rcMv;
rcML.moveLeft(-10);
qDebug("rcML: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcML.left(), rcML.right(), rcML.top(), rcML.bottom(), rcML.width(), rcML.height());
QRect rcMR = rcMv;
rcMR.moveRight(20);
qDebug("rcMR: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcMR.left(), rcMR.right(), rcMR.top(), rcMR.bottom(), rcMR.width(), rcMR.height());
QRect rcMT = rcMv;
rcMT.moveTop(-10);
qDebug("rcMT: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcMT.left(), rcMT.right(), rcMT.top(), rcMT.bottom(), rcMT.width(), rcMT.height());
QRect rcMB = rcMv;
rcMB.moveBottom(20);
qDebug("rcMB: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcMB.left(), rcMB.right(), rcMB.top(), rcMB.bottom(), rcMB.width(), rcMB.height());
QRect rcTL = rcMv;
rcTL.moveTopLeft(QPoint(-10, -10));
qDebug("rcTL: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcTL.left(), rcTL.right(), rcTL.top(), rcTL.bottom(), rcTL.width(), rcTL.height());
QRect rcTR = rcMv;
rcTR.moveTopRight(QPoint(20, -10));
qDebug("rcTR: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcTR.left(), rcTR.right(), rcTR.top(), rcTR.bottom(), rcTR.width(), rcTR.height());
QRect rcBL = rcMv;
rcBL.moveBottomLeft(QPoint(-10, 20));
qDebug("rcBL: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcBL.left(), rcBL.right(), rcBL.top(), rcBL.bottom(), rcBL.width(), rcBL.height());
QRect rcBR = rcMv;
rcBR.moveBottomRight(QPoint(20, 20));
qDebug("rcBR: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcBR.left(), rcBR.right(), rcBR.top(), rcBR.bottom(), rcBR.width(), rcBR.height());
QRect rcMc = rcMv;
rcMc.moveCenter(QPoint(10, 10));
qDebug("rcMc: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d, c = (%04d, %04d).",
    rcMc.left(), rcMc.right(), rcMc.top(), rcMc.bottom(), rcMc.width(), rcMc.height(),
    rcMc.center().x(), rcMc.center().y());
QRect rcTo = rcMv;
rcTo.moveTo(QPoint(-10, -10));
qDebug("rcTo: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcTo.left(), rcTo.right(), rcTo.top(), rcTo.bottom(), rcTo.width(), rcTo.height());
QRect rcTr = rcMv;
rcTr.translate(QPoint(-10, -10));
qDebug("rcTr: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcTr.left(), rcTr.right(), rcTr.top(), rcTr.bottom(), rcTr.width(), rcTr.height());

//
//计算
//
//void             adjust(int dx1, int dy1, int dx2, int dy2);
//QRect            adjusted(int dx1, int dy1, int dx2, int dy2) const;
//QRect            marginsAdded(const QMargins &margins) const;
//QRect            marginsRemoved(const QMargins &margins) const;
//QRect &          operator+=(const QMargins &margins);
//QRect &          operator-=(const QMargins &margins);
//bool             contains(int x, int y, bool proper) const;
//bool             contains(const QPoint &rectangle, bool proper = false) const;
//bool             contains(int x, int y) const;
//bool             contains(const QRect &rectangle, bool proper = false) const;
QRect rcCacu(0, 0, 11, 11);
qDebug("rcCacu: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcCacu.left(), rcCacu.right(), rcCacu.top(), rcCacu.bottom(), rcCacu.width(), rcCacu.height());
QRect rcAjst = rcCacu;
rcAjst.adjust(-10, -10, 10, 10);
qDebug("rcAjst: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcAjst.left(), rcAjst.right(), rcAjst.top(), rcAjst.bottom(), rcAjst.width(), rcAjst.height());
QRect rcMAdd = rcCacu.marginsAdded(QMargins(2, 2, 2, 2));
qDebug("rcMAdd: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcMAdd.left(), rcMAdd.right(), rcMAdd.top(), rcMAdd.bottom(), rcMAdd.width(), rcMAdd.height());
QRect rcMRmv = rcCacu.marginsRemoved(QMargins(2, 2, 2, 2));
qDebug("rcMRmv: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcMRmv.left(), rcMRmv.right(), rcMRmv.top(), rcMRmv.bottom(), rcMRmv.width(), rcMRmv.height());
QRect rcOAdd = rcCacu;
rcOAdd += QMargins(2, 2, 2, 2);
qDebug("rcOAdd: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcOAdd.left(), rcOAdd.right(), rcOAdd.top(), rcOAdd.bottom(), rcOAdd.width(), rcOAdd.height());
QRect rcOMns = rcCacu;
rcOMns -= QMargins(2, 2, 2, 2);
qDebug("rcOMns: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcOMns.left(), rcOMns.right(), rcOMns.top(), rcOMns.bottom(), rcOMns.width(), rcOMns.height());
QPoint ptCenter = rcCacu.center();
qDebug("ptCenter of rcCacu is in rcCacu? %s.", rcCacu.contains(ptCenter, false) ? "yes" : "no");
QPoint ptTL = rcCacu.topLeft();
qDebug("ptTL of rcCacu is in rcCacu(include border)? %s.", rcCacu.contains(ptTL, false) ? "yes" : "no");
QRect rcSame = rcCacu;
qDebug("rcSame of rcCacu is in rcCacu(include border)? %s.", rcCacu.contains(rcSame, false) ? "yes" : "no");

//
//友元函数:操作符重载
//
//bool             operator!=(const QRect &r1, const QRect &r2);
//bool             operator==(const QRect &r1, const QRect &r2);
//QRect            operator+(const QMargins &margins, const QRect &rectangle);
//QRect            operator+(const QRect &rectangle, const QMargins &margins);
//QRect            operator-(const QRect &lhs, const QMargins &rhs);
QRect rcFriend(QPoint(0, 0), QPoint(10, 10));
QRect rcCompare = rcFriend;
qDebug("rcCompare == rcFriend? %s.", rcCompare == rcFriend ? "yes" : "no");
qDebug("rcCompare != rcFriend? %s.", rcCompare != rcFriend ? "yes" : "no");
QRect rcOA = rcFriend + QMargins(2, 2, 2, 2);
qDebug("rcOA: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcOA.left(), rcOA.right(), rcOA.top(), rcOA.bottom(), rcOA.width(), rcOA.height());
QRect rcOM = rcFriend - QMargins(2, 2, 2, 2);
qDebug("rcOM: l = %04d, r = %04d, t = %04d, b = %04d, w = %04d, h = %04d.",
    rcOM.left(), rcOM.right(), rcOM.top(), rcOM.bottom(), rcOM.width(), rcOM.height());

在学习过程中,可以将上述代码放到一个按钮的响应函数中,以调试方式运行,就可以在Qt Creater中的应用程序输出窗口看到输出结果了。

  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值