Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, July 2007
paintEvent QCalendarWidget


Message 1 in thread

Hello,

I have a problem with a painter, which is only allowed to paint in a
paintEvent. (I am using Qt4.2.3-0ubuntu3)

A lot of people seem to hava a problem with this, I read all (as far as i
know) the messages about paintEvents but still have a problem.

My class: Planner extends QCalendarWidget.
I want to paint in a Cell. (colour it)

This is my code:

void Planner::paintEvent(QPaintEvent *event){
cout<<"in paintevent"<<endl;
QPainter *painter = new QPainter(this);
painter->setPen(Qt::red);
for(int i=0; i<datesToPaint.size();i++){
        paintCell(painter, event->rect(),datesToPaint->at(i));
        }
}

According to the documentation (how i interpreted it) a paintEvent is send
when calling repaint.
But when I execute this->repaint() in another method nothing happens.

When I call: paintEvent(new QPaintEvent(QRect(0,0,300,300))); instead, it
does execute the paintEvent (the message printer by cout is shown) but
Qt still gives te warning that I may only paint in a paintEvent...(I don't
know why becose it is executing in paintEvent)


So...What am I doing wrong?
It seems that repaint() is uses the paintEvent function of QWidget instead
of mine or something like that.

Thanks



--
 [ signature omitted ] 

Message 2 in thread

Hi,

> It seems that repaint() is uses the paintEvent function of QWidget instead
> of mine or something like that.

If you just want to modify the way a cell is painted, why not override 
QCalendarWidget::paintCell()?

--
 [ signature omitted ] 

Message 3 in thread

Dimitri wrote:

> Hi,
> 
>> It seems that repaint() is uses the paintEvent function of QWidget
>> instead of mine or something like that.
> 
> If you just want to modify the way a cell is painted, why not override
> QCalendarWidget::paintCell()?
> 
> --
> Dimitri

I have tried overriding paintCell(). Without any result.
On Koder.com i found Qt's implementation of paintCell. 
http://www.koders.com/cpp/fid232194EFFAE3927DEF4471886B6F68BD4DF3193D.aspx#L818
That implementation is just fine I think. I just want to colour a cell.

I also made a testClass to check if I was doing something wrong with the
paintEvent. I made a MyQButton::QPushButton Class, override painEvent() and
everything worked.

So why doesn't it work with a QCalendarWidget?
Can it have something to do with Qt's Models and QItemDelegate (which are
used by QCalendarWidget).

This is my code:

class Planner : public QCalendarWidget
 {
     Q_OBJECT
        
        public:
        Planner(QWidget *parent=0);
        protected:
        void paintEvent(QPaintEvent *event);
        
};


Planner::Planner(QWidget *parent)
     : QCalendarWidget(parent)
{
}

void Planner::paintEvent(QPaintEvent *event){
cout<<"in paintevent"<<endl;
QPainter *painter = new QPainter(this);
QBrush brush = QBrush(Qt::SolidPattern);
brush.setColor(Qt::red);
painter->setBrush(brush);
painter->fillRect(event->rect(),brush);
paintCell(painter,event->rect(),QDate(QDate::currentDate().addDays(1)));

//QCalendarWidget::paintEvent(event);
}





When the widget is created, paintEvent is executed once. (output from cout
is printed)
When resizing the widget my paintEvent method is NOT executed.

when forcing execution of the method paintEvent or when forcing a paintEvent
Qt says it can only draw in paintEvent (although the painter is being used
in the paintEvent)

Any help is welcome because I really don't see why it isn't working.

Thanks

--
 [ signature omitted ] 

Message 4 in thread

Hi,

> I have tried overriding paintCell(). Without any result.

How did you try overriding it?

> On Koder.com i found Qt's implementation of paintCell. 
> http://www.koders.com/cpp/fid232194EFFAE3927DEF4471886B6F68BD4DF3193D.aspx#L818
> That implementation is just fine I think. I just want to colour a cell.

You need to override paintCell() accordingly.

> I also made a testClass to check if I was doing something wrong with the
> paintEvent. I made a MyQButton::QPushButton Class, override painEvent() and
> everything worked.
> 
> So why doesn't it work with a QCalendarWidget?
> Can it have something to do with Qt's Models and QItemDelegate (which are
> used by QCalendarWidget).

The QPushButton is s simple widget, it probably overrides Widgets empty 
QWidget::paintEvent() function.

The QCalendarWidget is a complex widget. It contains other widgets which are 
responsible for painting themselves, such as QCalendarView which is a 
QTableView and uses the model/view paradigm. The Qt framework optimizes away 
QCalendarWidget::repaint() since QCalendarWidget is not visible - only the 
QCalendarView within it is.

--
 [ signature omitted ] 

Message 5 in thread

Hello,

The problem seems to be resolved. I havn't implemented everything I want to
do with the QCalendarWidget but now I can paint the Cells.

Last time I override paintCell this way:
void Planner::paintCell(QPainter * painter, const QRect & rect, const QDate
& date)
It didn't work.....

Now I override it like this:
void Planner::paintVell(QPainter * painter, const QRect & rect, const QDate
& date) const

And now it works.

I didn't need the paintEvent idd...I only needed to override the paintEvent.

Thanks for the help.
Bye


Dimitri wrote:

> Hi,
> 
>> I have tried overriding paintCell(). Without any result.
> 
> How did you try overriding it?
> 
>> On Koder.com i found Qt's implementation of paintCell.
>>
http://www.koders.com/cpp/fid232194EFFAE3927DEF4471886B6F68BD4DF3193D.aspx#L818
>> That implementation is just fine I think. I just want to colour a cell.
> 
> You need to override paintCell() accordingly.
> 
>> I also made a testClass to check if I was doing something wrong with the
>> paintEvent. I made a MyQButton::QPushButton Class, override painEvent()
>> and everything worked.
>> 
>> So why doesn't it work with a QCalendarWidget?
>> Can it have something to do with Qt's Models and QItemDelegate (which are
>> used by QCalendarWidget).
> 
> The QPushButton is s simple widget, it probably overrides Widgets empty
> QWidget::paintEvent() function.
> 
> The QCalendarWidget is a complex widget. It contains other widgets which
> are responsible for painting themselves, such as QCalendarView which is a
> QTableView and uses the model/view paradigm. The Qt framework optimizes
> away QCalendarWidget::repaint() since QCalendarWidget is not visible -
> only the QCalendarView within it is.
> 
> --
> Dimitri

--
 [ signature omitted ]