QCalendarWidget - 如何突出显示日期 [英] QCalendarWidget - How to Highlight Dates

查看:43
本文介绍了QCalendarWidget - 如何突出显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有事件日期的 QList,我想在 QCalendarWidget 上突出显示这些日期,希望通过图像可以改变单元格颜色.

我可能在我的代码中犯了一个新手错误...

我修改的代码(

  1. 选择基类作为QCalendarWidgetPromoted Class Name:CalendarManagerHeader File: calendarmanager.h

  1. 按添加和推广:

输出截图:

注意:没有必要创建m_manager,如果我们看ui->calendarwidget的一个实例日历管理器.

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();
}

解决方案

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:

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

  1. 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.

这篇关于QCalendarWidget - 如何突出显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆