最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt基于QScrollArea實(shí)現(xiàn)界面嵌套移動(dòng)

 更新時(shí)間:2022年06月10日 14:29:26   作者:熊來(lái)闖一闖  
在實(shí)際的應(yīng)用場(chǎng)景中,經(jīng)常會(huì)出現(xiàn)軟件界面戰(zhàn)場(chǎng)圖大于實(shí)際窗體大小。本文將利用QScrollArea可以為widget窗體添加滾動(dòng)條,實(shí)現(xiàn)界面嵌套移動(dòng),感興趣的可以了解一下

在實(shí)際的應(yīng)用場(chǎng)景中,經(jīng)常會(huì)出現(xiàn)軟件界面戰(zhàn)場(chǎng)圖大于實(shí)際窗體大小,利用QScrollArea可以為widget窗體添加滾動(dòng)條,可以實(shí)現(xiàn)小窗體利用滾動(dòng)條顯示大界面需求。實(shí)現(xiàn)如下:

QT創(chuàng)建一個(gè)qWidget界面

在ui界面中利用QT自帶的widget控件布局一個(gè)如下圖所示的層疊關(guān)系,widget_2界面大小需大于widget大小

界面布局好后,將widget_2提升為類(lèi),提升之前需為工程新添加一個(gè)設(shè)計(jì)界面類(lèi),添加完之后,將widget_2提升為類(lèi)類(lèi)名和前面新添加的設(shè)計(jì)界面類(lèi)名一致

源碼實(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è)置鼠標(biāo)形狀
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è)置該層鼠標(biāo)事件透明,可以設(shè)置為顯示層

    m_pScroll = new QScrollArea(ui->widget);
    m_pScroll->setWidget(ui->widget_2);//給widget_2設(shè)置滾動(dòng)條
    //ui->widget_2->setMinimumSize(1500,1000);//這里注意,要比主窗體的尺寸要大,不然太小的話會(huì)留下一片空白

    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);

}

最終實(shí)現(xiàn)效果如下,可以通過(guò)滾輪滾動(dòng)界面,也可以通過(guò)鼠標(biāo)拖拽來(lái)實(shí)現(xiàn)界面拖拽效果:

到此這篇關(guān)于Qt基于QScrollArea實(shí)現(xiàn)界面嵌套移動(dòng)的文章就介紹到這了,更多相關(guān)Qt QScrollArea界面嵌套移動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c/c++ 奇技淫巧(一些c語(yǔ)言的技巧)

    c/c++ 奇技淫巧(一些c語(yǔ)言的技巧)

    這篇文章主要介紹了c/c++ 奇技淫巧,需要的朋友可以參考下
    2017-03-03
  • C語(yǔ)言標(biāo)準(zhǔn)時(shí)間與秒單位相互轉(zhuǎn)換

    C語(yǔ)言標(biāo)準(zhǔn)時(shí)間與秒單位相互轉(zhuǎn)換

    這篇文章主要介紹了C語(yǔ)言標(biāo)準(zhǔn)時(shí)間與秒單位相互轉(zhuǎn)換,秒單位與標(biāo)準(zhǔn)時(shí)間的轉(zhuǎn)換方式,這份代碼一般用在嵌入式單片機(jī)里比較多,比如:設(shè)置RTC時(shí)鐘的時(shí)間,從RTC里讀取秒單位時(shí)間后,需要轉(zhuǎn)換成標(biāo)準(zhǔn)時(shí)間顯示。下文分享需要的小伙伴可以參考一下
    2022-05-05
  • C++中vector的用法實(shí)例解析

    C++中vector的用法實(shí)例解析

    這篇文章主要介紹了C++中vector的用法,詳細(xì)描述了vector的各種常見(jiàn)的用法及注意事項(xiàng),需要的朋友可以參考下
    2014-08-08
  • C++ 反射機(jī)制詳解及實(shí)例代碼

    C++ 反射機(jī)制詳解及實(shí)例代碼

    這篇文章主要介紹了C++ 反射機(jī)制詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • C++中使用正則匹配問(wèn)題

    C++中使用正則匹配問(wèn)題

    這篇文章主要介紹了C++中使用正則匹配問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C/C++利用原生套接字抓取FTP數(shù)據(jù)包

    C/C++利用原生套接字抓取FTP數(shù)據(jù)包

    這篇文章主要為大家詳細(xì)介紹了如何基于原始套接字的網(wǎng)絡(luò)數(shù)據(jù)包捕獲與分析工具,通過(guò)實(shí)時(shí)監(jiān)控網(wǎng)絡(luò)流量,實(shí)現(xiàn)抓取流量包內(nèi)的FTP通信數(shù)據(jù),需要的小伙伴可以參考下
    2023-12-12
  • C語(yǔ)言深入分析函數(shù)與宏的使用

    C語(yǔ)言深入分析函數(shù)與宏的使用

    C語(yǔ)言函數(shù)是一種函數(shù),用來(lái)編譯C語(yǔ)言,一般包括字符庫(kù)函數(shù),數(shù)學(xué)函數(shù),目錄函數(shù),進(jìn)程函數(shù),診斷函數(shù),操作函數(shù)等,宏在C語(yǔ)言中是一段有名稱(chēng)的代碼片段。無(wú)論何時(shí)使用到這個(gè)宏的時(shí)候,宏的內(nèi)容都會(huì)被這段代碼替換掉
    2022-04-04
  • C語(yǔ)言轉(zhuǎn)義字符實(shí)例詳解

    C語(yǔ)言轉(zhuǎn)義字符實(shí)例詳解

    這里主要介紹了C語(yǔ)言的轉(zhuǎn)義字符的知識(shí),并附有示例代碼,以便理解,希望對(duì)學(xué)習(xí) C語(yǔ)言的同學(xué)有所幫助
    2016-07-07
  • C++內(nèi)嵌匯編示例詳解

    C++內(nèi)嵌匯編示例詳解

    這篇文章主要介紹了C++內(nèi)嵌匯編,本文的所有代碼是在我自己的VS2008中測(cè)試的,由于環(huán)境的差別,不能保證能在所有的編譯器上運(yùn)行,需要的朋友可以參考下
    2022-01-01
  • 一文教你Qt如何操作SQLite數(shù)據(jù)庫(kù)

    一文教你Qt如何操作SQLite數(shù)據(jù)庫(kù)

    Sqlite 數(shù)據(jù)庫(kù)作為 Qt 項(xiàng)目開(kāi)發(fā)中經(jīng)常使用的一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù),可以說(shuō)是兼容性相對(duì)比較好的數(shù)據(jù)庫(kù)之一。本文為大家介紹了Qt操作SQLite數(shù)據(jù)庫(kù)的具體方法,希望對(duì)大家有所幫助
    2023-03-03

最新評(píng)論

石台县| 威远县| 民县| 陇西县| 通城县| 石林| 洪泽县| 望江县| 南汇区| 靖宇县| 连山| 遵化市| 曲麻莱县| 方正县| 辛集市| 成安县| 天祝| 芮城县| 梁河县| 桓仁| 元朗区| 巴楚县| 孟津县| 太湖县| 玉树县| 临高县| 永兴县| 淮北市| 化隆| 神农架林区| 大洼县| 内江市| 静乐县| 名山县| 嘉鱼县| 轮台县| 团风县| 温泉县| 安达市| 麟游县| 阜南县|