LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12: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, 01: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, 04:55 AM   #3
Gavin Harper
Member
 
Registered: Feb 2010
Location: Finland
Distribution: Slackware
Posts: 169

Original Poster
Rep: Reputation: 24
Thank you!
 
  


Reply



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 09:10 PM
paint-mono (Paint.net for Mono) Installer? caa1980 Linux - Software 0 10-03-2007 10:52 AM
Paint programs General Linux - Software 3 11-19-2005 09:32 PM
Tux-paint Mahony Linux - Software 4 04-05-2004 10:14 AM
Mandrake 9.2, a paint in ... kapa Linux - Distributions 1 01-08-2004 06:41 PM

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

All times are GMT -5. The time now is 06:26 AM.

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
Open Source Consulting | Domain Registration