qt设置位置
时间: 2023-07-13 07:18:43 浏览: 44
在 Qt 中,可以使用 QWidget 类的 move() 函数来设置窗口的位置。move() 函数需要传入两个参数,分别是窗口的 x 和 y 坐标。例如,以下代码可以将窗口移动到屏幕左上角:
```cpp
QWidget *widget = new QWidget();
widget->move(0, 0);
```
另外,如果要将窗口移动到屏幕的中心位置,可以使用以下代码:
```cpp
QDesktopWidget *desktop = QApplication::desktop();
int screenWidth = desktop->width();
int screenHeight = desktop->height();
int windowWidth = widget->width();
int windowHeight = widget->height();
int x = (screenWidth - windowWidth) / 2;
int y = (screenHeight - windowHeight) / 2;
widget->move(x, y);
```
相关问题
qt widget设置位置
在 Qt 中,可以使用 `QWidget` 的 `move()` 函数来设置窗口部件(widget)的位置。`move()` 函数接受两个参数,分别为窗口部件的左上角的 x 坐标和 y 坐标,如下所示:
```python
widget.move(x, y)
```
其中,`widget` 是一个 QWidget 类型的对象,`x` 和 `y` 分别为窗口部件的左上角的 x 坐标和 y 坐标。
另外,也可以使用 `QWidget` 的 `setGeometry()` 函数来设置窗口部件的位置和大小。`setGeometry()` 函数接受四个参数,分别为窗口部件的左上角的 x 坐标、y 坐标、宽度和高度,如下所示:
```python
widget.setGeometry(x, y, width, height)
```
其中,`widget` 是一个 QWidget 类型的对象,`x` 和 `y` 分别为窗口部件的左上角的 x 坐标和 y 坐标,`width` 和 `height` 分别为窗口部件的宽度和高度。这种方法可以同时设置窗口部件的位置和大小。
python qt 设置控件位置
在 Python Qt 中设置控件位置可以使用 `setGeometry()` 方法。这个方法需要传入四个参数,分别是控件的 x 坐标、y 坐标、宽度和高度。
以下是一个例子,将一个按钮移动到窗口的中心位置:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn = QPushButton('Button', self)
btn.resize(btn.sizeHint())
btn.move(100, 100)
# 计算窗口的中心位置
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Example')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在上面的例子中,使用 `move()` 方法将按钮移动到 (100, 100) 的位置。然后计算窗口的中心位置,并使用 `move()` 方法将窗口移动到中心位置。
注意,`move()` 方法和 `setGeometry()` 方法都是 QWidget 类的方法。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)