geometry() : " <<_qwidget::move">
当前位置:   article > 正文

QWidget::move的偏移问题

qwidget::move

move设置位置但是不能到正确设置,但是用setGeometry是可以的

move设置位置是(0 , 0),但是实际位置为(10,42)

  1. void fixSizeWidget::on_pushButton_clicked()
  2. {
  3. QRect rect = QRect(0 , 0 , 1028 , 720);
  4. qDebug() << Q_FUNC_INFO << __LINE__ << "this->geometry() : " << this->geometry();
  5. qDebug() << Q_FUNC_INFO << __LINE__ << "rect.topLeft()" << rect.topLeft();
  6. this->move(rect.topLeft());
  7. qDebug() << Q_FUNC_INFO << __LINE__ << "this->geometry() : " << this->geometry();
  8. qDebug() << Q_FUNC_INFO << __LINE__ << "rect.topLeft()" << rect.topLeft();
  9. this->setFixedSize( 1028 , 720);
  10. qDebug() << Q_FUNC_INFO << __LINE__ << "this->geometry() : " << this->geometry();
  11. qDebug() << Q_FUNC_INFO << __LINE__ << "rect.topLeft()" << rect.topLeft();
  12. this->setGeometry(rect);
  13. qDebug() << Q_FUNC_INFO << __LINE__ << "this->geometry() : " << this->geometry();
  14. }

 打印的信息

void fixSizeWidget::on_pushButton_clicked() 20 this->geometry() : QRect(760,362 400x300)

void fixSizeWidget::on_pushButton_clicked() 21 rect.topLeft() QPoint(0,0)

void fixSizeWidget::on_pushButton_clicked() 23 this->geometry() : QRect(10,42 400x300)

void fixSizeWidget::on_pushButton_clicked() 24 rect.topLeft() QPoint(0,0)

void fixSizeWidget::on_pushButton_clicked() 26 this->geometry() : QRect(10,42 1028x720)

void fixSizeWidget::on_pushButton_clicked() 27 rect.topLeft() QPoint(0,0)

void fixSizeWidget::on_pushButton_clicked() 30 this->geometry() : QRect(0,0 1028x720)

 找到原因了,因为使用move的时候会包括边框的偏移,move的官方文档

If the widget is a window, the position is that of the widget on the desktop, including its frame.

而setGeometry不包括边框,setGeometry的官方文档 

This property holds the geometry of the widget relative to its parent and excluding the window frame

这里所说的边框是frameGeometry和Geometry之间,如下图所示

文献参考:

Qt绘图:Window and Dialog Widgets 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号