Unsolved To much space between spinner label and label in custom list widget.
-
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](
image url)Code:
- 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; }
- 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.
-
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); ...
-
Neat! Thank You!
-
@Krpadia123 Your welcome. Please do not forget to set the state to solved !