Corvus
Corvus

Reputation: 63

Why are my QListWidgetItems not taking up the right amount of space?

The last QListWidgetItem has its size improperly set and will overflow if it should have moved to the next row because it would not fit. Sometimes this last item is also only selectable on the right-hand side of the item.

The QListWidgetItems seemed to have been acting fine until I set a widget inside the items.

The issue appearing depends on whether the tags are added before or after the call to TagList->show(), if the tags are added after the call to TagList->show() in the below code, the right half of "tag seven" will not be right-clickable.

#include <QApplication>

#include "taglist.h"

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    TagList tagList;

    /* If Tags are added here they will work fine.
    tagList.addTag("tag one");
    tagList.addTag("tag two");
    tagList.addTag("tag three");
    tagList.addTag("tag four");
    tagList.addTag("tag five");
    tagList.addTag("tag six");
    tagList.addTag("tag seven");
    */

    // Which side of the call to show() the tags are added will
    // determine whether they work as expected or not.
    tagList.show();

    // If tags are added here "tag seven" will break.
    tagList.addTag("tag one");
    tagList.addTag("tag two");
    tagList.addTag("tag three");
    tagList.addTag("tag four");
    tagList.addTag("tag five");
    tagList.addTag("tag six");
    tagList.addTag("tag seven");

    return app.exec();
}

taglist.h

#ifndef TAGLIST_H
#define TAGLIST_H

#include <QWidget>
#include <QStackedLayout>
#include <QListWidget>
#include <QMenu>
#include <QString>
#include <QLabel>

class TagList : public QWidget
{
    Q_OBJECT

public:
    explicit TagList(QWidget *parent = nullptr);

    void addTag(QString name);

private:
    QListWidget* viewingArea;

public slots:
    void showContextMenu(const QPoint& point);
    void removeTag();
};

#endif // TAGLIST_H

taglist.cpp

#include "taglist.h"

TagList::TagList(QWidget *parent) : QWidget(parent)
{
    QStackedLayout* layout = new QStackedLayout;
    this->setLayout(layout);

    viewingArea = new QListWidget;
    viewingArea->setContextMenuPolicy(Qt::CustomContextMenu);
    viewingArea->setViewMode(QListView::IconMode);
    viewingArea->setSpacing(4);
    connect(viewingArea, SIGNAL (customContextMenuRequested(QPoint)), this, SLOT (showContextMenu(QPoint)));
    layout->addWidget(viewingArea);
}

void TagList::addTag(QString name)
{
    QListWidgetItem* item = new QListWidgetItem;

    QLabel* label = new QLabel(name);
    label->setStyleSheet("background-color: #A9DFBF; padding: 4px 2px 4px 3px; border-radius: 3px;");

    viewingArea->addItem(item);
    item->setSizeHint(label->sizeHint());
    viewingArea->setItemWidget(item, label);
}

void TagList::showContextMenu(const QPoint& point)
{
    if (!viewingArea->selectedItems().isEmpty()) {
        QPoint position = viewingArea->mapToGlobal(point);

        QMenu contextMenu;
        contextMenu.addAction("Remove", this, SLOT (removeTag()));
        contextMenu.exec(position);
    }
}

void TagList::removeTag()
{
    delete viewingArea->takeItem(viewingArea->currentRow());
}

tag overflowing

Tag can be seen flowing out of panel.

pushed to next line

But if another item is added, it will appear (as expected) on the next line.

can't select

Right clicking on the tag here will not open the context-menu.

can select only on tiny area

Only the far left of the item can be interacted with.

Upvotes: 1

Views: 187

Answers (1)

cbuchart
cbuchart

Reputation: 11555

You must set the size hint before adding the item.

QListWidgetItem* item = new QListWidgetItem();
item->setSizeHint(label->sizeHint());
viewingArea->addItem(item);
viewingArea->setItemWidget(item, label);

Upvotes: 1

Related Questions