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();
}
View Code

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
View Code

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;
}
View Code

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
View Code

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();
    }
}
View Code

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>
View Code

 

 

posted on 2023-09-07 11:20  疯狂delphi  阅读(68)  评论(0编辑  收藏  举报

导航