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

Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误

HTTP 状态码 406 (Not Acceptable) 和 500 (Internal Server Error) 是两类完全不同的错误&#xff0c;它们的含义、原因和解决方法都有显著区别。以下是详细对比&#xff1a; 1. HTTP 406 (Not Acceptable) 含义&#xff1a; 客户端请求的内容类型与服务器支持的内容类型不匹…...

脑机新手指南(八):OpenBCI_GUI:从环境搭建到数据可视化(下)

一、数据处理与分析实战 &#xff08;一&#xff09;实时滤波与参数调整 基础滤波操作 60Hz 工频滤波&#xff1a;勾选界面右侧 “60Hz” 复选框&#xff0c;可有效抑制电网干扰&#xff08;适用于北美地区&#xff0c;欧洲用户可调整为 50Hz&#xff09;。 平滑处理&…...

DockerHub与私有镜像仓库在容器化中的应用与管理

哈喽&#xff0c;大家好&#xff0c;我是左手python&#xff01; Docker Hub的应用与管理 Docker Hub的基本概念与使用方法 Docker Hub是Docker官方提供的一个公共镜像仓库&#xff0c;用户可以在其中找到各种操作系统、软件和应用的镜像。开发者可以通过Docker Hub轻松获取所…...

Auto-Coder使用GPT-4o完成:在用TabPFN这个模型构建一个预测未来3天涨跌的分类任务

通过akshare库&#xff0c;获取股票数据&#xff0c;并生成TabPFN这个模型 可以识别、处理的格式&#xff0c;写一个完整的预处理示例&#xff0c;并构建一个预测未来 3 天股价涨跌的分类任务 用TabPFN这个模型构建一个预测未来 3 天股价涨跌的分类任务&#xff0c;进行预测并输…...

Java多线程实现之Callable接口深度解析

Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...

06 Deep learning神经网络编程基础 激活函数 --吴恩达

深度学习激活函数详解 一、核心作用 引入非线性:使神经网络可学习复杂模式控制输出范围:如Sigmoid将输出限制在(0,1)梯度传递:影响反向传播的稳定性二、常见类型及数学表达 Sigmoid σ ( x ) = 1 1 +...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)

船舶制造装配管理现状&#xff1a;装配工作依赖人工经验&#xff0c;装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书&#xff0c;但在实际执行中&#xff0c;工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...

JS手写代码篇----使用Promise封装AJAX请求

15、使用Promise封装AJAX请求 promise就有reject和resolve了&#xff0c;就不必写成功和失败的回调函数了 const BASEURL ./手写ajax/test.jsonfunction promiseAjax() {return new Promise((resolve, reject) > {const xhr new XMLHttpRequest();xhr.open("get&quo…...

掌握 HTTP 请求:理解 cURL GET 语法

cURL 是一个强大的命令行工具&#xff0c;用于发送 HTTP 请求和与 Web 服务器交互。在 Web 开发和测试中&#xff0c;cURL 经常用于发送 GET 请求来获取服务器资源。本文将详细介绍 cURL GET 请求的语法和使用方法。 一、cURL 基本概念 cURL 是 "Client URL" 的缩写…...

嵌入式常见 CPU 架构

架构类型架构厂商芯片厂商典型芯片特点与应用场景PICRISC (8/16 位)MicrochipMicrochipPIC16F877A、PIC18F4550简化指令集&#xff0c;单周期执行&#xff1b;低功耗、CIP 独立外设&#xff1b;用于家电、小电机控制、安防面板等嵌入式场景8051CISC (8 位)Intel&#xff08;原始…...