当前位置: 首页 > news >正文

qt 5.15.2 主窗体事件及绘制功能

qt 5.15.2 主窗体事件及绘制功能

显示主窗体效果图如下所示:
在这里插入图片描述

main.cpp

#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.setFixedWidth(600);w.setFixedHeight(600);w.show();//w.showMaximized(); //最大化显示后,再添加布局部件return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();//void print(QString msg);void DrawBorad();void DrawTipText();void PrintMsg(int x,int y,QString msg);void line(int x1,int y1,int x2,int y2);void circle(int x,int y,int radius);//qt 绘制事件void paintEvent(QPaintEvent *event);//qt mouse eventvoid mousePressEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void keyPressEvent(QKeyEvent *event);void wheelEvent(QWheelEvent *event);void resizeEvent(QResizeEvent *event);//void DrawPiece();private:Ui::MainWindow *ui;    bool isMousePressed=false;QPoint currentPoint;char Current_piece='O';//初始化char Board_piece[3][3]={{'_','_','_'},{'_','_','o'},{'_','_','x'}};
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qpainter.h>
#include <iostream>
#include <QDebug>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QResizeEvent>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);//mouse move event setup must two line OKthis->setMouseTracking(true);ui->centralwidget->setMouseTracking(true);//
}MainWindow::~MainWindow()
{delete ui;    
}void MainWindow::print(QString msg)
{qDebug()<<msg;//std:cout<<msg.toStdString()<<std::endl;
}void MainWindow::DrawBorad()
{line(0,200,600,200);   //cline(0,400,600,400);   //dline(200,0,200,600);  //aline(400,0,400,600);   //b
}void MainWindow::line(int x1,int y1,int x2,int y2)
{QPainter painter(this);painter.drawLine(x1,y1,x2,y2);
}void MainWindow::circle(int x,int y,int radius)
{QPainter painter(this);painter.drawEllipse(QPoint(x,y),radius,radius);
}void MainWindow::DrawTipText()
{QPainter painter(this);//static TCHAR str[64];//_stprintf_s(str,_T("当前棋子类型:%c"),Current_piece);//settextcolor(RGB(255,175,45));//outtextxy(0,0,str);QPen pen;pen.setWidth(2);//设置线宽pen.setStyle(Qt::SolidLine);//样式pen.setColor(QColor(255,0,0));//文字颜色painter.setPen(pen);QString msg=QString::asprintf("当前棋子类型:%c",Current_piece);painter.drawText(10, 15, msg);//文本内容
}void MainWindow::PrintMsg(int x,int y,QString msg)
{QPainter painter(this);//static TCHAR str[64];//_stprintf_s(str,_T("当前棋子类型:%c"),Current_piece);//settextcolor(RGB(255,175,45));//outtextxy(0,0,str);QPen pen;pen.setWidth(2);//设置线宽pen.setStyle(Qt::SolidLine);//样式pen.setColor(QColor(255,45,100));//文字颜色painter.setPen(pen);painter.drawText(x, y, msg);//文本内容
}void MainWindow::DrawPiece()
{for(int i=0;i<3;i++){for(int j=0;j<3;j++){switch(this->Board_piece[i][j]){case 'O':case 'o':this->circle(200*j+100,200*i+100,100);break;case 'x':case 'X':{int x=200*j;int y=200*i;line(x,y,x+200,y+200);   //左对角线(x,y)-(x+100,y+100)line(x+200,y,x,y+200);   //右对角线(x+100,y)-(x,y+100)}break;case '_':case '-':break;}}}
}//总绘制事件
void MainWindow::paintEvent(QPaintEvent *event)
{QPainter painter(this);//反走样painter.setRenderHint(QPainter::Antialiasing, true);//画背景图QString jpgPath="E:\\cwgis_qt\\hsg\\qi_three_son\\images\\china_hdz.jpg";painter.drawPixmap(0,0, 600, 600, QPixmap(jpgPath));//定义画笔QPen pen;pen.setWidth(2);//设置线宽pen.setColor(QColor(222,255,55));//颜色pen.setStyle(Qt::SolidLine);//样式//画刷QBrush brush;brush.setColor(Qt::black);//颜色brush.setStyle(Qt::SolidPattern);//样式//设置画笔画刷painter.setPen(pen);painter.setBrush(brush);//画直线painter.drawLine(50,50,150,50);painter.drawLine(20,50,250,150 );//画矩形painter.drawRect(70,200,100,50);//画椭圆painter.drawEllipse(QPoint(270,150),50,80);//画文字QFont font;font.setFamily("MV Boli");//文字字体font.setPixelSize(20);//文字大小pen.setColor(QColor(255,55,255));//文字颜色painter.setFont(font);painter.setPen(pen);painter.drawText(200, 300, "Qt Creator 12.0.0 (opensource)");//文本内容//this->DrawBorad();this->DrawPiece();//this->DrawTipText();this->PrintMsg(20,30,jpgPath);this->PrintMsg(20,40,qApp->applicationDirPath());  //获取可执行文件所在目录//{QString msg=QString::asprintf("%d,%d",this->currentPoint.x(),this->currentPoint.y());this->PrintMsg(40,580,"(x,y)="+msg);}
}void MainWindow::mousePressEvent(QMouseEvent *event)
{this->isMousePressed=true;print("pressed mouse");this->update();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{this->isMousePressed=false;print("unpresse mouse");this->update();
}void MainWindow::mouseMoveEvent(QMouseEvent *event)
{QPoint p=event->pos();this->currentPoint=p;qDebug()<<"pos="<<p;   //需要引用#include <QMouseEvent>//QString msg=QString::asprintf("%d,%d",p.x(),p.y());//this->PrintMsg(40,300,"(x,y)="+msg);//qDebug()<<msg;if(this->isMousePressed){QString msg=QString::asprintf("%b",this->isMousePressed);this->PrintMsg(570,40,"isMousePressed="+msg);}else{QString msg=QString::asprintf("%b",this->isMousePressed);this->PrintMsg(570,40,"isMousePressed="+msg);}print("moveing mouse");this->update();   //更新触发重绘事件paintEvent 才能动态显示当前点坐标(x,y)=100,200
}void MainWindow::keyPressEvent(QKeyEvent *event)
{qDebug()<<"key: "<<event->key();this->update();
}
void MainWindow::wheelEvent(QWheelEvent *event)
{qDebug()<<"wheel: "<<event->angleDelta();this->update();
}void MainWindow::resizeEvent(QResizeEvent *event)
{qDebug()<<"resize: "<<event->size();this->update();
}

本blog地址:https://blog.csdn.net/hsg77

相关文章:

qt 5.15.2 主窗体事件及绘制功能

qt 5.15.2 主窗体事件及绘制功能 显示主窗体效果图如下所示&#xff1a; main.cpp #include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);MainWindow w;w.setFixedWidth(600);w.setFixedHeight(6…...

(2)(2.4) TerraRanger Tower/Tower EVO(360度)

文章目录 前言 1 安装传感器并连接 2 通过地面站进行配置 3 参数说明 前言 TeraRanger Tower 可用于在 Loiter 和 AltHold 模式下进行目标规避。传感器的最大可用距离约为 4.5m。 TeraRanger Tower EVO 可用于在 Loiter 和 AltHold 模式下进行目标规避。传感器的最大可用…...

Redis_主从复制、哨兵模式、集群模式详解

Redis的主从复制 为什么Redis要引入主从复制&#xff1f;what&#xff1f; 在这里博主为小伙伴们简单的做下解释&#xff0c;可以了解一下 实际生产环境下&#xff0c;单机的redis服务器是无法满足实际的生产需求的。 第一&#xff0c;单机的redis服务器很容易发生单点故障&am…...

关于神舟-战神TA5NS系统重装问题

加装固态卡在log处无法开机问题 下面是我的步骤 1.按f7选择pe安装系统&#xff0c;然后发现卡在战神log处不转动 2.下载驱动 TA5NS驱动地址 下载RAID驱动&#xff08;如果没有私信我&#xff0c;我网盘里有&#xff09;&#xff0c;拷到u盘中&#xff0c;然后进入pe系统里面…...

前端大文件上传webuploader(react + umi)

使用WebUploader还可以批量上传文件、支持缩略图等等众多参数选项可设置&#xff0c;以及多个事件方法可调用&#xff0c;你可以随心所欲的定制你要的上传组件。 分片上传 1.什么是分片上传 分片上传&#xff0c;就是将所要上传的文件&#xff0c;按照一定的大小&#xff0c;将…...

人大金仓(kingbase)数据库常用sql命令

一. 字段 1. 添加 alter table book add column book_id varchar not null, book_title varchar(10) default ;2. 删除 alter table book drop book_id, book_title;// 外键时 alter table book drop book_id, book_title cascade;3. 修改类型 alter table book alter colu…...

HashMap相关专题

前置知识&#xff1a;异或运算 异或运算介绍 异或有什么神奇之处&#xff08;应用&#xff09;&#xff1f; &#xff08;1&#xff09;快速比较两个值 &#xff08;2&#xff09;我们可以使用异或来使某些特定的位翻转&#xff0c;因为不管是0或者是1与1做异或将得到原值的相…...

threejs WebGLRenderer 像素比对画布大小的影响

官方文档 - WebGLRenderer .setPixelRatio ( value : number ) : undefined 设置设备像素比。通常用于避免HiDPI设备上绘图模糊 .setSize ( width : Integer, height : Integer, updateStyle : Boolean ) : undefined 将输出canvas的大小调整为(width, height)并考虑设备像素比…...

RocketMQTemplate.send() 与 RocketMQTemplate.syncSend() 方法详解

Apache RocketMQ 是一款强大的分布式消息中间件&#xff0c;与 Spring Boot 集成后&#xff0c;通过 RocketMQTemplate 提供了多种方法来发送消息。其中&#xff0c;send() 和 syncSend() 是两个常用的发送消息方法&#xff0c;本文将深入探讨它们的区别以及详细解释这两个方法…...

波奇学C++:类型转换和IO流

隐式类型转换 int i0; double pi; 强制类型转换 int* pnullptr; int a(int)p; 单参数构造函数支持隐式类型转换 class A { public:A(string a):_a(a){} private:string _a; }; A a("xxxx"); //"xxx" const char* 隐式转换为string 多参数也可以通过{…...

集成开发环境 PyCharm 的安装【侯小啾python基础领航计划 系列(二)】

集成开发环境PyCharm的安装【侯小啾python基础领航计划 系列(二)】 大家好,我是博主侯小啾, 🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔ꦿ🌹꧔…...

Java核心知识点整理大全27-笔记(已完结)

目录 30. 云计算 30.1.1. SaaS 30.1.2. PaaS 30.1.3. IaaS 30.1.4. Docker 30.1.4.1. 概念 30.1.4.2. Namespaces 30.1.4.3. 进程(CLONE_NEWPID 实现的进程隔离) 30.1.4.4. Libnetwork 与网络隔离 30.1.4.5. 资源隔离与 CGroups 30.1.4.6. 镜像与 UnionFS 30.1.4.7.…...

1. 使用poll或epoll创建echo服务器

1. 说明&#xff1a; 此篇博客主要记录一种客户端实现方式&#xff0c;和两种使用poll或者epoll分别创建echo服务器的方式&#xff0c;具体可看代码注释&#xff1a; 2. 相关代码&#xff1a; 2.1 echoClient.cpp #include <iostream> #include <cstdio> #incl…...

【对象数组根据属性排序】

// sort使用的排序方法 // 传入对象数组用于排序的对象的属性,升序/降序 function compare(property, sortType "asc") {debugger// 如果不是 asc,desc,不做下一步比较if (!(sortType "desc" || sortType "asc")) {return;}return function (…...

BACnet I/O模块:楼宇自动化的未来选择

在楼宇自动化领域&#xff0c;BACnet通信协议在确保设备之间无缝高效的数据交换方面发挥着至关重要的作用。该领域使用广泛的协议是BACnet。它使传感器、执行器和控制器等设备能够相互通信&#xff0c;从而促进工业过程的自动化。 BACNET介绍 BACnet是专门为楼宇自动化和控制系…...

android项目实战之使用框架 集成多图片、视频的上传

效果图 实现方式&#xff0c;本功能使用PictureSelector 第三方库 。作者项目地址&#xff1a;https://github.com/LuckSiege/PictureSelector 1. builder.gradle 增加 implementation io.github.lucksiege:pictureselector:v3.11.1implementation com.tbruyelle.rxpermissio…...

MyBatis查询优化:枚举在条件构建中的妙用

&#x1f680; 作者主页&#xff1a; 有来技术 &#x1f525; 开源项目&#xff1a; youlai-mall &#x1f343; vue3-element-admin &#x1f343; youlai-boot &#x1f33a; 仓库主页&#xff1a; Gitee &#x1f4ab; Github &#x1f4ab; GitCode &#x1f496; 欢迎点赞…...

Isaac Sim教程04 Isaac Sim的高级使用

Isaac Sim 高级使用 版权信息 Copyright 2023 Herman YeAuromix. All rights reserved.This course and all of its associated content, including but not limited to text, images, videos, and any other materials, are protected by copyright law. The author holds…...

《数据结构、算法与应用C++语言描述》-线索二叉树的定义与C++实现

_23Threaded BinaryTree 可编译运行代码见&#xff1a;GIithub::Data-Structures-Algorithms-and-Applications/_24Threaded_BinaryTree 线索二叉树定义 在普通二叉树中&#xff0c;有很多nullptr指针被浪费了&#xff0c;可以将其利用起来。 首先我们要来看看这空指针有多少…...

删除误提交的 git commit

背景描述 某次的意外 commit 中误将密码写到代码中并且 push 到了 remote repo 里面, 本文将围绕这个场景讨论如何弥补. 模拟误提交操作 在 Gitee 创建一个新的 Repo, clone 到本地 git clone https://gitee.com/lpwm/myrepo.git创建两个文件, commit 后 push 到 remote 作…...

C# SqlSugar:依赖注入与仓储模式实践

C# SqlSugar&#xff1a;依赖注入与仓储模式实践 在 C# 的应用开发中&#xff0c;数据库操作是必不可少的环节。为了让数据访问层更加简洁、高效且易于维护&#xff0c;许多开发者会选择成熟的 ORM&#xff08;对象关系映射&#xff09;框架&#xff0c;SqlSugar 就是其中备受…...

SpringCloudGateway 自定义局部过滤器

场景&#xff1a; 将所有请求转化为同一路径请求&#xff08;方便穿网配置&#xff09;在请求头内标识原来路径&#xff0c;然后在将请求分发给不同服务 AllToOneGatewayFilterFactory import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; impor…...

C# 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

python报错No module named ‘tensorflow.keras‘

是由于不同版本的tensorflow下的keras所在的路径不同&#xff0c;结合所安装的tensorflow的目录结构修改from语句即可。 原语句&#xff1a; from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后&#xff1a; from tensorflow.python.keras.lay…...

深度学习水论文:mamba+图像增强

&#x1f9c0;当前视觉领域对高效长序列建模需求激增&#xff0c;对Mamba图像增强这方向的研究自然也逐渐火热。原因在于其高效长程建模&#xff0c;以及动态计算优势&#xff0c;在图像质量提升和细节恢复方面有难以替代的作用。 &#x1f9c0;因此短时间内&#xff0c;就有不…...

解读《网络安全法》最新修订,把握网络安全新趋势

《网络安全法》自2017年施行以来&#xff0c;在维护网络空间安全方面发挥了重要作用。但随着网络环境的日益复杂&#xff0c;网络攻击、数据泄露等事件频发&#xff0c;现行法律已难以完全适应新的风险挑战。 2025年3月28日&#xff0c;国家网信办会同相关部门起草了《网络安全…...

Linux系统部署KES

1、安装准备 1.版本说明V008R006C009B0014 V008&#xff1a;是version产品的大版本。 R006&#xff1a;是release产品特性版本。 C009&#xff1a;是通用版 B0014&#xff1a;是build开发过程中的构建版本2.硬件要求 #安全版和企业版 内存&#xff1a;1GB 以上 硬盘&#xf…...

API网关Kong的鉴权与限流:高并发场景下的核心实践

&#x1f525;「炎码工坊」技术弹药已装填&#xff01; 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 引言 在微服务架构中&#xff0c;API网关承担着流量调度、安全防护和协议转换的核心职责。作为云原生时代的代表性网关&#xff0c;Kong凭借其插件化架构…...

CSS3相关知识点

CSS3相关知识点 CSS3私有前缀私有前缀私有前缀存在的意义常见浏览器的私有前缀 CSS3基本语法CSS3 新增长度单位CSS3 新增颜色设置方式CSS3 新增选择器CSS3 新增盒模型相关属性box-sizing 怪异盒模型resize调整盒子大小box-shadow 盒子阴影opacity 不透明度 CSS3 新增背景属性ba…...

node.js的初步学习

那什么是node.js呢&#xff1f; 和JavaScript又是什么关系呢&#xff1f; node.js 提供了 JavaScript的运行环境。当JavaScript作为后端开发语言来说&#xff0c; 需要在node.js的环境上进行当JavaScript作为前端开发语言来说&#xff0c;需要在浏览器的环境上进行 Node.js 可…...