56 lines
1.2 KiB
C++
56 lines
1.2 KiB
C++
#include "AdaptionComboBox.h"
|
|
#include <QEvent>
|
|
#include <QStandardItemModel>
|
|
|
|
AdaptionComboBox::AdaptionComboBox(QWidget* parent)
|
|
: QComboBox(parent)
|
|
, m_pListWidget(new QListView)
|
|
{
|
|
this->setView(m_pListWidget);
|
|
m_pListWidget->installEventFilter(this);
|
|
}
|
|
AdaptionComboBox::~AdaptionComboBox()
|
|
{
|
|
|
|
}
|
|
|
|
void AdaptionComboBox::resizeEvent(QResizeEvent *pEvent)
|
|
{
|
|
QComboBox::resizeEvent(pEvent);
|
|
UpdateItemSize();
|
|
}
|
|
|
|
bool AdaptionComboBox::eventFilter(QObject* pWatched, QEvent* pEvent)
|
|
{
|
|
if (pWatched == m_pListWidget && pEvent->type() == QEvent::Show)
|
|
{
|
|
UpdateItemSize();
|
|
}
|
|
return false;
|
|
}
|
|
void AdaptionComboBox::UpdateItemSize()
|
|
{
|
|
QStandardItemModel* pStandardItemModel =dynamic_cast<QStandardItemModel*>(m_pListWidget->model());
|
|
if (pStandardItemModel == NULL)
|
|
return;
|
|
|
|
int nWidth = 0;
|
|
|
|
for (int nRow = 0; nRow < pStandardItemModel->rowCount(); ++nRow)
|
|
{
|
|
QModelIndex rModelIndex = pStandardItemModel->index(nRow, 0);
|
|
QString szText = pStandardItemModel->data(rModelIndex).toString();
|
|
QFontMetrics rFontMetrics(m_pListWidget->font());
|
|
int nTemp = rFontMetrics.width(szText);
|
|
if (nTemp > nWidth)
|
|
{
|
|
nWidth = nTemp;
|
|
}
|
|
}
|
|
nWidth += 25;
|
|
if (nWidth < this->width())
|
|
{
|
|
nWidth = this->width();
|
|
}
|
|
m_pListWidget->setFixedWidth(nWidth);
|
|
} |