数据结构可以参考这个 https://blog.csdn.net/u012377293/article/details/108562076
class MyComboBox :public QComboBox
{
public:
MyComboBox(QWidget *pParent = nullptr);
~MyComboBox();
protected:
void showPopup(); //重写下拉显窗体,设置显示项的大小
private:
void resizeContent();
int MaxContentSize();
int getMaxContentSize(TreeViewItem *pRootItem, ComboxTreeModel *pComboxTreeModel);
int getRootNameLength(ComboxTreeModel *pComboxTreeModel, TreeViewItem *pRoot);
void getChildNameLength(int &nTextLeng, TreeViewItem *pRoot, QModelIndex &childIndex, ComboxTreeModel *pComboxTreeModel);
};
MyComboBox::MyComboBox(QWidget *pParent /*= nullptr*/) :GlodonComboBox(pParent)
{
}
MyComboBox::~MyComboBox()
{
}
void MyComboBox::showPopup()
{
resizeContent();
QComboBox::showPopup();
}
void MyComboBox::resizeContent()
{
//控制下拉窗体的大小
int nFitWidth(0);
nFitWidth += 100;
nFitWidth += MaxContentSize();
if (QWidget *popup = this->view()->window())
{
popup->setMinimumWidth(nFitWidth);
popup->setMaximumWidth(1000);
}
}
int MyComboBox::MaxContentSize()
{
int nMaxTextLeng(285);
QAbstractItemModel *model = this->view()->model();
if (model)
{
ComboxTreeModel *pComboxTreeModel = dynamic_cast<ComboxTreeModel *>(model);
if (pComboxTreeModel)
{
if (TreeViewItem *pRootItem = pComboxTreeModel->getRootItem())
{
nMaxTextLeng = getMaxContentSize(pRootItem, pComboxTreeModel);
}
}
}
return nMaxTextLeng;
}
int MyComboBox::getMaxContentSize(TreeViewItem *pRootItem, ComboxTreeModel *pComboxTreeModel)
{
int nTextLength = getRootNameLength(pComboxTreeModel, pRootItem);
return nTextLength;
}
int MyComboBox::getRootNameLength(ComboxTreeModel *pComboxTreeModel, TreeViewItem *pRoot)
{
int nTextLeng(285);
if (pRoot == nullptr)
{
return nTextLeng;
}
QModelIndex resultIndex = QModelIndex();
bool bFind = false;
for (int i = 0; i < pRoot->childCount(); i++)
{
const TreeViewItem* pChildItem = pRoot->child(i);
assert(NULL != pChildItem);
QModelIndex childIndex = pComboxTreeModel->index(i, 0);
TreeViewItem* pItme = pComboxTreeModel->itemFromIndex(childIndex);
if (this->view() && pItme && pItme->mapInfo())
{
QFontMetrics fmOther = this->fontMetrics();
int nTreeTextLength = fmOther.width(pItme->mapInfo()->m_strShowName);
if (nTreeTextLength > nTextLeng)
{
nTextLeng = nTreeTextLength;
}
}
if (pItme->childCount() > 0)
{
getChildNameLength(nTextLeng, pItme, childIndex, pComboxTreeModel);
}
}
return nTextLeng;
}
void MyComboBox::getChildNameLength(int &nTextLeng, TreeViewItem *pRoot, QModelIndex &childIndex, ComboxTreeModel *pComboxTreeModel)
{
if (pRoot == nullptr)
{
return ;
}
QModelIndex resultIndex = QModelIndex();
for (int i = 0; i < pRoot->childCount(); i++)
{
QModelIndex oIndex = pComboxTreeModel->index(i, 0, childIndex);
TreeViewItem* pItme = pComboxTreeModel->itemFromIndex(oIndex);
if (this->view() && pItme && pItme->mapInfo())
{
QFontMetrics fmOther = this->fontMetrics();
int nTreeTextLength = fmOther.width(pItme->mapInfo()->m_strShowName);
if (nTreeTextLength > nTextLeng)
{
nTextLeng = nTreeTextLength;
}
}
if (pItme->childCount() > 0) //有多层级时递归寻找
{
getChildNameLength(nTextLeng, pItme, oIndex, pComboxTreeModel);
}
}
return ;
}