QCalendarWidget - 如何突出日期

时间:2017-07-17 01:24:54

标签: c++ qt qt5

我有QCalendarWidget个活动日期,我希望在QCalendarWidget上突出显示这些日期,希望通过图片可以更改单元格颜色。

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

我从(Here)修改的代码应该使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 */ 使用红色边框绘制日期,但它不会......

mainwindor.cpp

#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.h

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

calendarmanager.cpp

Private Sub ImportFromGoogleSheet()
On Error GoTo ErrHandler

Dim appXL As Object 'Excel.Application
Dim wbk As Object 'Excel.Workbook
Dim wst As Object 'Excel.Worksheet
Dim Timer As Integer

Set appXL = CreateObject("Excel.Application")
appXL.Visible = True 'If you want to see the excel sheet - enable this row (good for debugging)
Set wbk = appXL.Workbooks.Add
Set wst = wbk.Worksheets(1)

With wst
    .QueryTables.Add Connection:= _
    "URL;https://connection to site here ", Destination:=.Range("$A$1")
    .Name = "Worksheet1"

    .QueryTables(1).Refresh
End With

'Wait for google-doc data to be downloaded.
Timer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And Timer < 40
    'Sleep 250 ' Wait 0.25 sec before re-checking data
    'Timer = Timer + 1
Loop

Dim rownum As Integer
rownum = 4
wst.cells(rownum, 2).Select
Do While (wst.cells(rownum, 2).Value <> "")
    Dim sqlStr As String
    Dim ts, dol As Date
    Dim sn, lt As String
    Dim nod As Integer
    ts = wst.cells(rownum, 2).Value
    dol = wst.cells(rownum, 5).Value
    sn = wst.cells(rownum, 3).Value
    lt = wst.cells(rownum, 4).Value
    nod = wst.cells(rownum, 6).Value

    sqlStr = "INSERT INTO table VALUES"
    DoCmd.SetWarnings False
    DoCmd.RunSQL sqlStr
    DoCmd.SetWarnings True

    rownum = rownum + 1
Loop

wbk.Close SaveChanges:=False 'Don't save excel sheet
appXL.Quit
Exit Sub

ErrHandler:
If (Err.Number = 3022) Then
    Debug.Print "Record Already Exists"
    Resume
End If
Debug.Print Err.Description & Err.Number

wbk.Close SaveChanges:=False 'Don't save excel sheet
appXL.Quit
End Sub

1 个答案:

答案 0 :(得分:1)

问题在于,在创建m_manager时,即使您传递了父ui->calendarWidget,也不会在GUI中包含此内容。

您应该做的是推广您的GUI以使用该小部件,您可以从设计视图轻松地使用该小部件。

  1. 右键单击calendarWidget并选择Promote to
  2. enter image description here

    1. 选择基类 a QCalendarWidget推荐类名CalendarManager标题文件:{{ 1}}
    2. enter image description here

      1. 按添加并升级:
      2. enter image description here

        输出屏幕截图:

        enter image description here

        注意:如果我们查看calendarmanager.hm_manager的实例,则无需创建ui->calendarwidget