LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Qt - QCalendar paint individual cell (https://www.linuxquestions.org/questions/programming-9/qt-qcalendar-paint-individual-cell-868835/)

Gavin Harper 03-16-2011 12:28 AM

Qt - QCalendar paint individual cell
 
Hello!

I am trying to do something that I initially thought was fairly trivial but has since kept me awake all night trying to figure out...

I have a QCalendarWidget that I need to change the cell colours of certain days corresponding with certain data from the database I am using.

I have heard a way to do this is with subclassing QCalenderWidget and implementing the paintCell() function though I have found no examples detailing this.

If anyone can explain to me the appropriate way of handling this issue, I would greatly appreciate it!

Thank you

-Gavin

paulsm4 03-16-2011 01:33 AM

Hi -

Google "QCalendarWidget paintcell" =>

Highlight today's date in a QCalendarWidget

newcalendarwidget.h
Code:

    #ifndef NEWCALENDARWIDGET_H
    #define NEWCALENDARWIDGET_H

    #include <qcalendarwidget>
    #include <qcolor>
    #include <qdate>
    #include <qpen>
    #include <qbrush>

    class NewCalendarWidget : public QCalendarWidget
    {
    Q_OBJECT

    Q_PROPERTY(QColor color READ getColor WRITE setColor)

    public:
      NewCalendarWidget(QWidget *parent = 0);
      ~NewCalendarWidget();

      void setColor(QColor& color);
      QColor getColor();

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

    private:
      QDate m_currentDate;
      QPen m_outlinePen;
      QBrush m_transparentBrush;
    };

    #endif // NEWCALENDARWIDGET_H

newcalendarwidget.cpp:
Code:

    #include <qpainter>
    #include "newcalendarwidget.h"

    NewCalendarWidget::NewCalendarWidget(QWidget *parent)
      : QCalendarWidget(parent)
    {
      m_currentDate = QDate::currentDate();
      m_outlinePen.setColor(Qt::red);
      m_transparentBrush.setColor(Qt::transparent);
    }

    NewCalendarWidget::~NewCalendarWidget()
    {

    }

    void NewCalendarWidget::setColor(QColor& color)
    {
      m_outlinePen.setColor(color);
    }

    QColor NewCalendarWidget::getColor()
    {
      return (m_outlinePen.color());
    }

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

      if (date == m_currentDate)
      {
          painter->setPen(m_outlinePen);
          painter->setBrush(m_transparentBrush);
          painter->drawRect(rect.adjusted(0, 0, -1, -1));
      }
    }


Gavin Harper 03-16-2011 04:55 AM

Thank you!


All times are GMT -5. The time now is 02:44 PM.