【Qt】QCombox的下拉框如何向上展开


当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的高
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM