QTreeWidget 设置自定义滚动条在合适的时候show或hide C++实现
时间: 2023-07-16 09:14:00 浏览: 174
可以通过继承QTreeWidget并覆盖重写其resizeEvent函数来实现自定义滚动条的显示和隐藏。在该函数中,可以根据QTreeWidget的大小和内容的高度与宽度计算出是否需要显示滚动条,然后通过调用setVerticalScrollBarPolicy和setHorizontalScrollBarPolicy函数来设置QTreeWidget的垂直和水平滚动条的可见性。以下是实现代码:
```cpp
class CustomTreeWidget : public QTreeWidget
{
public:
CustomTreeWidget(QWidget *parent = nullptr)
: QTreeWidget(parent)
{
// 设置垂直和水平滚动条的样式
m_scrollBarStyle = "QScrollBar:vertical, QScrollBar:horizontal{"
"background-color: #F5F5F5;"
"border: none;"
"width: 10px;"
"margin: 0px 0px 0px 0px;"
"}"
"QScrollBar::handle:vertical, QScrollBar::handle:horizontal{"
"background-color: #C0C0C0;"
"border-radius: 5px;"
"min-height: 30px;"
"}"
"QScrollBar::add-line:vertical, QScrollBar::add-line:horizontal,"
"QScrollBar::sub-line:vertical, QScrollBar::sub-line:horizontal{"
"background: none;"
"}"
"QScrollBar::add-page:vertical, QScrollBar::add-page:horizontal,"
"QScrollBar::sub-page:vertical, QScrollBar::sub-page:horizontal{"
"background: none;"
"}";
// 设置滚动条样式表
m_vScrollBar.setStyleSheet(m_scrollBarStyle);
m_hScrollBar.setStyleSheet(m_scrollBarStyle);
}
protected:
void resizeEvent(QResizeEvent *event) override
{
QTreeWidget::resizeEvent(event);
bool showVerticalScrollBar = verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOn ||
(verticalScrollBarPolicy() == Qt::ScrollBarAsNeeded &&
contentsSize().height() > viewport()->height());
bool showHorizontalScrollBar = horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn ||
(horizontalScrollBarPolicy() == Qt::ScrollBarAsNeeded &&
contentsSize().width() > viewport()->width());
if (showVerticalScrollBar) {
// 显示垂直滚动条
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_vScrollBar.setParent(viewport());
m_vScrollBar.setGeometry(viewport()->width() - m_vScrollBar.width(), 0,
m_vScrollBar.width(), viewport()->height());
m_vScrollBar.setRange(0, contentsSize().height() - viewport()->height());
m_vScrollBar.setSingleStep(verticalScrollBar()->singleStep());
m_vScrollBar.setPageStep(viewport()->height());
m_vScrollBar.setValue(verticalScrollBar()->value());
connect(verticalScrollBar(), &QScrollBar::valueChanged, &m_vScrollBar, &QScrollBar::setValue);
connect(&m_vScrollBar, &QScrollBar::valueChanged, verticalScrollBar(), &QScrollBar::setValue);
} else {
// 隐藏垂直滚动条
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_vScrollBar.setParent(nullptr);
}
if (showHorizontalScrollBar) {
// 显示水平滚动条
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_hScrollBar.setParent(viewport());
m_hScrollBar.setGeometry(0, viewport()->height() - m_hScrollBar.height(),
viewport()->width(), m_hScrollBar.height());
m_hScrollBar.setRange(0, contentsSize().width() - viewport()->width());
m_hScrollBar.setSingleStep(horizontalScrollBar()->singleStep());
m_hScrollBar.setPageStep(viewport()->width());
m_hScrollBar.setValue(horizontalScrollBar()->value());
connect(horizontalScrollBar(), &QScrollBar::valueChanged, &m_hScrollBar, &QScrollBar::setValue);
connect(&m_hScrollBar, &QScrollBar::valueChanged, horizontalScrollBar(), &QScrollBar::setValue);
} else {
// 隐藏水平滚动条
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_hScrollBar.setParent(nullptr);
}
}
private:
QString m_scrollBarStyle;
QScrollBar m_vScrollBar{Qt::Vertical};
QScrollBar m_hScrollBar{Qt::Horizontal};
};
```
在使用时,只需要将原本的QTreeWidget替换为CustomTreeWidget即可:
```cpp
CustomTreeWidget *treeWidget = new CustomTreeWidget(parent);
```
阅读全文