Qt-实现框选功能
相关资料:
https://linuxcpp.0voice.com/?id=102977 如何使用Qt实现框选功能?
实例代码:
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "TFrameSelect.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle(QStringLiteral("Qt实现框选功能")); TFrameSelect *o = new TFrameSelect(this); o->move(20, 20); o->resize(300, 200); } MainWindow::~MainWindow() { delete ui; }
TFrameSelect.h
#ifndef TFrameSelect_H #define TFrameSelect_H #include <QWidget> #include <QPainter> #include <QStyleOption> #include <QBitmap> #include <QLineEdit> #include <QMouseEvent> class TFrameSelect : public QWidget { Q_OBJECT public: TFrameSelect(QWidget *parent = nullptr); ~TFrameSelect(); protected: void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; private: QLineEdit *m_pEdit = nullptr; QRect m_selectRect; QPoint m_startPos; }; #endif // TFrameSelect_H
TFrameSelect.cpp
#include "TFrameSelect.h" TFrameSelect::TFrameSelect(QWidget *parent) : QWidget(parent) { m_pEdit = new QLineEdit(this); m_pEdit->setText("123"); } TFrameSelect::~TFrameSelect() { } void TFrameSelect::paintEvent(QPaintEvent *event) { QPainter painter(this); if (!m_selectRect.isNull()) { painter.setPen(Qt::red); painter.drawRect(m_selectRect); } // 绘制其他内容 } void TFrameSelect::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_startPos = event->pos(); m_selectRect.setTopLeft(m_startPos); m_selectRect.setBottomRight(m_startPos); update(); // 刷新界面 } } void TFrameSelect::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { m_selectRect.setBottomRight(event->pos()); update(); } } void TFrameSelect::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { QRect selectRect = m_selectRect.normalized(); // 完成最终的选择操作 m_selectRect = QRect(); // 设置为无效状态 update(); } }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>628</width> <height>345</height> </rect> </property> <property name="maximumSize"> <size> <width>100000</width> <height>400000</height> </size> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <property name="animated"> <bool>false</bool> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>9</x> <y>9</y> <width>1920</width> <height>1200</height> </rect> </property> <property name="text"> <string/> </property> <property name="pixmap"> <pixmap resource="abc.qrc">:/new/prefix1/a.jpg</pixmap> </property> <property name="scaledContents"> <bool>true</bool> </property> </widget> </widget> </widget> <resources> <include location="abc.qrc"/> </resources> <connections/> </ui>
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我