網(wǎng)站首頁 編程語言 正文
現(xiàn)有功能
1.在窗口上繪制任意大小的矩形。
2.通過邊角的拖曳按鈕改變矩形大小。
運行結(jié)果
源碼
point_button.h
#ifndef POINTBUTTON_H
#define POINTBUTTON_H
#include <QPushButton>
#include <QWidget>
#include <QMouseEvent>
class PointButton : public QPushButton
{
public:
PointButton(QWidget *parent);
~PointButton();
public:
bool isPressed; // 用來判斷用戶是否正按在拖曳按鈕上
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
void setQss(); // 設(shè)置拖曳按鈕樣式
private:
float startX; // 用來移動拖曳按鈕
float startY;
};
#endif // POINTBUTTON_H
point_button.cpp
#include "point_button.h"
#include <QString>
#include "window.h"
PointButton::PointButton(QWidget *parent): QPushButton(parent) {
this->resize(10, 10);
this->isPressed = false;
this->setQss();
}
PointButton::~PointButton() {
}
void PointButton::setQss() {
QString qss = "QPushButton {\n"
"border-radius: 5px;\n"
"border: 1px solid black;"
"background-color: rgb(255, 255, 255);\n"
"}\n"
"QPushButton:hover {\n"
"border-width: 2px;\n"
"}";
this->setStyleSheet(qss);
}
void PointButton::mousePressEvent(QMouseEvent *event) {
QPushButton::mousePressEvent(event);
this->startX = event->x();
this->startY = event->y();
this->isPressed = true;
}
void PointButton::mouseMoveEvent(QMouseEvent *event) {
QPushButton::mouseMoveEvent(event);
float disX = event->x() - this->startX;
float disY = event->y() - this->startY;
this->move(this->x()+disX, this->y()+disY);
Window *parent = (Window*) this->parent();
parent->changeSize();
}
void PointButton::mouseReleaseEvent(QMouseEvent *event) {
QPushButton::mouseReleaseEvent(event);
this->isPressed = false;
}
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QPen>
#include "point_button.h"
class Window : public QWidget
{
Q_OBJECT
public:
Window(QWidget *parent = nullptr);
~Window();
void changeSize(); // 改變矩形尺寸
void hideCornerBtns(); // 隱藏邊角拖曳按鈕
void showCornerBtns(); // 設(shè)置邊角拖曳按鈕位置并顯示
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
private:
int x1; // x1和y1是矩形左上角坐標(biāo)
int y1;
int x2; // x2和y2是矩形右下角坐標(biāo)
int y2;
QPen pen;
PointButton *topLeftBtn;
PointButton *topRightBtn;
PointButton *bottomLeftBtn;
PointButton *bottomRightBtn;
};
#endif // WINDOW_H
window.cp
#include "window.h"
#include <Qt>
#include <QPainter>
#include <QDebug>
Window::Window(QWidget *parent): QWidget(parent) {
this->pen = QPen(Qt::black);
this->topLeftBtn = new PointButton(this);
this->topRightBtn = new PointButton(this);
this->bottomLeftBtn = new PointButton(this);
this->bottomRightBtn = new PointButton(this);
this->x1 = float(NULL);
this->y1 = float(NULL);
this->x2 = float(NULL);
this->y2 = float(NULL);
this->hideCornerBtns();
}
Window::~Window() {
}
void Window::mousePressEvent(QMouseEvent *event) {
QWidget::mousePressEvent(event);
this->x1 = float(NULL);
this->y1 = float(NULL);
this->x2 = float(NULL);
this->y2 = float(NULL);
this->hideCornerBtns();
this->x1 = event->x();
this->y1 = event->y();
this->update();
}
void Window::mouseMoveEvent(QMouseEvent *event) {
QWidget::mouseMoveEvent(event);
if (this->topLeftBtn->isPressed || this->topRightBtn->isPressed ||
this->bottomLeftBtn->isPressed || this->bottomRightBtn->isPressed)
return;
this->x2 = event->x();
this->y2 = event->y();
this->update();
}
void Window::paintEvent(QPaintEvent *event) {
QWidget::paintEvent(event);
if (this->x1==float(NULL) || this->y1==float(NULL) || this->x2==float(NULL) || this->y2==float(NULL)) {
return;
}
QPainter painter(this);
painter.setPen(this->pen);
int width = this->x2 - this->x1;
int height = this->y2 - this->y1;
painter.drawRect(this->x1, this->y1, width, height);
this->showCornerBtns();
}
void Window::hideCornerBtns() {
this->topLeftBtn->hide();
this->topRightBtn->hide();
this->bottomLeftBtn->hide();
this->bottomRightBtn->hide();
}
void Window::showCornerBtns() {
int halfWidth = int(this->topLeftBtn->width() / 2);
int halfHeight = int(this->topLeftBtn->height() / 2);
this->topLeftBtn->move(this->x1-halfWidth, this->y1-halfHeight);
this->topRightBtn->move(this->x2-halfWidth, this->y1-halfHeight);
this->bottomLeftBtn->move(this->x1-halfWidth, this->y2-halfHeight);
this->bottomRightBtn->move(this->x2-halfWidth, this->y2-halfHeight);
this->topLeftBtn->show();
this->topRightBtn->show();
this->bottomLeftBtn->show();
this->bottomRightBtn->show();
}
void Window::changeSize() {
if (this->topLeftBtn->isPressed) {
this->x1 = int(this->topLeftBtn->x() + this->topLeftBtn->width()/2);
this->y1 = int(this->topLeftBtn->y() + this->topLeftBtn->height()/2);
}
else if (this->topRightBtn->isPressed) {
this->x2 = int(this->topRightBtn->x() + this->topRightBtn->width()/2);
this->y1 = int(this->topRightBtn->y() + this->topRightBtn->height()/2);
}
else if (this->bottomLeftBtn->isPressed) {
this->x1 = int(this->bottomLeftBtn->x() + this->bottomLeftBtn->width()/2);
this->y2 = int(this->bottomLeftBtn->y() + this->bottomLeftBtn->height()/2);
}
else if (this->bottomRightBtn->isPressed) {
this->x2 = int(this->bottomRightBtn->x() + this->bottomRightBtn->width()/2);
this->y2 = int(this->bottomRightBtn->y() + this->bottomRightBtn->height()/2);
}
this->update();
}
main.cpp
#include "window.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}
原文鏈接:https://blog.csdn.net/La_vie_est_belle/article/details/125117484
相關(guān)推薦
- 2023-01-17 pytest內(nèi)置fixture使用臨時目錄流程詳解_python
- 2022-04-14 Python中的flask框架詳解_python
- 2022-11-14 解決“您的連接不是私密鏈接”的問題
- 2022-07-19 Ribbon負(fù)載均衡深入探究
- 2023-03-11 Go語言break跳轉(zhuǎn)語句怎么使用_Golang
- 2022-05-08 利用Pandas讀取某列某行數(shù)據(jù)之loc和iloc用法總結(jié)_python
- 2022-05-09 輕量級ORM框架Dapper應(yīng)用之實現(xiàn)DTO_實用技巧
- 2024-01-08 Request請求轉(zhuǎn)發(fā)和Response重定向
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支