當combox位置太靠下時,下拉框會超出邊框,很丑,平時擺位置肯定不會擺這么靠下,但是假如把combox嵌入到tableview里時,就很容易出現這種情況了。
如下圖
向上展開的修改方法:
重載 showPopup()函數,選擇Popup時的位置
//.h
class myCombox : public QComboBox
{
Q_OBJECT
public:
myCombox(QWidget *parent = nullptr);
protected:
void showPopup() override;
};
//.cpp
myCombox::myCombox(QWidget *parent)
: QComboBox(parent)
{
}
void myCombox::showPopup()
{
QComboBox::showPopup();
QWidget *popup = this->findChild<QFrame*>();
popup->move(popup->x(),popup->y()-this->height()-popup->height());//x軸不變,y軸向上移動 list的高+combox的高
}
此時我們已經完全可以決定什么時候下拉框往上展開了,現在完全可以寫個if--else來決定
void myCombox::showPopup()
{
if(這個條件){
QComboBox::showPopup();
return;
}
//不滿足才向上展開
QComboBox::showPopup();
QWidget *popup = this->findChild<QFrame*>();
popup->move(popup->x(),popup->y()-this->height()-popup->height());//x軸不變,y軸向上移動 list的高+combox的高
}