搜索框优化
优化前:
void MediaListview::search(const QString& strText)
{
for (int i = 0; i < m_ui->listWidget->count(); ++i)
{
QWidget* matchItem = m_ui->listWidget->itemWidget(m_ui->listWidget->item(i));
QList<QLabel*> labelList = matchItem->findChildren<QLabel*>();
for(QLabel* label: labelList)
{
if (label->objectName() == labelName)
{
if (label->text().contains(strText))
m_ui->listWidget->setRowHidden(i, false); //显示匹配的结果
else
m_ui->listWidget->setRowHidden(i, true); //隐藏不匹配的结果
}
}
}
}
优化后
void MediaListview::search(const QString& strText)
{
for (int i = 0; i < m_ui->listWidget->count(); ++i)
{
MediaItemWidget* matchItem = static_cast<MediaItemWidget*>(m_ui->listWidget->itemWidget(m_ui->listWidget->item(i)));
if (matchItem->getName().contains(strText))
m_ui->listWidget->setRowHidden(i, false); //显示匹配的结果
else
m_ui->listWidget->setRowHidden(i, true); //隐藏不匹配的结
}
}
QString MediaItemWidget::getName()
{
return ui->labelName->text();
}