自学内容网 自学内容网

day01-Qt5入门

day01-Qt5入门

窗体应用

1.1 窗体基类说明

创建项目在details中编辑器提供了三个基类,分别是

QMainWindows、Qwidget、QDialog

1、 QMainWindow

QMainWindow 类提供一个有菜单条、锚接窗口(例如工具条)和一个状态条的主应用 程序窗口。主窗口通常用在提供一个大的中央窗口部件(例如文本编辑或者绘制画布)以及 周 围 菜单、工具条和一个状态条。QMainWindow 常常被继承,因为这使得封装中央部件、菜 单和工具条以及窗口状态条变得更容易,当用户点击菜单项或者工 具条按钮时,槽会被调 用。

2、 QWidget

QWidgt 类是所有用户界面对象的基类。 窗口部件是用户界面的一个基本单元:它从窗 口系统接收鼠标、键盘和其它事件,并且在屏幕上绘制自己。每一个窗口部件都是矩形的, 并且它们按 Z 轴顺序排列。一个窗口部件可以被它的父窗口部件或者它前面的窗口部件盖住 一部分。

3、 QDialog

QDialog 类是对话框窗口的基类。对话框窗口是主要用于短期任务以及和用户进行简要 通讯的顶级窗口。QDialog 可以是模态对话框也可以是非模态对话框。QDialog 支持扩展性并且可以提供返回值。它们可以有默认按钮。

1.2 控制窗体大小

如控制窗体不可更改大,最大、最小为 400x300。

在这里插入图片描述

运行后生成一个窗体不可更改大小

1.3 窗体初始位置及背景色

  • 控制窗体在屏幕右上角 X 轴 100,Y 轴 100 显示。 背景色为红色。
    //默认窗体居中显示,如果想要更改用 move 或 setGeometry
    this->move(100,100);
    //修改样式
    this->setStyleSheet("background:red");

在这里插入图片描述

1.4 修改标题栏图标

1.添加资源文件(图标)

Qt Creator – 文件 – 新建文件或项目 – 文件和类中选择 Qt – Qt 资源文件

-给资源文件起个名字如images

在这里插入图片描述

此时自动生成文件夹Resources

单击 image.qrc 文件 – 右键[open in edit] –在单击 [add prefix]-输入前缀后选择-点击[add files]-添加你的图片文件-在别名处输入-ctrl+s进行保存

在这里插入图片描述

最后在 mainwindow.cpp 中添加代码。

 //窗体ICO图片设置,如图不起别名,后缀直接写图片全名。
    this->setWindowIcon(QIcon(":/image/icon"));
  • 注意资源目录要对应

    格式为::/前缀.../别名

点击运行后,应用的图标则变为你指定的文件。

1.5移动无边框窗体

第一步:打开 mainwindow.h 头文件,添加代码。

#include <QMouseEvent> //引用鼠标类头文件
#include <QPushButton> //引用按钮类头文件

在mainWindow中添加鼠标的3个方法,并添加Botton对象和鼠标指针对象。

protected:
    //鼠标按下
    void mousePressEvent(QMouseEvent *e);
    //鼠标移动
    void mouseMoveEvent(QMouseEvent *e);
    //鼠标释放
    void mouseReleaseEvent(QMouseEvent *e);
    //定义 QPoint 对象

//定义 QPoint 对象
private:
    QPushButton *btClose;
QPoint last;

第二部:在mainwindow.cpp 源代码的MainWindow类的实例中,添加代码。

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include<QPushButton>
//引用Qlabel类

//绘制窗口并显示
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QLabel *label = new QLabel(this);
    //实列一个Qlabel控件以便显示内容

    label->setText("helloWorld");

    label->setGeometry(QRect(150,50,200,25));
//    行控件在窗体中显示位置,QRect(参数 1,参数 2,参数 3,参数 4)
//    参数 1:在窗体中 X 轴参数
//    参数 2:在窗体中 Y 轴参数
//    参数 3:QLabel 控件宽度
//    参数 4:QLabel 控件高度

    //窗体标题
    this->setWindowTitle("移动无边框窗体");
    //窗体最大尺寸
    this->setMaximumSize(400,300);
    //窗体最小尺寸
    this->setMinimumSize(400,300);
    //默认窗体居中显示,如果想要更改用 move 或 setGeometry
    this->move(100,100);
    //修改样式
    this->setStyleSheet("background:red");
    //窗体ICO图片设置,如图不起别名,后缀直接写图片全名。
    this->setWindowIcon(QIcon(":/image/icon"));
    //去掉标题栏
    this->setWindowFlags(Qt::FramelessWindowHint);
    //创建一个按钮实现关闭功能
    btClose = new QPushButton(this);
    btClose->setText("关闭");
    btClose->setGeometry(QRect(150,50,200,25));
    connect(btClose,SIGNAL(clicked()),this,SLOT(close()));


    //关闭按钮失效
    this->setWindowFlags(Qt::WindowCloseButtonHint);
    //最大最小化失效
    this->setWindowFlags(Qt::WindowMaximizeButtonHint);

    btButton2 = new QPushButton(this);
    btButton2->setGeometry(QRect(50,50,100,25));
    btButton2->setText("按钮");
    connect(btButton2,SIGNAL(clicked()),this,SLOT(showMainwindow2()));

}

void MainWindow::mousePressEvent(QMouseEvent *e){
    last = e->globalPos();
}
void MainWindow::mouseMoveEvent(QMouseEvent *e){
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();

    last = e->globalPos();
    move(x()+dx,y()+dy);

}
void MainWindow::mouseReleaseEvent(QMouseEvent *e){
//    int dx = e->globalX() - last.x();
//    int dy = e->globalY() - last.y();
//    move(x()+dx, y()+dy);
}

void MainWindow::showMainwindow2(){
    w2.show();
}

MainWindow::~MainWindow()
{
    delete ui;
}
//回收机制,用完实例后释放内存



原文地址:https://blog.csdn.net/m0_57852920/article/details/142887777

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!