您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Qt基于QScrollArea如何實現(xiàn)界面嵌套移動”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
QT創(chuàng)建一個qWidget界面
在ui界面中利用QT自帶的widget控件布局一個如下圖所示的層疊關(guān)系,widget_2界面大小需大于widget大小
界面布局好后,將widget_2提升為類,提升之前需為工程新添加一個設(shè)計界面類,添加完之后,將widget_2提升為類類名和前面新添加的設(shè)計界面類名一致
源碼實現(xiàn)如下
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; } //設(shè)置鼠標形狀 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);//設(shè)置該層鼠標事件透明,可以設(shè)置為顯示層 m_pScroll = new QScrollArea(ui->widget); m_pScroll->setWidget(ui->widget_2);//給widget_2設(shè)置滾動條 //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); }
最終實現(xiàn)效果如下,可以通過滾輪滾動界面,也可以通過鼠標拖拽來實現(xiàn)界面拖拽效果:
“Qt基于QScrollArea如何實現(xiàn)界面嵌套移動”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。