Qt笔记-qss定制QCalendarWidget

-由于QCalendarWidget是Qt的一种复杂控件,故除了qss外,还需要进行一些代码编写以达到定制效果

    if (  p->inherits("QCalendarWidget") )
    {
        QCalendarWidget *pCalendarWidget = static_cast<QCalendarWidget *>(p);
        pCalendarWidget->setMinimumSize(271,211);

        //去掉左手边的一年里的第几周的统计UI 
       pCalendarWidget->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);

       //设置图标
        {
            QToolButton *tools =pCalendarWidget->findChild<QToolButton*>(QLatin1String("qt_calendar_prevmonth"));
            QIcon i(":/image/PrevButton.png");
            tools->setIcon(i);
        }
        {
            QToolButton *tools = pCalendarWidget->findChild<QToolButton*>(QLatin1String("qt_calendar_nextmonth"));
            QIcon i(":/image/NextButton.png");
            tools->setIcon(i);
        }

       //更改当前的语言显示 csq
      //pCalendarWidget->setLocale( QLocale::English );

       //将周日放到前面
       pCalendarWidget->setFirstDayOfWeek(Qt::Sunday);

       //年 使用SpinBox风格
       QSpinBox *spinBox = pCalendarWidget->findChild<QSpinBox*>(QLatin1String("qt_calendar_yearedit"));
       //IStyleSetting.SetStyle(spinBox  , EStyle_SpinBox_Litter);

       StyleCalendar *date = new StyleCalendar( p->parent() ) ;//传入父指针对象是为了自动帮忙析构
       date->setCalendar(pCalendarWidget);
    }
static QString strStyle;
    if ( strStyle.isEmpty() )
     {
        strStyle.append("QToolButton#qt_calendar_monthbutton,#qt_calendar_yearbutton{ ");//月、年按钮的字体
        strStyle.append("    color:#FFFFFF; ");
        strStyle.append(" font: 10pt; "  );
        strStyle.append("} ");
        strStyle.append("QToolButton#qt_calendar_prevmonth,#qt_calendar_nextmonth{ ");//需要设置前后月按钮图标的大小
        strStyle.append("    icon-size:34px 26px; ");
        strStyle.append("    border:0px solid; ");
        strStyle.append("} ");
        strStyle.append("QCalendarWidget QWidget#qt_calendar_navigationbar{ ");
        strStyle.append("background-color: #1C8DC7; ");
        strStyle.append("min-height:26px; ");
        strStyle.append("max-height:26px; ");
        strStyle.append("} ");
        strStyle.append("QCalendarWidget QTableView  ");
        strStyle.append("{ ");
        strStyle.append("    alternate-background-color:#DEDEDE; ");
        strStyle.append("    background-color: white; ");
        strStyle.append("} ");
        strStyle.append("QCalendarWidget QMenu { ");
        strStyle.append("     background-color: white;  ");
        strStyle.append("     border: 1px solid white; ");
        strStyle.append(" } ");
        strStyle.append("QCalendarWidget  QMenu::item { ");
        strStyle.append("     background-color: transparent; ");
        strStyle.append("     padding:2px 8px; ");
        strStyle.append("     margin:0px 1px; ");
        strStyle.append("     border-bottom:1px solid #DBDBDB; ");
        strStyle.append("     color: #444444; ");
        strStyle.append(" } ");
        strStyle.append(" QMenu::item:selected { ");
        strStyle.append("     background-color:  #DEDEDE; "); //这一句是设置菜单项鼠标经过选中的样式
        strStyle.append(" } ");
        strStyle.append("QCalendarWidget QAbstractItemView:enabled  ");
        strStyle.append("{ ");
        strStyle.append("  font: 10pt; ");
        strStyle.append("  color: #444444;   ");
        strStyle.append("  selection-background-color: #BCDDEE;  ");
        strStyle.append("  selection-color: #444444;  ");
        strStyle.append("} ");
        strStyle.append("QCalendarWidget QAbstractItemView:disabled { color:#C0C0C0; ");
        strStyle.append(" } ");
     }

    if (  p->inherits("QCalendarWidget") )
    {
        QCalendarWidget *pCalendarWidget = static_cast<QCalendarWidget *>(p);

       //设置周六、日的字体颜色
       {
           QTextCharFormat f =pCalendarWidget->weekdayTextFormat(Qt::Monday);
           f.setForeground( QBrush(QColor("#848484")) );//参照手机,将周六日的字体颜色改一下 csq
           QTextCharFormat weekdays = f;
           pCalendarWidget->setWeekdayTextFormat(Qt::Sunday,weekdays);
           pCalendarWidget->setWeekdayTextFormat(Qt::Saturday,weekdays);
       }
    }
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值