Qt——窗口部件及窗口类型、坐标系统

1.QWidget类继承QObject和QPaintDevice类,是所有用户界面组件的父类

  • QObject是所有支持Qt对象模型的基类
  • QPaintDevice是Qt中所有可绘制组件的基类

QWidget的功能:

  • QWidget能够绘制自己和处理用户的输入

  • QWidget是Qt中所有窗口组件类的父类

  • QWidget是所有窗口组件的抽象

  • Qt中的每个窗口组件都是一个QWidget

  • QWidget类对象常作为父组件或顶级组件使用

    #include "Widget.h"

    #include

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QWidget w;

    复制代码
      w.setWindowTitle("Hello World"); //设置窗口标题 
      w.show();
      return QCoreApplication::exec();

    }

运行结果:

由于上面的QWidget w 对象没有父组件,所以QWidget w 便成为了没有父组件的顶级组件,从而生成了窗口,自带标题栏、最大化、最小化以及关闭功能

2.功能类QLabel组件

  • 用于显示一个提示性的字符串

  • 是功能性组件,一般需要父组件作为容器

  • QLabel可以作为窗口存在,但没什么意义

    #include "Widget.h"

    #include
    #include
    #include

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QWidget w;
    QLabel l(&w);
    l.setText("标签");

    复制代码
      w.setWindowTitle("Hello World"); //设置窗口标题
      w.show();
      return QCoreApplication::exec();

    }

运行结果:

若将QLabel设置为顶级组件,独立存在,将拥有一个包含自己的窗口,因为QLabel继承了QWidget,所有拥有QWidget的所有成员函数,但这种做法通常没有意义

复制代码
#include "Widget.h"

#include <QtGui>
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // QWidget w;
    QLabel l ;
    l.setText("标签");

    l.setWindowTitle("Hello World"); //设置窗口标题
    l.show();
    return QCoreApplication::exec();
}

运行结果:

3.Qt中可以根据需要定制窗口样式

窗口类型:

  • Qt::Dialog------对话框类型
  • Qt::Window------主窗口类型
  • Qt::SplashScreen------启动画面类型

窗口标志:

  • Qt::WindowStaysOnTopHint ------窗口始终在最顶层

  • Qt::WindowContextHelpButtonHint ------最大化最小化按钮变为帮助按钮

    #include "Widget.h"

    #include
    #include
    #include

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    // QWidget w(NULL, Qt::Dialog); //对话框的顶级组件的样式,第一个参数是父类,为空,所以是顶级组件
    // QWidget w(NULL, Qt::Window); //窗口类型的顶级组件的样式
    // QWidget w(NULL, Qt::SplashScreen); //启动画面类型的顶级组件的样式,通常做欢迎界面

    复制代码
      //QWidget w(NULL, Qt::Window|Qt::WindowStaysOnTopHint); //窗口始终在最顶层
      QWidget w(NULL, Qt::Window|Qt::WindowContextHelpButtonHint); //最大化最小化按钮变为帮助按钮
      w.resize(400,300); //改变窗口大小
      QLabel l(&w);
      l.setText("标签");
    
      w.setWindowTitle("Hello World"); //设置窗口标题
      w.show();
      return QCoreApplication::exec();

    }

4.坐标系统

  • GUI操作系统都拥有特定的坐标系统
  • 图形界面程序在坐标系统中进行窗口和部件的定位
  • 定位类型:顶级窗口部件的定位、窗口内部件的定位、窗口部件的大小设置

QWidget类中的坐标系统成员函数

  • x()
  • y()
  • width()
  • height()
  • geometry():x()、y()、width()、height()
  • frameGeometry():x()、y()、width()、height()
复制代码
#include "Widget.h"
#include <QtGui>
#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Widget w;
    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 QCoreApplication::exec();
}

注意:geometry()和frameGeometry()中的几何数据必须在show()调用后才有效!因为QT是跨平台的,在不同平台对应的窗口位置不一样

5.窗口部件的大小设置

QWidget类提供了成员函数:

  • 改变窗口部件的大小:void resize(int w,int h) void resize(const QSize &)

  • 改变窗口部件的位置:void move(int x,int y) void move(const QPoint &)

    #include "Widget.h"
    #include
    #include
    #include

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    复制代码
      Widget w;
      w.resize(300,200);
      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 QCoreApplication::exec();

    }

运行结果:

QWidget:

120

120

300

200

QWidget::geometry()

121

158

300

200

QWidget::frameGeometry()

120

120

302

239

5.QPushButton组件

  • 用于接收用户点击事件

  • 能够显示提示性字符串

  • 是功能性组件,需要父组件作为容器

  • 能够在父组件中进行定位

    #include "Widget.h"
    #include
    #include
    #include
    #include

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Widget w;
    QPushButton b1(&w); //生成QPushButton对象,其父组件是QWidget
    b1.setText("Button1"); //设置显示的字符串
    b1.move(10,10); //移动到坐标(10,10)
    b1.resize(100,25); //设置宽和高

    复制代码
      QPushButton b2(&w); //生成QPushButton对象,其父组件是QWidget
      b2.setText("Button2"); //设置显示的字符串
      b2.move(120,10); //移动到坐标(10,10)
      b2.resize(100,25); //设置宽和高
      w.resize(300,200);
      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 QCoreApplication::exec();

    }

相关推荐
xcyxiner13 小时前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner1 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner2 天前
DicomViewer (添加模型类)3
qt
xcyxiner2 天前
DicomViewer (目录调整) 2
qt
xcyxiner2 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR0064 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术4 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园4 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob4 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享4 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm