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. To much space between spinner label and label in custom list widget.
Forum Updated to NodeBB v4.3 + New Features

To much space between spinner label and label in custom list widget.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 280 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
    #1

    I am trying to add loading spinner and some text besides it in custom list widget, but somehow I can see lot of space between them, I tried various alignment but none worked.
    Please follow below image.

    ![alt text](
    54f6c439-f568-45b5-8dba-db84a1ef7a4c-Screenshot (109).png image url)

    Code:

    1. main.cpp
    #include "mainwindow.h"
    #include <QApplication>
    #include <QHBoxLayout>
    #include <unistd.h>
    #include <QMovie>
    
    QCustomListWidgetItem::QCustomListWidgetItem(std::string labelText, QWidget* parent)
    : QWidget(parent), m_pJobName(labelText)
    {
        setup();
        layout();
    }
    
    void QCustomListWidgetItem::setup()
    {
        m_pItemLabel = new QLabel(QString::fromStdString(m_pJobName), this);
        m_pItemLabel->setObjectName("messageLabel");
        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(0);
        listWidgetLayout->addWidget(m_pSpinnnerLabel, 0, Qt::AlignLeft);
        listWidgetLayout->addWidget(m_pItemLabel, 0, Qt::AlignLeft);
        setLayout(listWidgetLayout);
        setFixedHeight(25);
    }
    
    UploadJobListDialog::UploadJobListDialog(std::vector<std::string> jobNames, QWidget* parent)
        : QDialog(parent) , m_pJobNames(jobNames)
    {
        addListWidgetItems();
    }
    
    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));
    
        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 < 5; i++)
       {
           jobs.push_back(std::to_string(i));
       }
    
       UploadJobListDialog obj(jobs);
       obj.exec();
       return 0;
    }
    
    
    1. mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QtWidgets/QDialog>
    #include <QtWidgets/QListWidget>
    #include <QLabel>
    #include <QObject>
    
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    
    //Custom WidgetItem
    class UploadJobListDialog : public QDialog
    {
        Q_OBJECT
    public:
        explicit UploadJobListDialog(std::vector<std::string> jobNames, QWidget* parent = NULL);
    
    private:
        void addListWidgetItems();
        QListWidget* m_pListWidget = nullptr;
    
        std::vector<std::string> m_pJobNames = {};
    
    };
    
    class QCustomListWidgetItem : public QWidget
    {
        Q_OBJECT
        public:
            explicit QCustomListWidgetItem(std::string labelText, QWidget* parent = NULL);
        void setup();
        void layout();
    
        private:
    
            QLabel* m_pItemLabel = nullptr;
            QLabel* m_pSpinnnerLabel = nullptr;
            std::string m_pJobName;
    };
    
    

    Why I am getting this large space in between, am I doing something horrible?

    I am new to QT, any help is very much appreciated.

    Gojir4G 1 Reply Last reply
    0
    • K Krpadia123

      I am trying to add loading spinner and some text besides it in custom list widget, but somehow I can see lot of space between them, I tried various alignment but none worked.
      Please follow below image.

      ![alt text](
      54f6c439-f568-45b5-8dba-db84a1ef7a4c-Screenshot (109).png image url)

      Code:

      1. main.cpp
      #include "mainwindow.h"
      #include <QApplication>
      #include <QHBoxLayout>
      #include <unistd.h>
      #include <QMovie>
      
      QCustomListWidgetItem::QCustomListWidgetItem(std::string labelText, QWidget* parent)
      : QWidget(parent), m_pJobName(labelText)
      {
          setup();
          layout();
      }
      
      void QCustomListWidgetItem::setup()
      {
          m_pItemLabel = new QLabel(QString::fromStdString(m_pJobName), this);
          m_pItemLabel->setObjectName("messageLabel");
          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(0);
          listWidgetLayout->addWidget(m_pSpinnnerLabel, 0, Qt::AlignLeft);
          listWidgetLayout->addWidget(m_pItemLabel, 0, Qt::AlignLeft);
          setLayout(listWidgetLayout);
          setFixedHeight(25);
      }
      
      UploadJobListDialog::UploadJobListDialog(std::vector<std::string> jobNames, QWidget* parent)
          : QDialog(parent) , m_pJobNames(jobNames)
      {
          addListWidgetItems();
      }
      
      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));
      
          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 < 5; i++)
         {
             jobs.push_back(std::to_string(i));
         }
      
         UploadJobListDialog obj(jobs);
         obj.exec();
         return 0;
      }
      
      
      1. mainwindow.h
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QtWidgets/QDialog>
      #include <QtWidgets/QListWidget>
      #include <QLabel>
      #include <QObject>
      
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
      };
      #endif // MAINWINDOW_H
      
      
      //Custom WidgetItem
      class UploadJobListDialog : public QDialog
      {
          Q_OBJECT
      public:
          explicit UploadJobListDialog(std::vector<std::string> jobNames, QWidget* parent = NULL);
      
      private:
          void addListWidgetItems();
          QListWidget* m_pListWidget = nullptr;
      
          std::vector<std::string> m_pJobNames = {};
      
      };
      
      class QCustomListWidgetItem : public QWidget
      {
          Q_OBJECT
          public:
              explicit QCustomListWidgetItem(std::string labelText, QWidget* parent = NULL);
          void setup();
          void layout();
      
          private:
      
              QLabel* m_pItemLabel = nullptr;
              QLabel* m_pSpinnnerLabel = nullptr;
              std::string m_pJobName;
      };
      
      

      Why I am getting this large space in between, am I doing something horrible?

      I am new to QT, any help is very much appreciated.

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      Hi @Krpadia123, try adding a stretch with a factor of 1 for your label so it takes the whole space and not only half.

      ...
      listWidgetLayout->addWidget(m_pSpinnnerLabel, 0, Qt::AlignLeft);
      listWidgetLayout->addWidget(m_pItemLabel, 1, Qt::AlignLeft);
      setLayout(listWidgetLayout);
      ...
      
      1 Reply Last reply
      2
      • K Offline
        K Offline
        Krpadia123
        wrote on last edited by
        #3

        Neat! Thank You!

        Gojir4G 1 Reply Last reply
        0
        • K Krpadia123

          Neat! Thank You!

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @Krpadia123 Your welcome. Please do not forget to set the state to solved !

          1 Reply Last reply
          0

          • Login

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