Results 1 to 11 of 11

Thread: QCalenderWidget::paintCell

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default QCalenderWidget::paintCell

    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:aintCell(QPainter*&, <unresolved overloaded function type>, const QDate&)'
    Candidates are: virtual void MyCalender:aintCell(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:

    Qt Code:
    1. class MyCalender: public QCalendarWidget
    2. {
    3. public:
    4. MyCalender(QWidget *parent);
    5. ~MyCalender();
    6.  
    7.  
    8. protected:
    9. virtual void paintCell(QPainter *painter, const QRect &rect, const QDate &date);
    10.  
    11. };
    12.  
    13. MyCalender::MyCalender(QWidget *parent): QCalendarWidget(parent)
    14. {
    15.  
    16. }
    17.  
    18. void MyCalender::paintCell(QPainter *painter, const QRect &rect, const QDate &date)
    19. {
    20.  
    21. if(date.dayOfWeek() == Qt::Monday)
    22. {
    23. painter->save();
    24. painter->fillRect(rect, Qt::green);
    25. painter->drawRect(rect);
    26. painter->drawText(rect, Qt::AlignCenter, QString::number(date.day()));
    27. painter->restore();
    28. }
    29. else
    30. {
    31. QCalendarWidget::paintCell(painter, rect, date);
    32. }
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 

    My Window:

    Qt Code:
    1. class MyWindow: public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6.  
    7. MyWindow(QWidget *parent = 0);
    8. ~MyWindow();
    9.  
    10. QString getFileName();
    11.  
    12. public slots:
    13. void colorADate(const QDate &date);
    14.  
    15. private:
    16. MyCalender *m_calendrier;
    17. };
    18.  
    19. void MyWindow::colorADate(const QDate &date)
    20. {
    21. QPainter *painter = new QPainter;
    22.  
    23. m_calendrier->paintCell(painter, rect, date);
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QCalenderWidget::paintCell

    Qt Code:
    1. void MyWindow::colorADate(const QDate &date)
    2. {
    3. QPainter *painter = new QPainter;
    4.  
    5. m_calendrier->paintCell(painter, rect, date);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    '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).

  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCalenderWidget::paintCell

    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:aintCell(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.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QCalenderWidget::paintCell

    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 ?

  5. #5
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCalenderWidget::paintCell

    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
    Last edited by Stanfillirenfro; 30th March 2014 at 17:29.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QCalenderWidget::paintCell

    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 , inheritance-and-access-specifiers

  7. #7
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCalenderWidget::paintCell

    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.

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QCalenderWidget::paintCell

    I can not call th eoperator delete on the pointer "pianter" in this cas
    Of course you can:
    Qt Code:
    1. QPainter * painter = new QPainter(...);
    2. ...
    3. delete painter;
    To copy to clipboard, switch view to plain text mode 
    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.

  9. #9
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCalenderWidget::paintCell

    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.

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QCalenderWidget::paintCell

    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.

  11. #11
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCalenderWidget::paintCell

    Thanks Stampede.

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

Similar Threads

  1. QdataWidgetmapper + Qcalenderwidget
    By Delphi in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2013, 15:54
  2. QCalenderWidget mark dates RED
    By ad3d in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2011, 10:59
  3. QCalenderWidget doesn't show the month name and Day name.
    By jthacker in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2010, 20:43
  4. QTable - paintCell() - how to do with it?
    By mp33919 in forum Qt Programming
    Replies: 8
    Last Post: 23rd April 2009, 19:55
  5. QTable>>>paintCell
    By :db:sStrong in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2006, 10:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.