今天查了好多资料,发现做出来的都不理想。去查了下Qt源码。发现setView(QAbstractItemView *itemView)这个方法,官方的解释是这样的:
void QComboBox::setView(QAbstractItemView *itemView)
Sets the view to be used in the combobox popup to the given itemView. The combobox takes ownership of the view.
Note: If you want to use the convenience views (like QListWidget, QTableWidget or QTreeWidget), make sure to call setModel() on the combobox with the convenience widgets model before calling this function.
See also view().
大概说的意思是:设置了view以后,就可以获得下拉框的所有权。
根据这个思路做如下测试,发现可行:
1.设置view:
cbb_unit = new QComboBox(this);
cbb_unit->setObjectName("UnitLineEditCbb");
QAbstractItemView *pListView = new QListView(this);
pListView->setObjectName("UnitLineEditListView");
cbb_unit->setView(pListView);
2.设置QSS并加载:
QListView:item{background-color:rgba(68,138,202,50%);color:#eeeeee;font-family:"Microsoft YaHei";font-size:12px;border:none;}
QListView:item:hover{background-color:#448aca;color:#eeeeee;border:0px;}
QListView:item:disabled{background-color:rgba(104,121,151,90%);color:#a0a0a0;border:0px solid #aaaaaa;}
成功设置了QComboBox的下拉框。如有错误,欢迎留言交流