欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

現(xiàn)有功能

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

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

運(yùn)行結(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();
}

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

相關(guān)文章

  • C語言實(shí)現(xiàn)520表白代碼 祝你表白成功!

    C語言實(shí)現(xiàn)520表白代碼 祝你表白成功!

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)520表白代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • C++類成員函數(shù)后面加const問題

    C++類成員函數(shù)后面加const問題

    這篇文章主要介紹了C++類成員函數(shù)后面加const問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++聚合關(guān)系類的構(gòu)造函數(shù)的調(diào)用順序詳解

    C++聚合關(guān)系類的構(gòu)造函數(shù)的調(diào)用順序詳解

    下面小編就為大家?guī)硪黄狢++聚合關(guān)系類的構(gòu)造函數(shù)的調(diào)用順序詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考,一起跟隨小編過來看看吧
    2016-05-05
  • C語言?詳解如何刪除有序數(shù)組中的重復(fù)項(xiàng)

    C語言?詳解如何刪除有序數(shù)組中的重復(fù)項(xiàng)

    數(shù)組不擅長插入(添加)和刪除元素。數(shù)組的優(yōu)點(diǎn)在于它是連續(xù)的,所以查找數(shù)據(jù)速度很快。但這也是它的一個(gè)缺點(diǎn)。正因?yàn)樗沁B續(xù)的,所以當(dāng)插入一個(gè)元素時(shí),插入點(diǎn)后所有的元素全部都要向后移;而刪除一個(gè)元素時(shí),刪除點(diǎn)后所有的元素全部都要向前移
    2022-03-03
  • MFC中exe圖標(biāo)修改的方法

    MFC中exe圖標(biāo)修改的方法

    修改窗口標(biāo)題圖標(biāo)可通過導(dǎo)入圖標(biāo),然后在CMainFrame.:OnCreate函數(shù)中加載圖標(biāo)即可, 代碼如下:
    2013-04-04
  • 關(guān)于C++靜態(tài)成員函數(shù)訪問非靜態(tài)成員變量的問題

    關(guān)于C++靜態(tài)成員函數(shù)訪問非靜態(tài)成員變量的問題

    靜態(tài)成員函數(shù)不能訪問非靜態(tài)成員,這是因?yàn)殪o態(tài)函數(shù)屬于類而不是屬于整個(gè)對象,靜態(tài)函數(shù)中的 member可能都沒有分配內(nèi)存。靜態(tài)成員函數(shù)沒有隱含的this自變量。所以,它就無法訪問自己類的非靜態(tài)成員
    2013-10-10
  • Qt實(shí)現(xiàn)編輯框失去焦點(diǎn)隱藏功能

    Qt實(shí)現(xiàn)編輯框失去焦點(diǎn)隱藏功能

    這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)的一個(gè)簡單的編輯框操作——主窗口失去焦點(diǎn)隱藏功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-10-10
  • wxWidgets實(shí)現(xiàn)無標(biāo)題欄窗口拖動效果

    wxWidgets實(shí)現(xiàn)無標(biāo)題欄窗口拖動效果

    這篇文章主要為大家詳細(xì)介紹了wxWidgets實(shí)現(xiàn)無標(biāo)題欄窗口拖動效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • C++之智能指針初步及棄用auto_ptr的原因分析

    C++之智能指針初步及棄用auto_ptr的原因分析

    這篇文章主要介紹了C++之智能指針初步及棄用auto_ptr的原因分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 解析C++11的std::ref、std::cref源碼

    解析C++11的std::ref、std::cref源碼

    這篇文章主要介紹了解析C++11的std::ref、std::cref源碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05

最新評論