1

I have a QList<QDate> with dates to events, and I want to highlight those dates on a QCalendarWidget, hopefully, with an image, can be changing the cell color.

I'm probably making a novice mistake in my code...

This code that I modified from (Here) should make the QCalendarWidget paint the dates with a red border, but it doesn't...

mainwindor.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_manager = new CalendarManager(ui->calendarWidget);

    setupConnections();

    on_calendarWidget_clicked(QDate::currentDate());
}

/* GUI button behavior */

calendarmanager.h

#ifndef CALENDARMANAGER_H
#define CALENDARMANAGER_H

#include <QCalendarWidget>

#include <QStringList>
#include <QBrush>
#include <QColor>
#include <QFile>
#include <QList>
#include <QDate>
#include <QPen>

class CalendarManager : public QCalendarWidget
{
    Q_OBJECT

    Q_PROPERTY(QColor color READ getColor WRITE setColor)

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

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

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

private:
    struct calendarEvent
    {
        QDate date;
        QString name;
    };

    QList<calendarEvent> m_events;
    QList<QDate> m_dates;

    QPen m_outlinePen;
    QBrush m_transparentBrush;

    void getDates();
};

#endif // CALENDARMANAGER_H

calendarmanager.cpp

#include <QPainter>

#include "calendarmanager.h"

CalendarManager::CalendarManager(QWidget *parent)
    : QCalendarWidget(parent)
{
    m_outlinePen.setColor(Qt::red);
    m_transparentBrush.setColor(Qt::transparent);

    getDates();
}

CalendarManager::~CalendarManager()
{

}

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

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

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

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

void CalendarManager::getDates()
{
    QFile file("/data/events.csv");
    if(!file.open(QIODevice::ReadOnly)) {
        //Error code
    }

    QList<QByteArray> wordList;

    QDate date;
    QString name;
    calendarEvent e;

    while(!file.atEnd()) {
        QByteArray line = file.readLine();
        wordList = line.split(',');

        date = QDate::fromString( wordList.first(), "dd/MM/yyyy" );
        name = wordList.last();

        e.date = date;
        e.name = name;

        m_events.append(e);
        m_dates.append(date);
    }

    file.close();
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Teivel
  • 162
  • 1
  • 3
  • 12

1 Answers1

1

The problem is that when creating m_manager you are not including this in the GUI, even if you pass the parent ui->calendarWidget.

What you should do is promote your GUI to use that widget, that you can easily from the design view.

  1. Right click on calendarWidget and select Promote to:

enter image description here

  1. Select as Base Class a QCalendarWidget, Promoted Class Name: CalendarManager and Header File: calendarmanager.h

enter image description here

  1. Press Add and Promoted:

enter image description here

Screenshot of output:

enter image description here

Note: it is not necessary to create m_manager, if we look at ui->calendarwidget is an instance of CalendarManager.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • It should work, I mean, I really see it working, but it doesn't... (could the stacked widget mess anything up? Or the file reading? Let me check – Teivel Jul 17 '17 at 02:01
  • Yes, file reading has a problem, that works perfectly, thanks – Teivel Jul 17 '17 at 02:03
  • I recommend you use an `absolute path` instead of `relative path`, or use `QFileDialog: getOpenFileName()` – eyllanesc Jul 17 '17 at 02:04
  • Will look at it, thanks, but hte problem is in how the string is converted to a date, i think, because that's where it seems to fail, and it uses a application file, not something the user should select... for debug that shouldn't be a problem. – Teivel Jul 17 '17 at 02:11
  • My file has the following format: `22/07/2017,"hola"` – eyllanesc Jul 17 '17 at 02:16
  • mine was: 22/07/2017,EVENT – Teivel Jul 17 '17 at 02:25
  • Nice, Greetings From Chile! – Teivel Jul 17 '17 at 02:26
  • Hey all, using this I have a problem (qt 5.13), the drawing is done only when I click on the promoted QCalendarWidget, tried using update() and redraw(), from inside the derived class or from the parent code, but to no avail. – Debianita Apr 13 '22 at 14:56