2017-12-28 662 views
0

사용자가 QCalendarWidget에서 월요일, 화요일, 수요일, 목요일 또는 금요일 (평일)을 선택할 수 있기를 원합니다. 그러나 토요일이나 일요일은 아닙니다. (주말)QCalendarWidget에서 토요일과 일요일을 사용 중지 할 수 있습니까?

  • 이 기능은 QCalendarWidget에서 사용할 수 있습니까?
  • 그렇지 않으면 캘린더의 날짜를 어떻게 비활성화합니까?
+0

@eyllanesc 모호한을 추가 할 수 있지만 잘, 더 모호하지

문제. – user0042

+0

해결책이 확정되지 않은 것으로 보입니다. 그래서 답변이 잘못되었다고 생각합니다. 그래서 폐회 표를 제거 할 것입니다. – eyllanesc

답변

0

당신은 사용자 정의 CalendarWidget를 작성하고 당신이 원하는대로 셀을 다시 그릴 수 있습니다. 사용자의 요청에 따라 date.dayOfWeek()이 6 또는 7인지 확인할 수 있습니다.

이 예에서 달력 위젯은 평일 인 경우 선택한 날짜의 색을 변경하고 주말 인 경우 변경하지 않을 수 있습니다. 그러나 위젯 캘린더는 여전히 clicked 이벤트를받습니다. 희망이 도움이됩니다.

TestCalendar.h

class TestCalendar: public QCalendarWidget//: public QWidget// 
{ 
    Q_OBJECT 

    Q_PROPERTY(QColor color READ getColor WRITE setColor) 
public: 
    TestCalendar(QWidget* parent = 0);//();// 
    ~TestCalendar(); 

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

TestCalendar.cpp

편집
#include <QtWidgets> 

#include "TestCalendar.h" 

TestCalendar::TestCalendar(QWidget *parent) 
    : QCalendarWidget(parent) 
{ 
    m_currentDate = QDate::currentDate(); 
    m_outlinePen.setColor(Qt::blue); 
    m_transparentBrush.setColor(Qt::transparent); 
} 

TestCalendar::~TestCalendar() 
{ 
} 

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

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

void TestCalendar::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const 
{ 
    if (date.dayOfWeek() == 6 or date.dayOfWeek() == 7) { 
     painter->save(); 
     painter->drawText(rect, Qt::AlignCenter,QString::number(date.day())); 
     painter->restore(); 
    } else { 
     QCalendarWidget::paintCell(painter, rect, date); 
    } 
} 

: 나는보다 이미지 enter image description here

+0

답변 해 주셔서 감사합니다. 코드가 컴파일됩니다. 그러나 평일에도 주말에도 색상을 변경하지 않습니다. 또한 헤더 파일에는 QCalendarWidget 포인터 선언이 있습니다. 하지만 어디서나 사용되지는 않습니다. 아무것도 잊었 니? – ukll

+0

죄송합니다. 테스트 코드'* calendar'을 잊어 버렸습니다. 평일을 클릭하면 색깔이 변하는가? – GAVD

+0

주말의 숫자 색상이 검은 색으로, 배경이 흰색으로 바뀝니다. 그러나 그것은 아무것도하지 않습니다. 숫자를 다른 색상으로 변경하지도 않습니다. 잦은 수정을 드려서 죄송합니다. – ukll