问题
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();
}
回答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.
- Right click on calendarWidget and select
Promote to
:
- Select as Base Class a
QCalendarWidget
, Promoted Class Name:CalendarManager
and Header File:calendarmanager.h
- Press Add and Promoted:
Screenshot of output:
Note: it is not necessary to create m_manager
, if we look at ui->calendarwidget
is an instance of CalendarManager
.
来源:https://stackoverflow.com/questions/45134809/qcalendarwidget-how-to-highlight-dates