Qt基于QScrollArea的界面嵌套移动

在实际的应用场景中,经常会出现软件界面战场图大于实际窗体大小,利用QScrollArea可以为widget窗体添加滚动条,可以实现小窗体利用滚动条显示大界面需求。实现如下:

  • QT创建一个qWidget界面

 

  • 在ui界面中利用QT自带的widget控件布局一个如下图所示的层叠关系,widget_2界面大小需大于widget大小

 

  • 界面布局好后,将widget_2提升为类,提升之前需为工程新添加一个设计界面类,添加完之后,将widget_2提升为类类名和前面新添加的设计界面类名一致

  

  • 源码实现如下

patchwindow.h

#ifndef PATCHWINDOW_H
#define PATCHWINDOW_H

#include <QDebug>
#include <QPainter>
#include <QWidget>
#include <QMouseEvent>
#include <QStyleOption>
#include <QPaintEvent>

enum CursorRegion{
    NONE,
    TOPLEFT,
    TOPRIGHT,
    BOTTOMRIGHT,
    BOTTOMLEFT
};

namespace Ui {
class Patchwindow;
}

class Patchwindow : public QWidget
{
    Q_OBJECT

public:
    explicit Patchwindow(QWidget *parent = 0);
    ~Patchwindow();
    CursorRegion getCursorRegion(QPoint);

public:
    int borderWidth;
    int handleSize;

    bool mousePressed;
    QPoint previousPos;

private:
    Ui::Patchwindow *ui;

protected:
    void mousePressEvent(QMouseEvent*);
    void mouseReleaseEvent(QMouseEvent*);
    void mouseMoveEvent(QMouseEvent*);

signals:
    void send_widget_rx_ry(int rx,int ry);
};

#endif // PATCHWINDOW_H

patchwindow.cpp

#include "patchwindow.h"
#include "ui_patchwindow.h"

Patchwindow::Patchwindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Patchwindow)
{
    ui->setupUi(this);

    this->setMouseTracking(true);

    setFocusPolicy(Qt::StrongFocus);

    mousePressed = false;
    borderWidth = 1;
    handleSize = 8;

}

Patchwindow::~Patchwindow()
{
    delete ui;
}


//设置鼠标形状
CursorRegion Patchwindow::getCursorRegion(QPoint pos)
{
    if (pos.x() > 0 && pos.x() < (handleSize + borderWidth) &&
        pos.y() > 0 && pos.y() < (handleSize + borderWidth)  ){
        if (this->hasFocus())
            this->setCursor(QCursor(Qt::SizeFDiagCursor));
        return CursorRegion::TOPLEFT;
    }

    if (pos.x() > (this->width() - handleSize - borderWidth) && pos.x() < this->width() &&
        pos.y() > 0 && pos.y() < (handleSize + borderWidth)  ){
        if (this->hasFocus())
            this->setCursor(QCursor(Qt::SizeBDiagCursor));
        return CursorRegion::TOPRIGHT;
    }

    if (pos.x() > (this->width() - handleSize - borderWidth) && pos.x() < this->width() &&
        pos.y() > (this->height() - handleSize - borderWidth) && pos.y() < this->height()  ){
        if (this->hasFocus())
            this->setCursor(QCursor(Qt::SizeFDiagCursor));
        return CursorRegion::BOTTOMRIGHT;
    }


    if (pos.x() > 0 && pos.x() < (handleSize + borderWidth) &&
        pos.y() > (this->height() - handleSize - borderWidth) && pos.y() < this->height()  ){
        if (this->hasFocus())
            this->setCursor(QCursor(Qt::SizeBDiagCursor));
        return CursorRegion::BOTTOMLEFT;
    }

    this->setCursor(Qt::ArrowCursor);
    return CursorRegion::NONE;
}

void Patchwindow::mousePressEvent(QMouseEvent *event)
{
    mousePressed = true;
    previousPos = this->mapToParent(event->pos());
    //qDebug()<<"previousPos = "<<previousPos;
}

void Patchwindow::mouseReleaseEvent(QMouseEvent*)
{
    mousePressed = false;
}

void Patchwindow::mouseMoveEvent(QMouseEvent *event)
{
    if (mousePressed){
        QPoint _curPos = this->mapToParent(event->pos());
        QPoint _offPos = _curPos - previousPos;
        previousPos = _curPos;
        //qDebug()<<"_offPos = "<<_offPos;
        //qDebug()<<"_curPos = "<<_curPos;
        emit send_widget_rx_ry(_offPos.rx(),_offPos.ry());
    }
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QHBoxLayout>
#include <QDebug>
#include <QScrollArea>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    QScrollArea *m_pScroll;


private:
    Ui::MainWindow *ui;

private slots:
    void remove_widget(int r_x,int r_y);


};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPalette>

#include <QScrollBar>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //this->resize(600,600);

    //给父窗体填充颜色
    QPalette palette = ui->widget_2->palette();
    palette.setBrush(QPalette::Window,QBrush(QColor(61,61,61)));
    ui->widget_2->setAutoFillBackground(true);
    ui->widget_2->setPalette(palette);

    ui->widget_2->setAttribute(Qt::WA_StyledBackground);
    ui->widget_2->setStyleSheet("QWidget{background: black}");

    ui->widget_3->setAttribute(Qt::WA_TransparentForMouseEvents, true);//设置该层鼠标事件透明,可以设置为显示层

    m_pScroll = new QScrollArea(ui->widget);
    m_pScroll->setWidget(ui->widget_2);//给widget_2设置滚动条
    //ui->widget_2->setMinimumSize(1500,1000);//这里注意,要比主窗体的尺寸要大,不然太小的话会留下一片空白

    QHBoxLayout *pLayout = new QHBoxLayout;
    pLayout->addWidget(m_pScroll);
    pLayout->setMargin(0);
    pLayout->setSpacing(0);
    ui->widget->setLayout(pLayout);

    connect(ui->widget_2,&Patchwindow::send_widget_rx_ry,this,&MainWindow::remove_widget);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::remove_widget(int r_x,int r_y)
{
    r_y = m_pScroll->verticalScrollBar()->value()-r_y;
    r_x = m_pScroll->horizontalScrollBar()->value()-r_x;

    if((0 < r_y) | (r_y == 0))
    {
        if(r_y > m_pScroll->verticalScrollBar()->maximum())
        {
            r_y = m_pScroll->verticalScrollBar()->maximum();
        }
    }
    else
    {
        r_y = 0;
    }

    if((0 < r_x) | (r_x == 0))
    {
        if(r_x > m_pScroll->horizontalScrollBar()->maximum())
        {
            r_x = m_pScroll->horizontalScrollBar()->maximum();
        }
    }
    else
    {
        r_x = 0;
    }

    m_pScroll->verticalScrollBar()->setValue(r_y);
    m_pScroll->horizontalScrollBar()->setValue(r_x);

}
  • 最终实现效果如下,可以通过滚轮滚动界面,也可以通过鼠标拖拽来实现界面拖拽效果: 

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt下的QScrollArea也可以实现放大缩小功能,实现方法类似于上面提到的Python示例代码。以下是C++的示例代码: ```cpp #include <QApplication> #include <QScrollArea> #include <QWidget> #include <QVBoxLayout> #include <QSlider> #include <QPainter> #include <QTransform> #include <Qt> class MyWidget : public QWidget { public: void paintEvent(QPaintEvent *event) override { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.drawText(rect(), Qt::AlignCenter, tr("Hello World")); } }; class MyScrollArea : public QScrollArea { public: MyScrollArea(QWidget *parent = nullptr) : QScrollArea(parent) { // 创建QWidget部件和QSlider部件 widget = new MyWidget(this); slider = new QSlider(Qt::Horizontal, this); // 将QWidget部件设置为QScrollArea的widget setWidget(widget); // 设置QSlider的范围和初始值 slider->setRange(1, 100); slider->setValue(100); // 创建垂直布局,并将QSlider添加到布局中 auto layout = new QVBoxLayout(this); layout->addWidget(slider); setLayout(layout); // 连接QSlider的valueChanged()信号到槽函数 connect(slider, &QSlider::valueChanged, this, &MyScrollArea::scaleContent); } private: MyWidget *widget; QSlider *slider; void scaleContent(int value) { // 设置QTransform缩放比例 QTransform transform; transform.scale(value / 100.0, value / 100.0); widget->setFixedSize(transform.mapRect(widget->rect()).size()); widget->setTransform(transform); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MyScrollArea scrollArea; scrollArea.show(); return app.exec(); } ``` 在这个示例中,我们创建了一个MyWidget类来绘制需要显示的内容,并将其作为QScrollArea的子部件。然后创建了一个QSlider来控制缩放比例,并将其添加到QScrollArea的布局中。在槽函数scaleContent()中,我们使用QTransform对QWidget进行缩放操作,并设置QWidget的大小。最终实现了QScrollArea的放大缩小功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值