Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Not able to add horizontal scrollbar to listWidget.
Forum Updated to NodeBB v4.3 + New Features

Not able to add horizontal scrollbar to listWidget.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 610 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Krpadia123
    wrote on last edited by Krpadia123
    #1

    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)
    bfde6b05-0a66-4f59-b2c1-36ae96143502-Screenshot (120).png

    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.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      ThThoma
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Krpadia123
        wrote on last edited by
        #3

        Hi @ThThoma , sorry for the typo but it still doesnt work

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved