Avoid assigning wrong window icon to settings tab with no icon

* Reset the settings page's widget's parent so `windowIcon()` won't return
  the parent's icon
* Improve the coding style a little bit
This commit is contained in:
Martchus 2023-02-11 16:21:19 +01:00
parent 41ddfc08b7
commit 1dded32095
1 changed files with 18 additions and 9 deletions

View File

@ -224,29 +224,35 @@ void SettingsDialog::updateTabWidget()
return; return;
} }
m_ui->pagesTabWidget->setUpdatesEnabled(false); m_ui->pagesTabWidget->setUpdatesEnabled(false);
const QString searchKeyWord = m_ui->filterLineEdit->text();
const auto searchKeyWord = m_ui->filterLineEdit->text();
int index = 0, pageIndex = 0; int index = 0, pageIndex = 0;
for (OptionPage *const page : m_currentCategory->pages()) { for (OptionPage *const page : m_currentCategory->pages()) {
if (page->matches(searchKeyWord)) { if (page->matches(searchKeyWord)) {
// ensure the page's widget has no parent anymore; otherwise windowIcon() might return the parent's icon
auto *const widget = page->widget();
widget->setParent(nullptr);
// add the widget to the tab widget within a scroll area
QScrollArea *scrollArea; QScrollArea *scrollArea;
if (index < m_ui->pagesTabWidget->count()) { if (index < m_ui->pagesTabWidget->count()) {
scrollArea = qobject_cast<QScrollArea *>(m_ui->pagesTabWidget->widget(index)); scrollArea = qobject_cast<QScrollArea *>(m_ui->pagesTabWidget->widget(index));
scrollArea->takeWidget(); scrollArea->takeWidget();
m_ui->pagesTabWidget->setTabText(index, page->widget()->windowTitle()); m_ui->pagesTabWidget->setTabText(index, widget->windowTitle());
m_ui->pagesTabWidget->setTabIcon(index, page->widget()->windowIcon()); m_ui->pagesTabWidget->setTabIcon(index, widget->windowIcon());
} else { } else {
scrollArea = new QScrollArea(m_ui->pagesTabWidget); scrollArea = new QScrollArea(m_ui->pagesTabWidget);
scrollArea->setFrameStyle(QFrame::NoFrame); scrollArea->setFrameStyle(QFrame::NoFrame);
scrollArea->setBackgroundRole(QPalette::Base); scrollArea->setBackgroundRole(QPalette::Base);
scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
scrollArea->setWidgetResizable(true); scrollArea->setWidgetResizable(true);
m_ui->pagesTabWidget->addTab(scrollArea, page->widget()->windowTitle()); m_ui->pagesTabWidget->addTab(scrollArea, widget->windowTitle());
m_ui->pagesTabWidget->setTabIcon(index, page->widget()->windowIcon()); m_ui->pagesTabWidget->setTabIcon(index, widget->windowIcon());
} }
if (page->widget()->layout()) { if (auto *const layout = widget->layout()) {
page->widget()->layout()->setAlignment(Qt::AlignTop | Qt::AlignLeft); layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
} }
scrollArea->setWidget(page->widget()); scrollArea->setWidget(widget);
++index; ++index;
} }
if (pageIndex == m_currentCategory->currentIndex()) { if (pageIndex == m_currentCategory->currentIndex()) {
@ -254,12 +260,15 @@ void SettingsDialog::updateTabWidget()
} }
++pageIndex; ++pageIndex;
} }
// remove surplus tabs
while (index < m_ui->pagesTabWidget->count()) { while (index < m_ui->pagesTabWidget->count()) {
QScrollArea *const scrollArea = qobject_cast<QScrollArea *>(m_ui->pagesTabWidget->widget(index)); auto *const scrollArea = qobject_cast<QScrollArea *>(m_ui->pagesTabWidget->widget(index));
scrollArea->takeWidget(); scrollArea->takeWidget();
m_ui->pagesTabWidget->removeTab(index); m_ui->pagesTabWidget->removeTab(index);
delete scrollArea; delete scrollArea;
} }
m_ui->pagesTabWidget->tabBar()->setHidden(!m_tabBarAlwaysVisible && m_ui->pagesTabWidget->count() == 1); m_ui->pagesTabWidget->tabBar()->setHidden(!m_tabBarAlwaysVisible && m_ui->pagesTabWidget->count() == 1);
m_ui->pagesTabWidget->setUpdatesEnabled(true); m_ui->pagesTabWidget->setUpdatesEnabled(true);
} }