最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt實現(xiàn)矩形大小任意縮放的示例代碼

 更新時間:2022年06月07日 14:33:30   作者:la_vie_est_belle  
這篇文章主要介紹了Qt如何實現(xiàn)在窗口上繪制任意大小的矩形,并且通過邊角的拖曳按鈕可改變矩形大小,感興趣的小伙伴可以跟隨小編一起學習一下

現(xiàn)有功能

1.在窗口上繪制任意大小的矩形。

2.通過邊角的拖曳按鈕改變矩形大小。

運行結果

源碼

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();                                  // 設置拖曳按鈕樣式

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();                          // 設置邊角拖曳按鈕位置并顯示

protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);

private:
    int x1;                                         // x1和y1是矩形左上角坐標
    int y1;
    int x2;                                         // x2和y2是矩形右下角坐標
    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();
}

以上就是Qt實現(xiàn)矩形大小任意縮放的示例代碼的詳細內(nèi)容,更多關于Qt矩形任意縮放的資料請關注腳本之家其它相關文章!

相關文章

  • 詳解C++中stoi/stol/stoll函數(shù)的用法

    詳解C++中stoi/stol/stoll函數(shù)的用法

    這篇文章主要為大家詳細介紹了C++中stoi、stol、stoll函數(shù)的具體用法,文中的示例代碼講解詳細,對我們學校C++有一點的幫助,需要的可以參考一下
    2023-03-03
  • C++中異常的深度解析

    C++中異常的深度解析

    異常處理機制允許程序中獨立開發(fā)部分能夠在運行時就出現(xiàn)的問題進行通信并做出相應的處理,這篇文章主要介紹了C++中異常的深度解析,需要的朋友可以參考下
    2025-03-03
  • 解析C++無鎖隊列的實現(xiàn)代碼

    解析C++無鎖隊列的實現(xiàn)代碼

    本篇文章是對C++無鎖隊列的實現(xiàn)進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Linux?C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息

    Linux?C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息

    NIC流量統(tǒng)計信息是由操作系統(tǒng)維護的,當數(shù)據(jù)包通過NIC傳輸時,操作系統(tǒng)會更新相關的計數(shù)器,通過讀取這些計數(shù)器,我們可以獲得關于網(wǎng)絡流量的信息,下面我們就來學習一下如何通過C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息吧
    2024-01-01
  • 詳解C語言處理算經(jīng)中著名問題百錢百雞

    詳解C語言處理算經(jīng)中著名問題百錢百雞

    古代的很多數(shù)學問題都可以用現(xiàn)代的編程語言去嘗試解決,就如本篇,將會帶你通過C語言來解決算經(jīng)中百錢百雞問題,感興趣的朋友來看看吧
    2022-02-02
  • C++ lambda函數(shù)詳解

    C++ lambda函數(shù)詳解

    小編可以明確告訴大家:lambda函數(shù)是C++11中最重要的,使用最廣泛的,最具現(xiàn)代風格的內(nèi)容,lambda函數(shù)的出現(xiàn)改變了C++編程的思維方式。所以快和小編學習一下C++11中l(wèi)ambda函數(shù)的使用吧
    2023-02-02
  • C++實現(xiàn)連連看游戲

    C++實現(xiàn)連連看游戲

    這篇文章主要為大家詳細介紹了C++實現(xiàn)連連看游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C語言實現(xiàn)點餐系統(tǒng)

    C語言實現(xiàn)點餐系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)點餐系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C++入門基礎之命名空間、輸入輸出和缺省參數(shù)

    C++入門基礎之命名空間、輸入輸出和缺省參數(shù)

    C++入門基礎篇的內(nèi)容為C++的基本特性,只有在掌握C++的基本特性后,是進入后面類和對象學習的基礎,下面這篇文章主要給大家介紹了關于C++入門基礎之命名空間、輸入輸出和缺省參數(shù)的相關資料,需要的朋友可以參考下
    2023-01-01
  • C++與C語言常用的語法對比

    C++與C語言常用的語法對比

    這篇文章主要介紹了C++與C語言常用的語法對比,文章基于c++和C語言的相關資料展開兩者的語法相互對比,需要的小伙伴可以參考一下,希望對你的學習有所幫助
    2022-04-04

最新評論

神池县| 永嘉县| 库车县| 交城县| 建水县| 永修县| 安多县| 双辽市| 桃江县| 榆树市| 周宁县| 四会市| 宁陕县| 兴和县| 康平县| 桦南县| 开平市| 松潘县| 黄龙县| 万山特区| 中阳县| 交城县| 西安市| 定兴县| 体育| 平塘县| 宁乡县| 涿州市| 台江县| 吉水县| 金坛市| 临西县| 马尔康县| 崇阳县| 河北区| 株洲市| 唐海县| 河源市| 阳原县| 望城县| 富平县|