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

【大模型RAG】拍照搜题技术架构速览:三层管道、两级检索、兜底大模型

摘要 拍照搜题系统采用“三层管道&#xff08;多模态 OCR → 语义检索 → 答案渲染&#xff09;、两级检索&#xff08;倒排 BM25 向量 HNSW&#xff09;并以大语言模型兜底”的整体框架&#xff1a; 多模态 OCR 层 将题目图片经过超分、去噪、倾斜校正后&#xff0c;分别用…...

JavaScript 中的 ES|QL:利用 Apache Arrow 工具

作者&#xff1a;来自 Elastic Jeffrey Rengifo 学习如何将 ES|QL 与 JavaScript 的 Apache Arrow 客户端工具一起使用。 想获得 Elastic 认证吗&#xff1f;了解下一期 Elasticsearch Engineer 培训的时间吧&#xff01; Elasticsearch 拥有众多新功能&#xff0c;助你为自己…...

通过Wrangler CLI在worker中创建数据库和表

官方使用文档&#xff1a;Getting started Cloudflare D1 docs 创建数据库 在命令行中执行完成之后&#xff0c;会在本地和远程创建数据库&#xff1a; npx wranglerlatest d1 create prod-d1-tutorial 在cf中就可以看到数据库&#xff1a; 现在&#xff0c;您的Cloudfla…...

PL0语法,分析器实现!

简介 PL/0 是一种简单的编程语言,通常用于教学编译原理。它的语法结构清晰,功能包括常量定义、变量声明、过程(子程序)定义以及基本的控制结构(如条件语句和循环语句)。 PL/0 语法规范 PL/0 是一种教学用的小型编程语言,由 Niklaus Wirth 设计,用于展示编译原理的核…...

WEB3全栈开发——面试专业技能点P2智能合约开发(Solidity)

一、Solidity合约开发 下面是 Solidity 合约开发 的概念、代码示例及讲解&#xff0c;适合用作学习或写简历项目背景说明。 &#x1f9e0; 一、概念简介&#xff1a;Solidity 合约开发 Solidity 是一种专门为 以太坊&#xff08;Ethereum&#xff09;平台编写智能合约的高级编…...

零基础设计模式——行为型模式 - 责任链模式

第四部分&#xff1a;行为型模式 - 责任链模式 (Chain of Responsibility Pattern) 欢迎来到行为型模式的学习&#xff01;行为型模式关注对象之间的职责分配、算法封装和对象间的交互。我们将学习的第一个行为型模式是责任链模式。 核心思想&#xff1a;使多个对象都有机会处…...

【开发技术】.Net使用FFmpeg视频特定帧上绘制内容

目录 一、目的 二、解决方案 2.1 什么是FFmpeg 2.2 FFmpeg主要功能 2.3 使用Xabe.FFmpeg调用FFmpeg功能 2.4 使用 FFmpeg 的 drawbox 滤镜来绘制 ROI 三、总结 一、目的 当前市场上有很多目标检测智能识别的相关算法&#xff0c;当前调用一个医疗行业的AI识别算法后返回…...

算法笔记2

1.字符串拼接最好用StringBuilder&#xff0c;不用String 2.创建List<>类型的数组并创建内存 List arr[] new ArrayList[26]; Arrays.setAll(arr, i -> new ArrayList<>()); 3.去掉首尾空格...

Aspose.PDF 限制绕过方案:Java 字节码技术实战分享(仅供学习)

Aspose.PDF 限制绕过方案&#xff1a;Java 字节码技术实战分享&#xff08;仅供学习&#xff09; 一、Aspose.PDF 简介二、说明&#xff08;⚠️仅供学习与研究使用&#xff09;三、技术流程总览四、准备工作1. 下载 Jar 包2. Maven 项目依赖配置 五、字节码修改实现代码&#…...

淘宝扭蛋机小程序系统开发:打造互动性强的购物平台

淘宝扭蛋机小程序系统的开发&#xff0c;旨在打造一个互动性强的购物平台&#xff0c;让用户在购物的同时&#xff0c;能够享受到更多的乐趣和惊喜。 淘宝扭蛋机小程序系统拥有丰富的互动功能。用户可以通过虚拟摇杆操作扭蛋机&#xff0c;实现旋转、抽拉等动作&#xff0c;增…...