Not able to add horizontal scrollbar to listWidget.
-
Hello,
I am trying to add vertical and horizontal scrollbar to my listwidget, I wanted to show horizontal scrollbar whenever my text goes beyond the border, I tried keeping horizontal bar always on but no luck, any help is very much appreciated.
Code:
QScrollBar* makeItVertical(QWidget * pParent) { QScrollBar * pScrollBar = new QScrollBar(Qt::Vertical, pParent); return pScrollBar; } QScrollBar* makeItHorizontal(QWidget * pParent) { QScrollBar * pScrollBar = new QScrollBar(Qt::Horizontal, pParent); return pScrollBar; } ////////////////////////////////// Custom List wiget //////////////////////////////// void QCustomListWidgetItem::setup() { m_pItemLabel = new QLabel(QString::fromStdString(m_pJobName), this); m_pItemLabel->setObjectName("messageLabel"); m_pItemLabel->setAlignment(Qt::AlignVCenter); m_pSpinnnerLabel = new QLabel(this); QMovie* movie = new QMovie(QString::fromStdString("D:/temp/loadingSpinner.gif"), QByteArray(), this); movie->setScaledSize(QSize(25, 25)); m_pSpinnnerLabel->setMovie(movie); m_pSpinnnerLabel->show(); movie->start(); } void QCustomListWidgetItem::layout() { QHBoxLayout* listWidgetLayout = new QHBoxLayout(this); listWidgetLayout->setContentsMargins(0, 0, 0, 0); listWidgetLayout->setSpacing(10); listWidgetLayout->addWidget(m_pSpinnnerLabel, 0, Qt::AlignLeft); listWidgetLayout->addWidget(m_pItemLabel, 1, Qt::AlignLeft); setLayout(listWidgetLayout); setFixedHeight(25); } QCustomListWidgetItem::QCustomListWidgetItem(std::string labelText, QWidget* parent): QWidget(parent), m_pJobName(labelText) { setup(); layout(); } /////////////////////////// Dialog////////////////////////////////////////// void UploadJobListDialog::setup() { setObjectName(QString("uploadJobListDialog")); m_pContent = new QFrame(this); m_pContent->setContentsMargins(0, 0, 0, 0); m_pContent->setObjectName("Content"); m_pHeader = new QFrame(this); m_pHeader->setContentsMargins(0, 0, 0, 0); m_pHeader->setObjectName("Header"); m_pBody = new QFrame(this); m_pBody->setContentsMargins(0, 0, 0, 0); m_pBody->setObjectName("Body"); m_pHeaderLabel = new QLabel(QString::fromStdString("Sample"), this); m_pHeaderLabel->setObjectName("titleLabel"); // Status Indicator Bar m_pStatusIndicatorBar = new QFrame(this); m_pStatusIndicatorBar->setObjectName("statusIndicatorInfo"); m_pStatusIndicatorBar->setFrameShape(QFrame::Box); m_pStatusIndicatorBar->setFixedWidth(4); // Seperator m_pFrameDivider = new QFrame(this); m_pFrameDivider->setFrameShape(QFrame::HLine); m_pFrameDivider->setObjectName("titleSeparator"); // Basic setup setWindowModality(Qt::ApplicationModal); setWindowFlags(windowFlags() | Qt::FramelessWindowHint); setMinimumWidth(500); addListWidgetItems(); } void UploadJobListDialog::layout() { int nLeftSpacing = 16; int nRightSpacing = 16; int nSpacing = 8; int nBottomSpacing = 10; // Header layout spacing int nHeaderTopSpacing = 12; QHBoxLayout* headerLayout = new QHBoxLayout(m_pHeader); headerLayout->setContentsMargins(QMargins(nLeftSpacing, nHeaderTopSpacing, nRightSpacing, nBottomSpacing)); headerLayout->setSpacing(nSpacing); headerLayout->addWidget(m_pHeaderIcon, 0, Qt::AlignLeft | Qt::AlignVCenter); headerLayout->addWidget(m_pHeaderLabel, 0, Qt::AlignVCenter); headerLayout->addStretch(); QVBoxLayout* bodyLayout = new QVBoxLayout(m_pBody); bodyLayout->setContentsMargins(nLeftSpacing, 0, nRightSpacing, nBottomSpacing); bodyLayout->setSpacing(0); bodyLayout->addWidget(m_pListWidget, 0, Qt::AlignVCenter); QVBoxLayout* contentLayout = new QVBoxLayout(m_pContent); contentLayout->setContentsMargins(0, 0, 0, 0); contentLayout->addWidget(m_pHeader); contentLayout->addWidget(m_pFrameDivider); contentLayout->addWidget(m_pBody); QHBoxLayout* mainLayout = new QHBoxLayout(this); mainLayout->setContentsMargins(0, 0, 0, 0); mainLayout->setSpacing(0); mainLayout->addWidget(m_pStatusIndicatorBar); mainLayout->addWidget(m_pContent); setLayout(mainLayout); } UploadJobListDialog::UploadJobListDialog(std::vector<std::string> jobNames, QWidget* parent) : QDialog(parent) , m_pJobNames(jobNames) { setup(); layout(); } void UploadJobListDialog::addListWidgetItems() { m_pListWidget = new QListWidget(this); QString listStyle( "QListWidget::item:hover," "QListWidget::item:disabled:hover," "QListWidget::item:hover:!active," "{background:#FFFFFF;}"); m_pListWidget->setStyleSheet(listStyle); m_pListWidget->setFixedSize(QSize(470, 150)); m_pListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_pListWidget->setHorizontalScrollBar(makeItHorizontal(m_pListWidget)); m_pListWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); m_pListWidget->setVerticalScrollBar(makeItVertical(m_pListWidget)); for (const auto& jobName: m_pJobNames) { QListWidgetItem* listWidgetItem = new QListWidgetItem(m_pListWidget); QCustomListWidgetItem* customListWidget = new QCustomListWidgetItem(jobName,m_pListWidget); listWidgetItem->setSizeHint(customListWidget->size()); listWidgetItem->setFlags(listWidgetItem->flags() & ~Qt::ItemIsSelectable); m_pListWidget->addItem(listWidgetItem); m_pListWidget->setItemWidget(listWidgetItem, customListWidget); } } int main(int argc, char *argv[]) { QApplication a(argc, argv); std::vector<std::string> jobs; for(int i = 0; i < 15; i++) { jobs.push_back((std::to_string(i) + "Wigglslkfkdsfkfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe") + std::to_string(i)); } UploadJobListDialog obj(jobs); obj.exec(); return 0; }
My output: (No Horizontal bar)
Can someone please help, what I am doing wrong here?
PS: I am very new to QT, extremely sorry If I made stupid mistake or forget to mention basic details.
-
Hello,
QListWidget inherits from QAbstractScrollArea at some point. So by default it should show the scroll bars (both horizontal and vertical) when needed. You can just change the policy if you need a different behavior. So there is no need to use setHorizontalScrollBar() etc.
That being said, there seems to be an issue here :
QScrollBar* makeItVertical(QWidget * pParent) { QScrollBar * pScrollBar = new QScrollBar(Qt::Horizontal, pParent); //Qt:Vertical here? return pScrollBar; } QScrollBar* makeItHorizontal(QWidget * pParent) { QScrollBar * pScrollBar = new QScrollBar(Qt::Horizontal, pParent); return pScrollBar; }
I'm not sure if it will make a difference. But try leaving the default behavior of QListWidget scrollbars first.
-
Hi @ThThoma , sorry for the typo but it still doesnt work
-
Hi,
You seem to have a pretty convoluted setup and in fact using widgets to show content in your QListWidget which is a wrong thing to do.
If you need special rendering, use a custom QStyledItemDelegate.
As a first step, drop the setCellWidget and custom item class. Just fill your cells with your long strings and you should have the effect you want.