服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - Qt基于QScrollArea实现界面嵌套移动

Qt基于QScrollArea实现界面嵌套移动

2022-12-22 14:58熊来闯一闯 C/C++

在实际的应用场景中,经常会出现软件界面战场图大于实际窗体大小。本文将利用QScrollArea可以为widget窗体添加滚动条,实现界面嵌套移动,感兴趣的可以了解一下

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

QT创建一个qWidget界面

Qt基于QScrollArea实现界面嵌套移动

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

Qt基于QScrollArea实现界面嵌套移动

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

Qt基于QScrollArea实现界面嵌套移动

Qt基于QScrollArea实现界面嵌套移动

源码实现如下

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基于QScrollArea实现界面嵌套移动

到此这篇关于Qt基于QScrollArea实现界面嵌套移动的文章就介绍到这了,更多相关Qt QScrollArea界面嵌套移动内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/xionglaichuangyichuang/p/16363022.html

延伸 · 阅读

精彩推荐
  • C/C++C++中opencv4.1.0环境配置的详细过程

    C++中opencv4.1.0环境配置的详细过程

    这篇文章主要介绍了C++中opencv4.1.0环境配置的详细过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需...

    孤独的猫7752022-01-21
  • C/C++详解C++内存的代码区,全局区,栈区和堆区

    详解C++内存的代码区,全局区,栈区和堆区

    这篇文章主要为大家介绍了C++内存的代码区,全局区,栈区和堆区,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助...

    维纳斯高多少9302022-08-04
  • C/C++C++11中跳转initializer_list实现分析

    C++11中跳转initializer_list实现分析

    这篇文章主要介绍了C++11中跳转initializer_list实现分析,实例分析initializer_list初体验,结合示例代码给大家介绍的非常详细,需要的朋友可以参考下...

    浅墨浓香11622022-11-03
  • C/C++C语言实现串的顺序存储表示与基本操作

    C语言实现串的顺序存储表示与基本操作

    这篇文章主要为大家详细介绍了C语言实现串的顺序存储表示与基本操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考...

    似曾不相识11912022-01-10
  • C/C++如何基于 Blueprint 在游戏中创建实时音视频功能

    如何基于 Blueprint 在游戏中创建实时音视频功能

    我们在本文先来讲讲如何在 Unreal 中用 Blueprint 快速实现。稍后会分享基于 C++的实现步骤。感兴趣的朋友跟随小编一起看看吧...

    声网Agora8962021-09-08
  • C/C++一篇文章带你了解C/C++的回调函数

    一篇文章带你了解C/C++的回调函数

    这篇文章主要为大家介绍了C/C++的回调函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助...

    MAX在码字4042022-09-03
  • C/C++C语言实现合式公式的判断示例

    C语言实现合式公式的判断示例

    这篇文章主要介绍了C语言实现合式公式的判断示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    New Youg7762022-11-03
  • C/C++C语言的动态内存分配及动态内存分配函数详解

    C语言的动态内存分配及动态内存分配函数详解

    这篇文章主要为大家详细介绍了C语言的动态内存分配及动态内存分配函数,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    Green_75611012022-10-26