当前位置:网站首页>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 .
边栏推荐
- About daily report plan
- pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
- First day of database
- Completed in May, 22
- Leetcode-9.Palindrome Numbber
- Typora set markdown syntax inline mode
- 1266_ Implementation analysis of FreeRTOS scheduler startup code
- Listen to the left width of the browser to calculate the distance
- Shuttle container component
- Janus feature draft
猜你喜欢

Listen to the left width of the browser to calculate the distance

Modular notes

First day of database

教育专家王中泽老师多年经验分享:家庭教育不是附庸品

1266_ Implementation analysis of FreeRTOS scheduler startup code

Leetcode-104. Maximum Depth of Binary Tree
![[并发进阶]——线程池总结](/img/69/dc8146dafc30f8a8efa012b67aa05c.png)
[并发进阶]——线程池总结

Outer margin collapse

Duality-Gated Mutual Condition Network for RGBT Tracking

No response from win10 explorer when dragging files
随机推荐
Leetcode hot topic 100 topic 21-25 solution
AtomicInteger原子操作类
Gobang interface of mobile console (C language)
P5431 [template] multiplicative inverse 2
Shangtang technology has actively resumed work and will vigorously invest in the capacity and deployment of the digital sentry
Niuke wrong question 3.1
P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
The gap between the parent box and the child box
Whether the ZABBIX monitoring host is online
Library management system 2- demand analysis
Leetcode-141. Linked List Cycle
Web API、DOM
Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
【CF#262 (Div. 2)】 A. Vasya and Socks
Modular notes
Outer margin collapse
. Net C Foundation (6): namespace - scope with name
Comparison of DOM tags of wechat applet development (native and uniapp)
webserver
LEARNING TARGET-ORIENTED DUAL ATTENTION FOR ROBUST RGB-T TRACKING