Qt: Restrict trophy manager combo box size

This commit is contained in:
Megamouse 2018-05-19 23:37:32 +02:00 committed by Ivan
parent 05efa7d957
commit 4c03348e60
3 changed files with 29 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#include "qt_utils.h"
#include <QApplication>
#include <QBitmap>
#include <QFontMetrics>
#include <QPainter>
#include <QScreen>
@ -156,6 +157,27 @@ namespace gui
return image.copy(QRect(QPoint(w_max, h_max), QPoint(w_min, h_min)));
}
// taken from https://stackoverflow.com/a/30818424/8353754
// because size policies won't work as expected (see similar bugs in Qt bugtracker)
void resize_combo_box_view(QComboBox* combo)
{
int max_width = 0;
QFontMetrics font_metrics(combo->font());
for (int i = 0; i < combo->count(); ++i)
{
max_width = std::max(max_width, font_metrics.width(combo->itemText(i)));
}
if (combo->view()->minimumWidth() < max_width)
{
// add scrollbar width and margin
max_width += combo->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
max_width += combo->view()->autoScrollMargin();
combo->view()->setMinimumWidth(max_width);
}
};
void update_table_item_count(QTableWidget* table)
{
if (!table)

View File

@ -2,6 +2,7 @@
#include "stdafx.h"
#include <QtCore>
#include <QComboBox>
#include <QFont>
#include <QIcon>
#include <QLabel>
@ -38,6 +39,9 @@ namespace gui
// Returns the part of the image loaded from path that is inside the bounding box of its opaque areas
QImage get_opaque_image_area(const QString& path);
// Workaround: resize the dropdown combobox items
void resize_combo_box_view(QComboBox* combo);
// Recalculates a table's item count based on the available visible space and fills it with empty items
void update_table_item_count(QTableWidget* table);

View File

@ -58,6 +58,7 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
// Game chooser combo box
m_game_combo = new QComboBox();
m_game_combo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
// Game progression label
m_game_progress = new QLabel(tr("Progress: %1% (%2/%3)").arg(0).arg(0).arg(0));
@ -148,6 +149,8 @@ trophy_manager_dialog::trophy_manager_dialog(std::shared_ptr<gui_settings> gui_s
m_game_table->setItem(i, GameColumns::GameProgress, new custom_table_widget_item(progress, Qt::UserRole, percentage));
}
gui::utils::resize_combo_box_view(m_game_combo);
m_game_table->setSortingEnabled(true); // Enable sorting only after using setItem calls
// Checkboxes to control dialog