当前位置:网站首页>QT interface nested movement based on qscrollarea
QT interface nested movement based on qscrollarea
2022-06-11 07:13:00 【Illusory private school】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
In a real application scenario , Often, the software interface battlefield image is larger than the actual window size , utilize QScrollArea It can be for widget Add a scroll bar to the form , Small forms can be achieved by using the scroll bar to display large interface requirements . The implementation is as follows :
- QT Create a qWidget Interface

- stay ui Interface QT Self contained widget The control layout has a cascading relationship as shown in the following figure ,widget_2 The size of the interface should be larger than widget size

- After the interface is laid out , take widget_2 Promote to class , Before upgrading, you need to add a new design interface class for the project , And then when we're done , take widget_2 Upgrade to the same class name as the newly added design interface class name


- The source implementation is as follows
patchwindow.h


1 #ifndef PATCHWINDOW\_H
2 #define PATCHWINDOW\_H
3
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10
11 enum CursorRegion{
12 NONE,
13 TOPLEFT,
14 TOPRIGHT,
15 BOTTOMRIGHT,
16 BOTTOMLEFT
17 };
18
19 namespace Ui {
20 class Patchwindow;
21 }
22
23 class Patchwindow : public QWidget
24 {
25 Q\_OBJECT
26
27 public:
28 explicit Patchwindow(QWidget *parent = 0);
29 ~Patchwindow();
30 CursorRegion getCursorRegion(QPoint);
31
32 public:
33 int borderWidth;
34 int handleSize;
35
36 bool mousePressed;
37 QPoint previousPos;
38
39 private:
40 Ui::Patchwindow *ui;
41
42 protected:
43 void mousePressEvent(QMouseEvent*);
44 void mouseReleaseEvent(QMouseEvent*);
45 void mouseMoveEvent(QMouseEvent*);
46
47 signals:
48 void send\_widget\_rx\_ry(int rx,int ry);
49 };
50
51 #endif // PATCHWINDOW\_H
View Code
patchwindow.cpp


1 #include "patchwindow.h"
2 #include "ui\_patchwindow.h"
3
4 Patchwindow::Patchwindow(QWidget *parent) :
5 QWidget(parent),
6 ui(new Ui::Patchwindow)
7 {
8 ui->setupUi(this);
9
10 this->setMouseTracking(true);
11
12 setFocusPolicy(Qt::StrongFocus);
13
14 mousePressed = false;
15 borderWidth = 1;
16 handleSize = 8;
17
18 }
19
20 Patchwindow::~Patchwindow()
21 {
22 delete ui;
23 }
24
25
26 // Set the mouse shape
27 CursorRegion Patchwindow::getCursorRegion(QPoint pos)
28 {
29 if (pos.x() > 0 && pos.x() < (handleSize + borderWidth) &&
30 pos.y() > 0 && pos.y() < (handleSize + borderWidth) ){
31 if (this->hasFocus())
32 this->setCursor(QCursor(Qt::SizeFDiagCursor));
33 return CursorRegion::TOPLEFT;
34 }
35
36 if (pos.x() > (this->width() - handleSize - borderWidth) && pos.x() < this->width() &&
37 pos.y() > 0 && pos.y() < (handleSize + borderWidth) ){
38 if (this->hasFocus())
39 this->setCursor(QCursor(Qt::SizeBDiagCursor));
40 return CursorRegion::TOPRIGHT;
41 }
42
43 if (pos.x() > (this->width() - handleSize - borderWidth) && pos.x() < this->width() &&
44 pos.y() > (this->height() - handleSize - borderWidth) && pos.y() < this->height() ){
45 if (this->hasFocus())
46 this->setCursor(QCursor(Qt::SizeFDiagCursor));
47 return CursorRegion::BOTTOMRIGHT;
48 }
49
50
51 if (pos.x() > 0 && pos.x() < (handleSize + borderWidth) &&
52 pos.y() > (this->height() - handleSize - borderWidth) && pos.y() < this->height() ){
53 if (this->hasFocus())
54 this->setCursor(QCursor(Qt::SizeBDiagCursor));
55 return CursorRegion::BOTTOMLEFT;
56 }
57
58 this->setCursor(Qt::ArrowCursor);
59 return CursorRegion::NONE;
60 }
61
62 void Patchwindow::mousePressEvent(QMouseEvent *event)
63 {
64 mousePressed = true;
65 previousPos = this->mapToParent(event->pos());
66 //qDebug()<<"previousPos = "<
67 }
68
69 void Patchwindow::mouseReleaseEvent(QMouseEvent*)
70 {
71 mousePressed = false;
72 }
73
74 void Patchwindow::mouseMoveEvent(QMouseEvent *event)
75 {
76 if (mousePressed){
77 QPoint \_curPos = this->mapToParent(event->pos());
78 QPoint \_offPos = \_curPos - previousPos;
79 previousPos = \_curPos;
80 //qDebug()<<"\_offPos = "<<\_offPos;
81 //qDebug()<<"\_curPos = "<<\_curPos;
82 emit send\_widget\_rx\_ry(\_offPos.rx(),\_offPos.ry());
83 }
84 }
View Code
mainwindow.h


1 #ifndef MAINWINDOW\_H
2 #define MAINWINDOW\_H
3
4 #include
5 #include
6 #include
7 #include
8
9
10 namespace Ui {
11 class MainWindow;
12 }
13
14 class MainWindow : public QMainWindow
15 {
16 Q\_OBJECT
17
18 public:
19 explicit MainWindow(QWidget *parent = 0);
20 ~MainWindow();
21
22 QScrollArea *m\_pScroll;
23
24
25 private:
26 Ui::MainWindow *ui;
27
28 private slots:
29 void remove\_widget(int r\_x,int r\_y);
30
31
32 };
33
34 #endif // MAINWINDOW\_H
View Code
mainwindow.cpp


1 #include "mainwindow.h"
2 #include "ui\_mainwindow.h"
3 #include
4
5 #include
6
7 MainWindow::MainWindow(QWidget *parent) :
8 QMainWindow(parent),
9 ui(new Ui::MainWindow)
10 {
11 ui->setupUi(this);
12 //this->resize(600,600);
13
14 // Color parent form
15 QPalette palette = ui->widget\_2->palette();
16 palette.setBrush(QPalette::Window,QBrush(QColor(61,61,61)));
17 ui->widget\_2->setAutoFillBackground(true);
18 ui->widget\_2->setPalette(palette);
19
20 ui->widget\_2->setAttribute(Qt::WA\_StyledBackground);
21 ui->widget\_2->setStyleSheet("QWidget{background: black}");
22
23 ui->widget\_3->setAttribute(Qt::WA\_TransparentForMouseEvents, true);// Set the mouse event transparency of this layer , Can be set to display layer
24
25 m\_pScroll = new QScrollArea(ui->widget);
26 m\_pScroll->setWidget(ui->widget\_2);// to widget\_2 Set scroll bar
27 //ui->widget\_2->setMinimumSize(1500,1000);// Note here , Larger than the size of the main window , Otherwise, too small will leave a blank
28
29 QHBoxLayout *pLayout = new QHBoxLayout;
30 pLayout->addWidget(m\_pScroll);
31 pLayout->setMargin(0);
32 pLayout->setSpacing(0);
33 ui->widget->setLayout(pLayout);
34
35 connect(ui->widget\_2,&Patchwindow::send\_widget\_rx\_ry,this,&MainWindow::remove\_widget);
36
37 }
38
39 MainWindow::~MainWindow()
40 {
41 delete ui;
42 }
43
44 void MainWindow::remove\_widget(int r\_x,int r\_y)
45 {
46 r\_y = m\_pScroll->verticalScrollBar()->value()-r\_y;
47 r\_x = m\_pScroll->horizontalScrollBar()->value()-r\_x;
48
49 if((0 < r\_y) | (r\_y == 0))
50 {
51 if(r\_y > m\_pScroll->verticalScrollBar()->maximum())
52 {
53 r\_y = m\_pScroll->verticalScrollBar()->maximum();
54 }
55 }
56 else
57 {
58 r\_y = 0;
59 }
60
61 if((0 < r\_x) | (r\_x == 0))
62 {
63 if(r\_x > m\_pScroll->horizontalScrollBar()->maximum())
64 {
65 r\_x = m\_pScroll->horizontalScrollBar()->maximum();
66 }
67 }
68 else
69 {
70 r\_x = 0;
71 }
72
73 m\_pScroll->verticalScrollBar()->setValue(r\_y);
74 m\_pScroll->horizontalScrollBar()->setValue(r\_x);
75
76 }
View Code
- The final effect is as follows , You can scroll the interface through the scroll wheel , You can also drag the interface by dragging the mouse :

Project source code download path :
「ScrollArea」https://www.aliyundrive.com/s/QMf912nt86A Click the link to save , Or copy this paragraph , open 「 Alicloud disk 」APP , No need to download speed online view , The original video is played at double speed .
边栏推荐
- The rotation of the earth and the moon (II)
- MS office level II wrong question record [4]
- 1266_FreeRTOS调度器启动代码实现分析
- Set center alignment
- Adventure of small X
- Atom, the top stream editor, will leave the historical stage on December 15
- 资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
- Mybags puls will report an error invalid bound statement (not found) when writing an SQL statement in the XML file:
- P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
- RGB-D Salient Object Detection withCross-Modality Modulation and Selection
猜你喜欢

资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员

Latex various arrows / arrows with text labels / variable length arrows

. Net C Foundation (6): namespace - scope with name

Whether the ZABBIX monitoring host is online

一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试

Create a form whose client area is 800 pixels by 600 pixels

二、用户登录和注册
![[deploy private warehouse based on harbor] 3 deploy harbor](/img/cd/be68a430e86b4b23ad93b42a338f00.jpg)
[deploy private warehouse based on harbor] 3 deploy harbor

Difference between byte and bit

Leetcode-104. Maximum Depth of Binary Tree
随机推荐
Education expert wangzhongze shared his experience for many years: family education is not a vassal
Graph Attention Tracking
mybaits-puls 在xml文件中写sql语句 会报错 Invalid bound statement (not found):
Leetcode hot topic 100 topic 21-25 solution
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
Typora set markdown syntax inline mode
Henan college entrance examination vs Tianjin college entrance examination (2008-2021)
Promises/a+ standard Chinese Translation
Phi and phi (Mobius inversion + formula)
Interview question 17.08 Circus tower
**Count the characters with the largest number of words**
webserver
教育专家王中泽老师:家庭教育重在自己成长
RGB-D Salient Object Detection withCross-Modality Modulation and Selection
【CF#654 (Div. 2)】A. Magical Sticks
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
421. maximum XOR value of two numbers in the array
Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Explain the difference between void 0 and undefined