qt setGeometry
时间: 2023-07-09 10:36:15 浏览: 118
在Qt中,setGeometry函数是用来设置控件的位置和大小的。它的函数签名为:
```cpp
void QWidget::setGeometry(int x, int y, int w, int h)
```
其中x、y表示控件的左上角在父窗口中的坐标,w、h表示控件的宽度和高度。可以通过调用该函数来设置控件的位置和大小,例如:
```cpp
QPushButton* button = new QPushButton("Button Text", parentWidget);
button->setGeometry(10, 10, 100, 30);
```
上述代码创建了一个QPushButton控件,并将其添加到parentWidget中。调用setGeometry函数将按钮放在父窗口的(10, 10)位置,并设置宽度为100,高度为30。
需要注意的是,setGeometry函数设置的是控件在父窗口中的位置和大小,如果控件的父窗口发生变化,控件的位置和大小也会发生变化。如果需要相对于控件本身的位置和大小,可以使用setGeometry函数的另外一个重载版本:
```cpp
void QWidget::setGeometry(const QRect& rect)
```
该函数的参数是一个QRect对象,表示控件相对于自己的位置和大小。例如:
```cpp
QPushButton* button = new QPushButton("Button Text", parentWidget);
button->setGeometry(QRect(10, 10, 100, 30));
```
上述代码创建了一个QPushButton控件,并将其添加到parentWidget中。调用setGeometry函数将按钮放在自己的(10, 10)位置,并设置宽度为100,高度为30。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)