PDA

View Full Version : Problem Subclassing Calendar



Archa4
8th March 2011, 09:11
I need to create a custom calendar, where i need to change the look of each cell that contains a date. I tried this:

class CustomCalendarWidget : public QCalendarWidget
{
Q_OBJECT
public:
CustomCalendarWidget( QWidget* parent = 0 ) : QCalendarWidget( parent ) {}
~CustomCalendarWidget() {}

protected:
virtual void paintCell( QPainter* painter, const QRect& rect, const QDate& date ) const
{
painter->setPen( Qt::black );
painter->drawText( rect, Qt::AlignCenter, tr("Qt by Nokia") ); //for example
}
};

But i get errors:

..\Calendar_forum_v2\/customcalendarwidget.h: In member function 'virtual void CustomCalendarWidget::paintCell(QPainter*, const QRect&, const QDate&) const':

..\Calendar_forum_v2\/customcalendarwidget.h:17: error: invalid use of incomplete type 'struct QPainter'

..\..\..\NokiaQtSDK\Simulator\Qt\mingw\include/QtGui/qwindowdefs.h:68: error: forward declaration of 'struct QPainter'

..\Calendar_forum_v2\/customcalendarwidget.h:18: error: invalid use of incomplete type 'struct QPainter'

..\..\..\NokiaQtSDK\Simulator\Qt\mingw\include/QtGui/qwindowdefs.h:68: error: forward declaration of 'struct QPainter'

Could someone please explain to me - what am I doing wrong?

wysota
8th March 2011, 09:25
You forgot to #include <QPainter>.

Archa4
8th March 2011, 09:43
Thanks Very much! (such an idiotic mystake by me)

Hm now i have a different kind of problem: In original all the dates from previous and next month were not black colour but black. Now all the dates are black. How should i alter the void PaintCell to make the dates look like original? Can I look anywhere how the void PaintCell was working in original? That way i could alter mine realisation to make the dates from previous and next month gray...

Thanks!

wysota
8th March 2011, 12:38
You could look into the source code for the method.

Archa4
8th March 2011, 13:19
Em.. I don't want to sound retarded, but where can i look in source code?

boudie
8th March 2011, 19:39
Try this location:
<your_qt_dir>/src/gui/widgets/qcalendarwidget.cpp

Archa4
9th March 2011, 08:19
Hm... I can only find QCalendarWidget.h files, but there are no QCalendarWidget.cpp anywhere...

Well, with trials and errors i wrote this thing, that seems to be working:

if (this->monthShown()!=date.month())
painter->setPen(Qt::gray);
else
if (date.dayOfWeek()==6 || date.dayOfWeek()==7)
painter->setPen(Qt::red);
else
painter->setPen(Qt::black);

wysota
9th March 2011, 12:50
Em.. I don't want to sound retarded, but where can i look in source code?

Download Qt's sources and look there.