日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

qt QRubberBand实现区域选择。

發(fā)布時間:2024/1/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 qt QRubberBand实现区域选择。 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

QRubberBand比較簡單,沒啥好講的,直接上示例。
.h

#ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QRubberBand> #include <QMouseEvent> #include <QCheckBox>class Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();protected:void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event); private:QRubberBand* m_rubberband;QPoint m_oldpoint;QPoint m_oldglobalpoint;QRect m_selectglobalrect;QRect m_selectrect;QList<QCheckBox*> m_boxList; }; #endif // WIDGET_H

.cpp

#include "widget.h" #include <QDebug> #include <QGridLayout>Widget::Widget(QWidget *parent): QWidget(parent) {QGridLayout* lay = new QGridLayout();for (int i = 0; i < 10; ++i) {for (int j = 0; j < 4; ++j) {QCheckBox* box = new QCheckBox(QString::number((i+1)*j),this);box->setFixedSize(50,20);box->setStyleSheet("background:green");lay->addWidget(box,i,j);m_boxList.append(box);}}this->setLayout(lay);m_rubberband = new QRubberBand(QRubberBand::Rectangle); }Widget::~Widget() { }void Widget::mousePressEvent(QMouseEvent *event) {m_oldglobalpoint = event->globalPos(); //獲取桌面坐標(biāo)m_oldpoint = event->pos(); //獲取當(dāng)前窗口坐標(biāo)m_rubberband->setGeometry(QRect(m_oldglobalpoint,m_oldglobalpoint));m_rubberband->show(); }void Widget::mouseMoveEvent(QMouseEvent *event) {m_selectglobalrect = QRect(m_oldglobalpoint,event->globalPos()); //獲取相對桌面的選擇區(qū)域m_selectrect = QRect(m_oldpoint,event->pos()); //獲取相對當(dāng)前窗口的界面m_rubberband->setGeometry(m_selectglobalrect.normalized()); }void Widget::mouseReleaseEvent(QMouseEvent *event) {m_rubberband->hide();//存在交叉則選中for (int i = 0; i < m_boxList.size(); ++i) {if (m_selectrect.intersects(m_boxList[i]->geometry())){m_boxList[i]->setChecked(true);}} }

總結(jié)

以上是生活随笔為你收集整理的qt QRubberBand实现区域选择。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。