<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Not able to add horizontal scrollbar to listWidget.]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">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.</p>
<p dir="auto"><strong>Code:</strong></p>
<pre><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-&gt;setObjectName("messageLabel");
    m_pItemLabel-&gt;setAlignment(Qt::AlignVCenter);
    m_pSpinnnerLabel = new QLabel(this);
    QMovie* movie =
        new QMovie(QString::fromStdString("D:/temp/loadingSpinner.gif"), QByteArray(), this);

    movie-&gt;setScaledSize(QSize(25, 25));
    m_pSpinnnerLabel-&gt;setMovie(movie);
    m_pSpinnnerLabel-&gt;show();
    movie-&gt;start();
}

void QCustomListWidgetItem::layout()
{
    QHBoxLayout* listWidgetLayout = new QHBoxLayout(this);
    listWidgetLayout-&gt;setContentsMargins(0, 0, 0, 0);
    listWidgetLayout-&gt;setSpacing(10);
    listWidgetLayout-&gt;addWidget(m_pSpinnnerLabel, 0, Qt::AlignLeft);
    listWidgetLayout-&gt;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-&gt;setContentsMargins(0, 0, 0, 0);
    m_pContent-&gt;setObjectName("Content");

    m_pHeader = new QFrame(this);
    m_pHeader-&gt;setContentsMargins(0, 0, 0, 0);
    m_pHeader-&gt;setObjectName("Header");

    m_pBody = new QFrame(this);
    m_pBody-&gt;setContentsMargins(0, 0, 0, 0);
    m_pBody-&gt;setObjectName("Body");

    m_pHeaderLabel = new QLabel(QString::fromStdString("Sample"), this);
    m_pHeaderLabel-&gt;setObjectName("titleLabel");

    // Status Indicator Bar
    m_pStatusIndicatorBar = new QFrame(this);
    m_pStatusIndicatorBar-&gt;setObjectName("statusIndicatorInfo");
    m_pStatusIndicatorBar-&gt;setFrameShape(QFrame::Box);
    m_pStatusIndicatorBar-&gt;setFixedWidth(4);

    // Seperator
    m_pFrameDivider = new QFrame(this);
    m_pFrameDivider-&gt;setFrameShape(QFrame::HLine);
    m_pFrameDivider-&gt;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-&gt;setContentsMargins(QMargins(nLeftSpacing, nHeaderTopSpacing, nRightSpacing, nBottomSpacing));
    headerLayout-&gt;setSpacing(nSpacing);
    headerLayout-&gt;addWidget(m_pHeaderIcon, 0, Qt::AlignLeft | Qt::AlignVCenter);
    headerLayout-&gt;addWidget(m_pHeaderLabel, 0, Qt::AlignVCenter);
    headerLayout-&gt;addStretch();

    QVBoxLayout* bodyLayout = new QVBoxLayout(m_pBody);
    bodyLayout-&gt;setContentsMargins(nLeftSpacing, 0, nRightSpacing, nBottomSpacing);
    bodyLayout-&gt;setSpacing(0);
    bodyLayout-&gt;addWidget(m_pListWidget, 0, Qt::AlignVCenter);

    QVBoxLayout* contentLayout = new QVBoxLayout(m_pContent);
    contentLayout-&gt;setContentsMargins(0, 0, 0, 0);
    contentLayout-&gt;addWidget(m_pHeader);
    contentLayout-&gt;addWidget(m_pFrameDivider);
    contentLayout-&gt;addWidget(m_pBody);

    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout-&gt;setContentsMargins(0, 0, 0, 0);
    mainLayout-&gt;setSpacing(0);
    mainLayout-&gt;addWidget(m_pStatusIndicatorBar);
    mainLayout-&gt;addWidget(m_pContent);

    setLayout(mainLayout);
}



UploadJobListDialog::UploadJobListDialog(std::vector&lt;std::string&gt; 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-&gt;setStyleSheet(listStyle);
    m_pListWidget-&gt;setFixedSize(QSize(470, 150));
    m_pListWidget-&gt;setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    m_pListWidget-&gt;setHorizontalScrollBar(makeItHorizontal(m_pListWidget));
    m_pListWidget-&gt;setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    m_pListWidget-&gt;setVerticalScrollBar(makeItVertical(m_pListWidget));

    for (const auto&amp; jobName: m_pJobNames)
    {
        QListWidgetItem* listWidgetItem = new QListWidgetItem(m_pListWidget);
        QCustomListWidgetItem* customListWidget = new QCustomListWidgetItem(jobName,m_pListWidget);
        listWidgetItem-&gt;setSizeHint(customListWidget-&gt;size());
        listWidgetItem-&gt;setFlags(listWidgetItem-&gt;flags() &amp; ~Qt::ItemIsSelectable);
        m_pListWidget-&gt;addItem(listWidgetItem);
        m_pListWidget-&gt;setItemWidget(listWidgetItem, customListWidget);
    }
}


int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   std::vector&lt;std::string&gt; jobs;
   for(int i = 0; i &lt; 15; i++)
   {
       jobs.push_back((std::to_string(i) + "Wigglslkfkdsfkfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe") + std::to_string(i));
   }

   UploadJobListDialog obj(jobs);
   obj.exec();
   return 0;
}
</code></pre>
<p dir="auto"><strong>My output: (No Horizontal bar)</strong><br />
<img src="https://ddgobkiprc33d.cloudfront.net/d76828f3-06c1-4817-a681-4196c562c11f.png" alt="bfde6b05-0a66-4f59-b2c1-36ae96143502-Screenshot (120).png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Can someone please help, what I am doing wrong here?</p>
<p dir="auto"><strong>PS: I am very new to QT, extremely sorry If I made stupid mistake or forget to mention basic details.</strong></p>
]]></description><link>https://forum.qt.io/topic/142060/not-able-to-add-horizontal-scrollbar-to-listwidget</link><generator>RSS for Node</generator><lastBuildDate>Sat, 16 May 2026 13:45:15 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/142060.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 09 Jan 2023 07:30:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Not able to add horizontal scrollbar to listWidget. on Mon, 09 Jan 2023 20:26:13 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">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.</p>
<p dir="auto">If you need special rendering, use a custom QStyledItemDelegate.</p>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/743168</link><guid isPermaLink="true">https://forum.qt.io/post/743168</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 09 Jan 2023 20:26:13 GMT</pubDate></item><item><title><![CDATA[Reply to Not able to add horizontal scrollbar to listWidget. on Mon, 09 Jan 2023 08:48:56 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/ththoma">@<bdi>ThThoma</bdi></a> , sorry for the typo but it still doesnt work</p>
]]></description><link>https://forum.qt.io/post/743064</link><guid isPermaLink="true">https://forum.qt.io/post/743064</guid><dc:creator><![CDATA[Krpadia123]]></dc:creator><pubDate>Mon, 09 Jan 2023 08:48:56 GMT</pubDate></item><item><title><![CDATA[Reply to Not able to add horizontal scrollbar to listWidget. on Mon, 09 Jan 2023 08:41:08 GMT]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">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 <strong>setHorizontalScrollBar()</strong> etc.</p>
<p dir="auto">That being said, there seems to be an issue here :</p>
<pre><code>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;
}
</code></pre>
<p dir="auto">I'm not sure if it will make a difference. But try leaving the default behavior of QListWidget scrollbars first.</p>
]]></description><link>https://forum.qt.io/post/743063</link><guid isPermaLink="true">https://forum.qt.io/post/743063</guid><dc:creator><![CDATA[ThThoma]]></dc:creator><pubDate>Mon, 09 Jan 2023 08:41:08 GMT</pubDate></item></channel></rss>