Handle potential horizontal scrollbar when resizing StringListEdit

This commit is contained in:
WarmUpTill 2023-07-21 22:39:30 +02:00 committed by WarmUpTill
parent fe3586b6f4
commit 47d6d4ac06
3 changed files with 43 additions and 8 deletions

View File

@ -112,6 +112,17 @@ void StringListEdit::SetMaxStringSize(int size)
_maxStringSize = size;
}
void StringListEdit::showEvent(QShowEvent *e)
{
QWidget::showEvent(e);
// This is necessary as the list viewport might not be updated yet
// while the list was hidden.
// Thus, previous calls to SetListSize() might not have resized the
// widget correctly, for example due to not regarding the horizontal
// scrollbar yet.
SetListSize();
}
void StringListEdit::Add()
{
std::string name;
@ -129,7 +140,9 @@ void StringListEdit::Add()
item->setData(Qt::UserRole, string);
_stringList << string;
SetListSize();
// Delay resizing to make sure the list viewport was already updated
QTimer::singleShot(0, this, [this]() { SetListSize(); });
StringListChanged(_stringList);
}
@ -147,7 +160,9 @@ void StringListEdit::Remove()
return;
}
delete item;
SetListSize();
// Delay resizing to make sure the list viewport was already updated
QTimer::singleShot(0, this, [this]() { SetListSize(); });
StringListChanged(_stringList);
}
@ -195,6 +210,9 @@ void StringListEdit::Clicked(QListWidgetItem *item)
int idx = _list->currentRow();
_stringList[idx] = string;
// Delay resizing to make sure the list viewport was already updated
QTimer::singleShot(0, this, [this]() { SetListSize(); });
StringListChanged(_stringList);
}

View File

@ -29,6 +29,9 @@ public:
void SetStringList(const StringList &);
void SetMaxStringSize(int);
protected:
void showEvent(QShowEvent *);
private slots:
void Add();
void Remove();

View File

@ -24,6 +24,7 @@
#include <QMainWindow>
#include <QScreen>
#include <QSpacerItem>
#include <QScrollBar>
#include <unordered_map>
#include <regex>
#include <set>
@ -1108,19 +1109,32 @@ bool listMoveDown(QListWidget *list)
return true;
}
static int getHorizontalScrollBarHeight(QListWidget *list)
{
if (!list) {
return 0;
}
auto horizontalScrollBar = list->horizontalScrollBar();
if (!horizontalScrollBar || !horizontalScrollBar->isVisible()) {
return 0;
}
return horizontalScrollBar->height();
}
void SetHeightToContentHeight(QListWidget *list)
{
auto nrItems = list->count();
if (nrItems == 0) {
list->setMaximumHeight(0);
list->setMinimumHeight(0);
} else {
int height =
(list->sizeHintForRow(0) + list->spacing()) * nrItems +
2 * list->frameWidth();
list->setMinimumHeight(height);
list->setMaximumHeight(height);
return;
}
int scrollBarHeight = getHorizontalScrollBarHeight(list);
int height = (list->sizeHintForRow(0) + list->spacing()) * nrItems +
2 * list->frameWidth() + scrollBarHeight;
list->setMinimumHeight(height);
list->setMaximumHeight(height);
}
bool DoubleEquals(double left, double right, double epsilon)