简体   繁体   English

QDateTimeEdit选择每月的最后一天

[英]QDateTimeEdit select last day of each month

I have a QDateTimeEdit and user should select a date with it. 我有一个QDateTimeEdit,用户应该选择一个日期。 However, I need to select the last day of each month. 但是,我需要选择每个月的最后一天。 So, for example if user select 3rd of March, I should set date to 31th of March. 因此,例如,如果用户选择3月3日,则应将日期设置为3月31日。

I try to do this in the slot of dateChanged(const QDate&) signal. 我尝试在dateChanged(const QDate&)信号的插槽中执行此操作。 But when I call setDate() function, it causes the slot to be called once more. 但是,当我调用setDate()函数时,它将导致再次调用该插槽。

Here is the sample code 这是示例代码

connect(m_pDateEdit, SIGNAL(dateChanged(const QDate&)), this, SLOT(OnDateChanged(const QDate&)));

void MyClass::OnDateChanged(const QDate& date)
{
    const bool b = m_pDateEdit->blockSignals(true);

    // THIS LINE CAUSES TO THIS SLOT TO BE CALLED TWICE
    m_pDateEdit->setDate(QDate(date.year(), date.month(), date.daysInMonth()));
    CallSomeFunction();

    m_pDateEdit->blockSignals(b)
}

Anything I'm missing? 我有什么想念的吗? Any Ideas? 有任何想法吗?

Thank you for your time! 感谢您的时间!

EDIT: since you can't just do a disconnect I would advise you make a checker instead and remove the connect. 编辑:由于您不能只是断开连接,所以我建议您改用检查器并删除连接。 You can do that : 您可以这样做:

In the constructor: 在构造函数中:

QTimer::singleShot(30, this, SLOT(checkDateChanged()));

Then in the class: 然后在课堂上:

void MyClass::checkDateChanged()
{
    if (pDateEdit->day() != pDateEdit->daysInMonth())
    {
        m_pDateEdit->setDate(QDate(date.year(), date.month(), date.daysInMonth()));
    }
    CallSomeFunction();
    QTimer::singleShot(30, this, SLOT(checkDateChanged())); // this will create a loop called every 30 ms.

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM