当前位置: 首页 > 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 作…...

React hook之useRef

React useRef 详解 useRef 是 React 提供的一个 Hook&#xff0c;用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途&#xff0c;下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…...

STM32+rt-thread判断是否联网

一、根据NETDEV_FLAG_INTERNET_UP位判断 static bool is_conncected(void) {struct netdev *dev RT_NULL;dev netdev_get_first_by_flags(NETDEV_FLAG_INTERNET_UP);if (dev RT_NULL){printf("wait netdev internet up...");return false;}else{printf("loc…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明

AI 领域的快速发展正在催生一个新时代&#xff0c;智能代理&#xff08;agents&#xff09;不再是孤立的个体&#xff0c;而是能够像一个数字团队一样协作。然而&#xff0c;当前 AI 生态系统的碎片化阻碍了这一愿景的实现&#xff0c;导致了“AI 巴别塔问题”——不同代理之间…...

Matlab | matlab常用命令总结

常用命令 一、 基础操作与环境二、 矩阵与数组操作(核心)三、 绘图与可视化四、 编程与控制流五、 符号计算 (Symbolic Math Toolbox)六、 文件与数据 I/O七、 常用函数类别重要提示这是一份 MATLAB 常用命令和功能的总结,涵盖了基础操作、矩阵运算、绘图、编程和文件处理等…...

Maven 概述、安装、配置、仓库、私服详解

目录 1、Maven 概述 1.1 Maven 的定义 1.2 Maven 解决的问题 1.3 Maven 的核心特性与优势 2、Maven 安装 2.1 下载 Maven 2.2 安装配置 Maven 2.3 测试安装 2.4 修改 Maven 本地仓库的默认路径 3、Maven 配置 3.1 配置本地仓库 3.2 配置 JDK 3.3 IDEA 配置本地 Ma…...

鸿蒙DevEco Studio HarmonyOS 5跑酷小游戏实现指南

1. 项目概述 本跑酷小游戏基于鸿蒙HarmonyOS 5开发&#xff0c;使用DevEco Studio作为开发工具&#xff0c;采用Java语言实现&#xff0c;包含角色控制、障碍物生成和分数计算系统。 2. 项目结构 /src/main/java/com/example/runner/├── MainAbilitySlice.java // 主界…...

2025季度云服务器排行榜

在全球云服务器市场&#xff0c;各厂商的排名和地位并非一成不变&#xff0c;而是由其独特的优势、战略布局和市场适应性共同决定的。以下是根据2025年市场趋势&#xff0c;对主要云服务器厂商在排行榜中占据重要位置的原因和优势进行深度分析&#xff1a; 一、全球“三巨头”…...

Python 实现 Web 静态服务器(HTTP 协议)

目录 一、在本地启动 HTTP 服务器1. Windows 下安装 node.js1&#xff09;下载安装包2&#xff09;配置环境变量3&#xff09;安装镜像4&#xff09;node.js 的常用命令 2. 安装 http-server 服务3. 使用 http-server 开启服务1&#xff09;使用 http-server2&#xff09;详解 …...

数学建模-滑翔伞伞翼面积的设计,运动状态计算和优化 !

我们考虑滑翔伞的伞翼面积设计问题以及运动状态描述。滑翔伞的性能主要取决于伞翼面积、气动特性以及飞行员的重量。我们的目标是建立数学模型来描述滑翔伞的运动状态,并优化伞翼面积的设计。 一、问题分析 滑翔伞在飞行过程中受到重力、升力和阻力的作用。升力和阻力与伞翼面…...

沙箱虚拟化技术虚拟机容器之间的关系详解

问题 沙箱、虚拟化、容器三者分开一一介绍的话我知道他们各自都是什么东西&#xff0c;但是如果把三者放在一起&#xff0c;它们之间到底什么关系&#xff1f;又有什么联系呢&#xff1f;我不是很明白&#xff01;&#xff01;&#xff01; 就比如说&#xff1a; 沙箱&#…...