QDateEdit的弹出日历小部件在文本区域的任何地方,而不仅仅是向下箭头



我在我的qdateedit'sdateEdit上使用了事件过滤器,如下所示:

bool Class::eventFilter ( QObject *obj, QEvent *event )
{
    if(event->type() == QEvent::MouseButtonPress)
    {      
        sdateEdit->calendarWidget()->show();
    }
    else
        return QObject::eventFilter ( obj, event );
}

这不起作用。我尝试了.. sdateEdit-> setCalendarPopup(true)。这也无法正常工作。

在这种情况下,我已经实现了自定义qdateEdit,策略是在单击qlineedit并将单击事件发送到箭头时使用EventFilter:

#include <QtWidgets>
class DateEdit: public QDateEdit
{
public:
    DateEdit(QWidget *parent=nullptr):
        QDateEdit(parent)
    {
       lineEdit()->installEventFilter(this);
    }
    bool eventFilter(QObject *watched, QEvent *event) override
    {
        if(watched == lineEdit() && event->type() == QEvent::MouseButtonPress){
            QStyleOptionComboBox opt;
            opt.init(this);
            QRect r = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxArrow, this);
            QPoint p = r.center();
            QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress, p, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
            QCoreApplication::sendEvent(this, event);
        }
        return QDateEdit::eventFilter(watched, event);
    }
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    DateEdit w;
    w.setCalendarPopup(true);
    w.show();
    return a.exec();
}

相关内容

最新更新