QScrollArea里动态添加控件,并自动滚动到最底部

QScrollArea里动态添加控件,并自动滚动到最底部_第1张图片

 

关键点在于最后的计算

void init()//初始化
{
    m_pScrollAreaContentWidget = new QWidget(this);
    m_pScrollAreaContentWidget->setObjectName("m_pScrollAreaContentWidget");
    m_pContentWidgetLayout = new QVBoxLayout;
    m_pContentWidgetLayout->setSpacing(16*m_dScaleFactor);
    m_pContentWidgetLayout->setMargin(0);
    m_pContentWidgetLayout->setContentsMargins(0,0,0,0);
    m_pContentWidgetLayout->setAlignment(Qt::AlignTop);
    m_pScrollAreaContentWidget->setFixedWidth(this->width() - 17*m_dScaleFactor);
    m_pScrollAreaContentWidget->setLayout(m_pContentWidgetLayout);
    m_pScrollArea = new QScrollArea(this);
    //加10是为了留出滚动条的位置,以防向左抖动
    //滚动条据边框距离为7
    m_pScrollArea->setFixedWidth(m_pScrollAreaContentWidget->width()+10*m_dScaleFactor);
    m_pScrollArea->setObjectName("m_scrollArea");
    m_pScrollArea->setWidgetResizable(true);////////////
    m_pScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_pScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pScrollArea->setStyleSheet("QAbstractScrollArea#m_scrollArea{border: none;}");
    QScrollBar* scrBar = m_pScrollArea->verticalScrollBar();
    scrBar->setFixedWidth(10*m_dScaleFactor);
    scrBar->setStyleSheet(QString("QScrollBar:vertical{"
                                  "width:%5px;background-color:#F2F3F9;"
                                  "margin:0px,0px,0px,0px;"
                                  "padding-top:0px;padding-bottom:0px;padding-right:%1px;}"
                                  "QScrollBar::handle:vertical{"
                                  "border-radius: %2px;width:%3px;"
                                  "background: rgb(0, 0, 0, 20);min-height:%4px;}"
                                  "QScrollBar::handle:vertical:hover{"
                                  "border-radius: %2px;width:%3px;"
                                  "background:rgb(0, 0, 0, 30);min-height:%4px;}"
                                  "QScrollBar::handle:vertical:pressed{"
                                  "border-radius: %2px;width:%3px;"
                                  "background:rgb(0, 0, 0, 40);min-height:%4px;}"
                                  "QScrollBar::sub-line:vertical, QScrollBar::add-line:vertical"
                                  "{width: 0px;height: 0px; background:#ffffff}"
                                  "QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical"
                                  "{background: #ffffff;}")
                          .arg(0*m_dScaleFactor).arg(int(4*m_dScaleFactor)).arg(10*m_dScaleFactor).arg(int(20*m_dScaleFactor)).arg(int(10*m_dScaleFactor)));
    m_pScrollArea->setWidget(m_pScrollAreaContentWidget);
    m_pScrollArea->setContentsMargins(0,0,0,0);
    m_pScrollArea->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
}
void slotAddWidget()//插入控件,并滚动
{
    HHContectWidget* wid = new HHContectWidget(this);
    m_pContentWidgetLayout->addWidget(wid);
    m_pScrollArea->ensureWidgetVisible(wid);
    m_pScrollAreaContentWidget->setFixedHeight(m_vPronunciationEdit.count()*48*m_dScaleFactor);//////////
    m_pScrollArea->verticalScrollBar()->setValue(m_vPronunciationEdit.count()*48*m_dScaleFactor);/////////
}
 
 

你可能感兴趣的:(QScrollArea里动态添加控件,并自动滚动到最底部)