|
坐标系统 1
GUI 操作系统都有特定的坐标系统
图形界面程序在坐标系中进行窗口和部件的定位
定位类型
- 顶级窗口部件的定位
- 窗口内部件的定位
- 窗口部件的大小设置
坐标系统 2
Qt 使用统一的坐标系统定位窗口部件的位置和大小
Qt 部件类提供成员函数在坐标系统中进行定位
QWidget 类提供了窗口部件所需的坐标系统成员函数
QWidget 类中坐标系统成员函数
- x() // 当前部件的左上角坐标坐标,和frameGeometry.x()相同
- y() // 当前部件的左上角坐标坐标,和frameGeometry.y()相同
- width() // 和geometry() .width() 意义相同
- height() // 和geometry() .heigth() 意义相同
- geometry() // 客户区(不包括标题栏和边框) 左上角的坐标
- - x(),y(),width(),heigth() // x(),y(),客户区(不包括标题栏和边框) 左上角的坐标,,,,width(),heigth() 指的是客户区的长度和高度
- frameGeometry
- - x(),y(),width(),heigth() // x(),y()左上角坐标,width(),heigth()指的是真正窗口的长宽(包括边框和标题栏)
注意:
为什么要提供三组不同的坐标:
为了跨平台,因为不同的窗口式样不同,不同之处体现在边框和标题栏
所以提供了非常详细的函数供调用,因为图形以像素为单位进行定位的
编程实验
Qt 坐标系统初探
#include <QtGui/QApplication>
#include <QPushButton>
#include <QDebug>
#include "MainWindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QPushButton b(&w); // 生成 QPushButton对象, 其父组件为 QWidget
QPushButton b1(&w)
w.resize(100, 100);
w.move(120, 120);
w.show();
qDebug()<<"QWidget:";
qDebug()<<w.x();
qDebug()<<w.y();
qDebug()<<w.width();
qDebug()<<w.height();
qDebug()<<"QWidget::geometry()";
qDebug()<<w.geometry().x();
qDebug()<<w.geometry().y();
qDebug()<<w.geometry().width();
qDebug()<<w.geometry().height();
qDebug()<<"QWidget::frameGeometry()";
qDebug()<<w.frameGeometry().x();
qDebug()<<w.frameGeometry().y();
qDebug()<<w.frameGeometry().width();
qDebug()<<w.frameGeometry().height();
return a.exec();
}
注意事项:
geometry() 和 frameGeometry() 中的稽核数据必须在 show() 调用后才有效
为什么?
因为Qt的目标是跨平台,因为在运行之前不知道会在什么环境下运行,所以Qt在窗口显示出来之前,无法得到真实有效的几何数据
窗口部件的大小设置
QWidget 类提供了成员函数
- 改编窗口部件的大小
- void resize( int w, int h )
- void resize( congst QSize& )
- 改编窗口部件的位置
- void move( int x, int y )
- void move( const QPoint& )
编程实验
窗口子组件的大小和位置
#include <QtGui/QApplication>
#include <QPushButton>
#include <QDebug>
#include "MainWindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QPushButton b(&w); // 生成 QPushButton对象, 其父组件为 QWidget
QPushButton b1(&w);
b.setText("Button"); // 设置显示的字符串
b.move(10, 10); // 移动到坐标 (10, 10)
b.resize(100, 50); // 设置大小 width = 100, height = 25
b1.setText("Button"); // 设置显示的字符串
b1.move(120, 10); // 移动到坐标 (120, 10)
b1.resize(100, 50); // 设置大小 width = 100, height = 25
w.resize(100, 100);
w.move(120, 120);
w.show();
qDebug()<<"QWidget:";
qDebug()<<w.x();
qDebug()<<w.y();
qDebug()<<w.width();
qDebug()<<w.height();
qDebug()<<"QWidget::geometry()";
qDebug()<<w.geometry().x();
qDebug()<<w.geometry().y();
qDebug()<<w.geometry().width();
qDebug()<<w.geometry().height();
qDebug()<<"QWidget::frameGeometry()";
qDebug()<<w.frameGeometry().x();
qDebug()<<w.frameGeometry().y();
qDebug()<<w.frameGeometry().width();
qDebug()<<w.frameGeometry().height();
return a.exec();
}
// 注意:
// 如果设置的操作系统小于操作系统所满足的最小宽度时,这时以操作系统对窗口规定的最小宽度来取代我们设定的值
// 进行开发时,有些时候必须注意操作系统的特性
QPushButton 组件
QPushButton 用于接受用户点击事件
QPushButton 能够显示提示性字符串
QPushButton 是功能组件,需要父组件作为容器
QPushButton 能够在父组件中进行定位
QWidget w; // 生成 QWidget 对象,顶级组件
QPushButton b(&w); // 生成 QPushButton 对象,其父组件为 QWidget
b.setText("Button"); // 设置显示字符串
b.move(10, 10); // 移动到坐标 (10, 10)
b.resize(100, 25); // 设置大小 width = 100, height = 25
总结:
Qt中的几何坐标以左上角为原定
- 水平为 x 轴,从左到右为正向
- 垂直为 y 轴, 从上到下为正向
Qt 中的 GUI 组件以左上角进行定位
Qt 中的 GUI 组件可以在坐标系统中进行大小设置
感谢关注,文章持续高速更新中……