[Qt-interest] Derived custom widgets

Neville Dastur qt at dastur.me.uk
Sun Aug 29 22:04:43 CEST 2010


I am trying to implement my own date edit control. This is to implement 
correct handling to null dates. I know the issue of null dates has been 
discussed lots and for a library with such tight database integration, I 
don't know why this isn't handled elegantly.

Any how, my idea was to subclass QDateEdit to create a custom widget. I 
want an extra button to the side of the combo control to clear the date 
and be able to set the lineedit of the combo or spinbox to something 
like "No Date"

The bit I am stuck on is the painting. It seems to me there are a number 
of options.

1) Derive from QWidget instead, and create a composite widget of 
QDateEdit and QToolButton. But then I have to re-implement all the 
QDateEdit members and I don't retain designer integration.

2) Do above, but write as a designer plugin, to get over last problem.

3) Derive from QDateEdit. Add some width to my sizeHint and call the 
base class to draw the date control and then paint on the extra button.

 From an object orientated POW it seems 3 should be the way forward.

I wrote this to test:

/*virtual (note without virtual makes no difference)*/
void wdgDateEdit::paintEvent(QPaintEvent *event) {
     QDateEdit::paintEvent(event);
     QPainter p(this);
     p.drawEllipse( event->rect() );
}

/*virtual (note without virtual makes no difference)*/
QSize wdgDateEdit::sizeHint() const {
     QSize sz = QDateEdit::sizeHint();
     sz.setWidth( sz.width() + 20 );
     DEBUG << "DateEdit size hint " << QDateEdit::sizeHint() << "  We 
return size " << sz;
     return sz;
}

I expected the QDateEdit to get it's size hint from the base class size 
hint, but it doesn't. As the ellipse fills the entire 
wdgDateEdit::sizeHint() rectangle.

So I thought I could modify the rect that QDateEdit::paintEvent(event); 
is acting on. But can't find a method to do so.

I know some people may say get on and implement 1 or 2 above. But it is 
not as elegant. No OO and therefore not as easy to maintain. I also 
think that what I am trying to achieve should be possible with Qt.

Any pointers would be appreciated.

Neville



More information about the Qt-interest-old mailing list