其他分享
首页 > 其他分享> > QScrollArea使用详解

QScrollArea使用详解

作者:互联网

 设置QScrollArea长期显示滚动条。

QScrollArea的使用是将widget作为子窗口,然后当子窗口的宽高大于滚动的窗口的时候进行滚动。

mainwindow.cpp

#include "mainwindow.h"
#include <QScrollArea>
#include <QVariant>
#include <QScrollBar>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      m_ScrollArea(new QScrollArea(parent)),
     m_main(new QWidget),
     m_layout(new QVBoxLayout),
     m_btn(new QPushButton("button"))
{
       QPushButton *a1= new QPushButton("button");
       a1->setMinimumHeight(50);
       m_layout->addWidget(a1);
       QPushButton *a2= new QPushButton("button");
       a2->setMinimumHeight(50);
       m_layout->addWidget(a2);
       QPushButton *a3= new QPushButton("button");
       a3->setMinimumHeight(50);
       m_layout->addWidget(a3);
       QPushButton *a4= new QPushButton("button");
       a4->setMinimumHeight(50);
       m_layout->addWidget(a4);
       QPushButton *a5= new QPushButton("button");
       a5->setMinimumHeight(50);
       m_layout->addWidget(a5);
       QPushButton *a6= new QPushButton("button");
       a6->setMinimumHeight(50);
       m_layout->addWidget(a6);
       QPushButton *a7= new QPushButton("button");
       a7->setMinimumHeight(50);
       m_layout->addWidget(a7);
       QPushButton *a8= new QPushButton("button");
       a8->setMinimumHeight(50);
       m_layout->addWidget(a8);
       QPushButton *a9= new QPushButton("button");
       a9->setMinimumHeight(50);
       m_layout->addWidget(a9);

       m_ScrollArea->setFrameShape(QFrame::NoFrame);
       m_ScrollArea->setFixedSize(200,200);
       m_ScrollArea->setGeometry(5, 20, 70, 100);
       m_main->setFixedSize(200,500);
       m_main->setLayout(m_layout);
       m_ScrollArea->setWidget(m_main);
// 设置滚动条不隐藏
       m_ScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
       m_ScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
       
       m_ScrollArea->verticalScrollBar()->setProperty("_d_dtk_slider_always_show",QVariant::fromValue(true));
       m_ScrollArea->horizontalScrollBar()->setProperty("_d_dtk_slider_always_show",QVariant::fromValue(true));
       setCentralWidget(m_ScrollArea);
}

MainWindow::~MainWindow()
{

}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>

class QScrollArea;
class QWidget;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    QScrollArea *m_ScrollArea ;
    QVBoxLayout *m_layout;
    QWidget *m_main;
    QPushButton *m_btn;
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H

标签:ScrollArea,layout,setMinimumHeight,button,详解,QPushButton,使用,new,QScrollArea
来源: https://blog.csdn.net/arv002/article/details/121660338