QDateTimeEdit clear text



  • In my application i show some times with the QDateTimeEdit widget on the screen.

    I want to clear the text of the QDateTimeEdit widget with the clear() function of the class.

    When i call the clear() function, only the current section will be cleared, not the whole text/time. So i tried to change the currentSection property, but without success.

    In the Qt Designer i'm not able to change the property, the property always switches back to HourSection.
    Change the currentSection property directly in the constructor also has no effect.

    I use the widget with followed properties:

    frame = false
    readOnly = true
    buttonSymbols = QAbstractSpinBox::NoButtons
    displayFormat = "HH:mm"
    

  • Qt Champions 2016

    Hi
    Do you mean so the area is completely blank
    or that value reset to 00:00 ?



  • @mrjj
    when i call the clear() function the HourSection is completely blank and the SecondSection is reset to 00.

    For me it would be nice to have the whole area to be completely blanked.


  • Qt Champions 2016

    @beecksche
    Ok, so its sort of both. :)
    Just tried here on Qt 5.6 and nothing happened at all with clear() :?!



  • @mrjj
    I tried it also with Qt5.6 MSVC2013:

    #include <QtWidgets/QApplication>
    #include <QHBoxLayout>
    #include <QDateTimeEdit>
    #include <QToolButton>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	
    	QWidget central;
    
    	QDateTimeEdit *edit = new QDateTimeEdit(&central);	
    	edit->setReadOnly(true); 
    	edit->setButtonSymbols(QAbstractSpinBox::NoButtons);
    	edit->setFrame(false);
    	edit->setDisplayFormat("HH:mm");
    
    	QToolButton *btn = new QToolButton(&central);
    	btn->setText("Clear()");
    
    	QHBoxLayout *layout = new QHBoxLayout(&central);
    	layout->addWidget(edit);
    	layout->addWidget(btn);
    	
    	QObject::connect(btn, &QToolButton::clicked, edit, &QDateTimeEdit::clear);
    
    	central.show();
    
    	return a.exec();
    }
    

    When i click the button only the selected section will be cleared/blanked. Once i clicked in the QDateTimeEdit widget the number is shown again. Either i missunderstood the doc or there is a bug in QDateTimeEdit class.

    I changed the way to display that no time is available with specialValueText property. So i do not have to call the clear() function at all. The text will be shown if the current value is equal to the set minimum value:

    #include <QtWidgets/QApplication>
    #include <QHBoxLayout>
    #include <QDateTimeEdit>
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	
    	QWidget central;
    
    	QDateTimeEdit *edit = new QDateTimeEdit(&central);	
    	edit->setReadOnly(true); 
    	edit->setButtonSymbols(QAbstractSpinBox::NoButtons);
    	edit->setFrame(false);
    	edit->setDisplayFormat("HH:mm");
    	edit->setSpecialValueText("No time available"); // will be shown when the current time is equal to the minimum
    
    	QHBoxLayout *layout = new QHBoxLayout(&central);
    	layout->addWidget(edit);
    
    	central.show();
    
    	return a.exec();
    }
    

  • Qt Champions 2016

    Good workaround.
    Maybe clear was never really implemented in QDateTime as
    it comes from the base class.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.