1

I have a QDateTimeEdit and user should select a date with it. 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.

I try to do this in the slot of dateChanged(const QDate&) signal. But when I call setDate() function, it causes the slot to be called once more.

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!

nabroyan
  • 3,225
  • 6
  • 37
  • 56

1 Answers1

1

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.

}
  • Actually I have tried that as well, but somehow slot is being called again. – nabroyan Mar 02 '17 at 16:41
  • ok I will update it with a better answer. (it would be easier to test if you give me the code but else that's ok too) – Gabrielle de Grimouard Mar 02 '17 at 16:44
  • Actually you gave me an idea. I disconnect my signal-slot, then call `setDate()`, afterwards call `singleShot()`, which connects back my signal-slot. – nabroyan Mar 03 '17 at 06:51
  • This works, but I think this is not a delicate solution and it would be nice to solve this more straightforward way. – nabroyan Mar 03 '17 at 06:52
  • A better way would be to implement your own setDate in your own DateEdit with your code. But as you seems you didn't want to do that I choose to send you another solution easier to do. – Gabrielle de Grimouard Mar 03 '17 at 09:49