【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的高
}
posted @ 2020-09-22 21:47  miyanyan  阅读(3047)  评论(1编辑  收藏  举报