C++ QT学习之路----窗体应用

本篇文章中,主要记录控制窗体的操作。

本文在上一篇文章《C++ QT学习之路----创建hello world窗体》基础上添加新的代码。

先来设置窗体固定大小

在QT_practice.cpp文件中添加新的代码:

    //窗体标题
	QString str;
	str = str.fromLocal8Bit("Qt5.1 窗体应用");
	this->setWindowTitle(str);
	//窗体最大300*300
	this->setMaximumSize(300, 300);
	//窗体最小300*300
	this->setMinimumSize(300, 300);

 编译运行出来的效果如下:

该窗体大小固定,不可以修改。

期间,有个小插曲。我开始生成的窗体,标题中文部分“窗体应用”是一串乱码,应该是字符串编码格式没有调整过来。网上找了下,发现需要用fromLocal8Bit()将本地字符集(我理解就是中文字串)转换成QT能识别的编码格式(Unicode)。编译,运行ok。

 

设置窗体初始位置、背景色:

    //默认窗体居中显示,如果想要更改,用move或setGeometry
	this->move(100,100);
	//背景灰色
	this->setStyleSheet("background:grey");

设置窗体的icon:

    //窗体ICO图片
	this->setWindowIcon(QIcon("qt.ico"));

 设置移动无边框窗体:

头文件中先添加鼠标移动事件以及记录窗体、鼠标的坐标

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

private:
	QPushButton *btClose;
	QPoint last;

 源文件中添加如下代码:

关闭按钮

    //窗体标题
	QString str;
	str = str.fromLocal8Bit("Qt1.0 移动无边框窗体");
	//去掉标题栏
	this->setWindowFlags(Qt::FramelessWindowHint);    
    //添加一个关闭按钮,实现关闭功能
	btClose = new QPushButton(this);
	btClose->setText("close");
	btClose->setGeometry(QRect(50, 100, 200, 25));
	//按钮点击事件
	connect(btClose, SIGNAL(clicked()), this, SLOT(close()));

 鼠标事件

void QT_practice::mousePressEvent(QMouseEvent *e)
{
	last = e->globalPos();
}

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

	last = e->globalPos();
	move(this->pos().x() + dx, this->pos().y() + dy);//这里(x(),y())是父窗体左上角在屏幕中的位置
}

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

运行就可以打开一个无边框的程序了,不过,鼠标滑到button上时,移动会出现错位现象,这个问题查了好久,没搞定,先就这样放着,哈哈。

 

去掉窗体最大化、最小化按钮:

//去掉标题栏最大化、最小化按钮
	this->setWindowFlags(Qt::WindowCloseButtonHint);

调用子窗体:

欸,研究了两天,没搞出来,应该是前期环境搭建得有点问题,VS2017选择新建项目的时候,没法创建带.h/.cpp的选项,只能创建一个ui文件,后面还是在QT creator里创建了个子窗体项目,然后加载到VS2017里,真是被自己笑哭了,为啥这么折腾。。。

 QT creator里创建个子窗体,然后加载到原工程中,目录如下:

 QT_practice.h头文件中添加控件和点击事件,调用子窗体

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QT_practice.h"

#include <QMouseEvent>	//鼠标类头文件
#include <QPushButton>	//引用按钮类头文件
#include "subwindow.h"	//引用子窗体

class QT_practice : public QMainWindow
{
    Q_OBJECT

public:
    QT_practice(QWidget *parent = Q_NULLPTR);

private:
    Ui::QT_practiceClass ui;
	QPushButton *button;
	subWindow sw;
private slots:
	void showsubWindow();
	/*
protected:
	//鼠标按下
	void mousePressEvent(QMouseEvent *e);
	//鼠标移动
	void mouseMoveEvent(QMouseEvent *e);
	//鼠标释放
	void mouseReleaseEvent(QMouseEvent *e);

private:
	QPushButton *btClose;
	QPoint last;*/
};

QT_practice.cpp文件中添加如下代码:

	//实例QPushButton控件
	button = new QPushButton(this);
	//按钮显示位置
	str = str.fromLocal8Bit("打开子窗口");
	button->setText(str);
	button->setGeometry(QRect(100, 100, 100, 25));
	//点击事件
	connect(button, SIGNAL(clicked()), this, SLOT(showsubWindow()));

 

void QT_practice::showsubWindow()
{
	//显示subWindow窗体
	sw.show();
}

不过,运行之后,打开了子窗口,关闭主窗口时,子窗口不会关闭,代码里需要在关闭主窗体事件中添加关闭子窗体,我就懒得改了。

还是得先把VS2017+QT环境搭建好,不然用起来太不方便了。

设置背景:

//设置背景透明
	this->setAttribute(Qt::WA_TranslucentBackground, true);
	this->setStyleSheet("background-image:url(:/new/prefixl/touming);background-repeat:no-repeat;");

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值