LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2011, 01:28 AM   #1
Gavin Harper
Member
 
Registered: Feb 2010
Location: Finland
Distribution: Slackware
Posts: 169

Rep: Reputation: 24
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
 
Old 03-16-2011, 02:33 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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));
       }
    }
 
Old 03-16-2011, 05:55 AM   #3
Gavin Harper
Member
 
Registered: Feb 2010
Location: Finland
Distribution: Slackware
Posts: 169

Original Poster
Rep: Reputation: 24
Thank you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Paint.NET for Linux: Paint.Mono LXer Syndicated Linux News 0 09-28-2009 10:10 PM
paint-mono (Paint.net for Mono) Installer? caa1980 Linux - Software 0 10-03-2007 11:52 AM
Paint programs General Linux - Software 3 11-19-2005 10:32 PM
Tux-paint Mahony Linux - Software 4 04-05-2004 11:14 AM
Mandrake 9.2, a paint in ... kapa Linux - Distributions 1 01-08-2004 07:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration