PDA

View Full Version : QCalenderWidget::paintCell



Stanfillirenfro
30th March 2014, 11:24
I am facing a problem regarding the coloration of a specific date on my QWidgetCalender. I
have to subclass QCalenderWidget and to reimplement de function painCell. What I did. And I have called
the function paintCell in my appropriate window, but I have this error message:

Error : no matching function for call to 'MyCalender::paintCell(QPainter*&, <unresolved overloaded function type>, const QDate&)'
Candidates are: virtual void MyCalender::paintCell(QPainter*, const QRect&, const QDate&)

What am I doing wrong?

Many thanks in advance.

Here is my code:

I subclass here QCalenderWidget and I reimplement paintCell:


class MyCalender: public QCalendarWidget
{
public:
MyCalender(QWidget *parent);
~MyCalender();


protected:
virtual void paintCell(QPainter *painter, const QRect &rect, const QDate &date);

};

MyCalender::MyCalender(QWidget *parent): QCalendarWidget(parent)
{

}

void MyCalender::paintCell(QPainter *painter, const QRect &rect, const QDate &date)
{

if(date.dayOfWeek() == Qt::Monday)
{
painter->save();
painter->fillRect(rect, Qt::green);
painter->drawRect(rect);
painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
painter->restore();
}
else
{
QCalendarWidget::paintCell(painter, rect, date);
}

}

My Window:


class MyWindow: public QDialog
{
Q_OBJECT

public:

MyWindow(QWidget *parent = 0);
~MyWindow();

QString getFileName();

public slots:
void colorADate(const QDate &date);

private:
MyCalender *m_calendrier;
};

void MyWindow::colorADate(const QDate &date)
{
QPainter *painter = new QPainter;

m_calendrier->paintCell(painter, rect, date);

}

stampede
30th March 2014, 12:11
void MyWindow::colorADate(const QDate &date)
{
QPainter *painter = new QPainter;

m_calendrier->paintCell(painter, rect, date);

}
'rect' is not defined in the scope of this method, compiler is trying to use method "QRect rect () const" from QWidget class.
Change "rect" to "rect()" and it should compile.
Btw. you have a memory leak (painter).

Stanfillirenfro
30th March 2014, 12:46
Many thanks Stampede for your reply.

The does not bring any change. After changing "rect" to "rect()" in the method colorADate(), i have the following error message:


Error : 'virtual void MyCalender::paintCell(QPainter*, const QRect&, const QDate&)' is protected

within this context (MyWindow):

Error : within this context

Another problem: When you mention that I have memory leak (painter), could you guide me to overcome this problem?

Many thanks in advance.

stampede
30th March 2014, 14:49
The does not bring any change
Yes it does - you have new error messages :)

When you mention that I have memory leak (painter), could you guide me to overcome this problem?
Always release allocated resources, in this case use operator "delete" on "painter". Anyway, what do you want to do with a QPainter not attached to a paint device ?

After changing "rect" to "rect()" in the method colorADate(), i have the following error message:
Because you are trying to access protected member of MyCalendar class, this is basic C++.
What is "MyWindow::colorADate(const QDate &date)" supposed to do ?

Stanfillirenfro
30th March 2014, 18:19
Many thanks Stampede for your reply.


Yes it does - you have new error messages
I was saying that say that the problem was not resolved.


Always release allocated resources, in this case use operator "delete" on "painter
Isn't it true that the destrcutor deletes all pointer of the class? Please correct if I am mistaking.

MyWindow::colorADate(const QDate &date) is supposed to color the selected cell.


Because you are trying to access protected member of MyCalendar class, this is basic C++
Yes, you are wright. But I am calling this method using an instance of the QCalenderWidget subclass. Not doing it here, I dought my cell can be painted just if it implemented in the subclass. Please correct me if I am wrong.

Many thanks in advance

stampede
30th March 2014, 18:43
deletes all pointer of the class?
I don't understand what you mean by "all pointer of the class".
If object is a QObject, then it will delete all it's children automatically. In other cases you have to take care of it manually.

MyWindow::colorADate(const QDate &date) is supposed to color the selected cell.
Maybe you should keep a list or set of coloured dates in MyCalendar object, instead of trying to manually call the rendering method.
Each "colorADate" call could add the date to a set. Then in calendar paintCell() method, you can test if date is in this set, and use custom painting for it.

But I am calling this method using an instance of the QCalenderWidget subclass
Yes, but you are doing it outside of the class' scope. Read here for more info: public-vs-private-access-specifiers (http://www.learncpp.com/cpp-tutorial/83-public-vs-private-access-specifiers/) , inheritance-and-access-specifiers (http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/)

Stanfillirenfro
30th March 2014, 19:01
Many thanks Tampede for your reply.


If object is a QObject, then it will delete all it's children automatically. In other cases you have to take care of it manually.
I can not call th eoperator delete on the pointer "pianter" in this cas. This operator does not exist when I am trying to call it in this case.


Maybe you should keep a list or set of coloured dates in MyCalendar object, instead of trying to manually call the rendering method.
Each "colorADate" call could add the date to a set. Then in calendar paintCell() method, you can test if date is in this set, and use custom painting for it.
Your solution is a seriuous path I am exploring.

I also have the following code:


QTextCharFormat greenColor;
couleurVerte.setBackground(Qt::green);

m_calendrier->setDateTextFormat(date, greenColor);

I can have the appropriate color on the cell selected, but the problem is that this color on the cell is not save. I mean, if I open the window next time, the color does not appear on the cell.

stampede
30th March 2014, 19:18
I can not call th eoperator delete on the pointer "pianter" in this cas
Of course you can:


QPainter * painter = new QPainter(...);
...
delete painter;


I mean, if I open the window next time, the color does not appear on the cell.
What do you mean by "next time" ? Next time the application is started ? Next time the m_calendrier instance is created ?
You are setting the format for m_calendrier instance, so only this object will have that information. If you want to share format data between objects then keep it outside of the object's scope. If you want to have it next time you start the app, use QSettings.

Stanfillirenfro
30th March 2014, 19:28
Thanks!


What do you mean by "next time" ? Next time the application is started ? Next time the m_calendrier instance is created ?
Next time m_calendrier isr created.


You are setting the format for m_calendrier instance, so only this object will have that information.
Of course, I want only this instance to have the information. I mean, just only m_calendrier. I want the information to be saved only for it.

stampede
30th March 2014, 19:40
Next time m_calendrier isr created.

I want only this instance to have the information
Next time m_calendrier is created, it'll be different instance, a different object. Sounds to me like you want to share this information between all instances of MyCalendar class. Again, if you want to have formatting info next time you launch the application, store it using QSettings. If you want to have that info only next time the calendar is shown, keep it outside of the object scope, in your main window for example, and assign this info to MyCalendar after you create it.

Stanfillirenfro
30th March 2014, 19:55
Thanks Stampede.

I start wright now modifying the code using QSettings. I will present you my nes code in coming minutes.