QRect的width()为什么返回的是right-left+1而不是right-left?

3x3只眼 2010-07-06 07:04:21
我以前用的openCV和C#中的矩形的width都是直接用right-left,而这段时间在使用Qt的时候在将cvRect转换为QRect时总是遇到差1的问题,后来查了一下Qt的源码,才发现Qt的width是right-left+1,并且Qt的文档中也有以下描述:
“返回矩形的宽度。宽度包括左和右边界,比如width = right - left + 1”

我觉得很奇怪,屏幕坐标系中(0,0)不是原点吗?那么(1,0)与原点的距离就是一个像素点的宽啊,如果用Qt的计算方法岂不宽为2了?这个2代表什么含义?

希望Qt高手现身,谢谢~_~
...全文
400 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
中才德创 2010-07-07
  • 打赏
  • 举报
回复
一个像素点的宽的问题,我也遇到,但不太明白根因。
富莱工作室 2010-07-07
  • 打赏
  • 举报
回复
我觉得好像没什么问题呀

就好像苹果(0,0),苹果(1,0), 宽度自然应该是2苹果才对,:)
tzcherish 2010-07-07
  • 打赏
  • 举报
回复
int QRect::right () const

Returns the x-coordinate of the rectangle's right edge.

Note that for historical reasons this function returns left() + width() - 1; use x() + width() to retrieve the true x-coordinate.
3x3只眼 2010-07-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tzcherish 的回复:]
int QRect::right () const

Returns the x-coordinate of the rectangle's right edge.

Note that for historical reasons this function returns left() + width() - 1; use x() + width() to retrieve the t……
[/Quote]

感谢大家的帮助,这个问题困扰我很久了,终于看到比较可信的说法了~_~
3x3只眼 2010-07-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 darkdong 的回复:]
width没错,错的是right,qt的文档说:
Note that for historical reasons the values returned by the bottom() and right() functions deviate from the true bottom-right corner of the rectangle: The right() function re……
[/Quote]

原来如此!受教了~_~
巴依老爷 2010-07-06
  • 打赏
  • 举报
回复
width没错,错的是right,qt的文档说:
Note that for historical reasons the values returned by the bottom() and right() functions deviate from the true bottom-right corner of the rectangle: The right() function returns left() + width() - 1 and the bottom() function returns top() + height() - 1.
We recommend that you use x() + width() and y() + height() to find the true bottom-right corner, and avoid right() and bottom(). Another solution is to use QRectF
3x3只眼 2010-07-06
  • 打赏
  • 举报
回复
自己顶一下,求解!
中国象棋的C++代码 #include "chess_zn.h" QTcpSocket * Chess_ZN::client = new QTcpSocket; QUndoStack * Chess_ZN::undoStack = new QUndoStack(); int Chess_ZN::second = 120; bool Chess_ZN::isTurn = false; Chess_ZN::Chess_ZN(QWidget *parent) : QWidget(parent) { init(); initElse(); } void Chess_ZN::initElse(){ treeitem = 1; timer=new QTimer; portmap=0; isConn = true; start = false; isTimer = false; isSearch = false; connect(timer,SIGNAL(timeout()),this,SLOT(stopWatch())); connect(wigettree[1],SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(getInfo(QTreeWidgetItem*))); connect(wigettree[0],SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(connectToHost_PK(QTreeWidgetItem*))); connect(client,SIGNAL(connected()),this,SLOT(connected())); //连接一旦断开 connect(client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError ))); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); peer = new PeerManager(this); peer->setServerPort(10001); items=wigettree[1]->currentItem(); item_pk=wigettree[0]->currentItem(); item_pk_info=wigettree[0]->currentItem(); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[8], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[9], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[10], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[11], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[0], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[1], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[2], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[3], SLOT(setEnabled(bool))); timer->start(1000); createUndoView(); isChoose = true; tableeditor=new TableEditor("users"); } void Chess_ZN::createUndoView() { undoVie

16,203

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧