【问题标题】:Qt QCalendarWidget QSS StylingQt QCalendarWidget QSS 样式
【发布时间】:2013-01-18 01:36:02
【问题描述】:

我知道不支持 QCalendarWidget QSS 样式,但有人知道更改 12 部分颜色的解决方法吗? (1 部分为浅蓝色,2 部分为白色)

谢谢!

【问题讨论】:

    标签: qt qt4 qtstylesheets


    【解决方案1】:

    我检查了QCalendarWidget source code 并找到了解决方案。

    QCalendarWidget 内部有一个模型和一个视图来显示天数。 QCalendarModel 有一个 formatForCell(int, int) 函数,它为给定的单元格返回 QTextCharFormat。返回格式是合并QCalendarView调色板数据的结果,当前日期的格式(星期六和星期日显示为红色)和当前日期的格式,可以使用QCalendarWidget::setDateTextFormat函数设置。

    其实一个项目的背景是:

    format.setBackground(pal.brush(cg, header ? QPalette::AlternateBase : QPalette::Base));
    
    • palQCalendarView 的调色板;
    • cgcolor group
    • 当当前单元格是标题单元格时,header 为真(示例中的第 1 节)

    因此,您只需将自定义调色板设置为该内部QCalendarView。在源代码中我们可以发现QCalendarView 对象有一个名称“qt_calendar_calendarview”,我们可以使用它:

    QCalendarWidget *c = new QCalendarWidget;
    
    QTableView *view = c->findChild<QTableView*>("qt_calendar_calendarview");
    if (view)
    {
        QPalette pal = view->palette();
        pal.setColor(QPalette::Base, Qt::red);
        pal.setColor(QPalette::AlternateBase, Qt::green);
        view->setPalette(pal);
    }
    

    在我的示例中,第 1 部分为红色,第 2 部分为绿色。 此外,您可以为调色板中的每个color group 设置颜色,以便在其处于活动状态、非活动状态等时获得您喜欢的小部件。

    【讨论】:

      【解决方案2】:

      区域“1”自定义:

      QTextCharFormat format;
      format.setForeground(QBrush(Qt::blue));
      format.setBackground(QBrush(Qt::red);
      ui->calendarWidget->setHeaderTextFormat(format);
      

      区域“2”QSS CSS:

      QCalendarWidget QAbstractItemView
      {
      background-color: rgb(192,192,192); /* цвет фона текущего месяца */
      selection-background-color: yellow; /* цвет фона выбранного дня */
      selection-color: black; /* цвет текста выбранного дня */
      }
      

      #qt_calendar_calendarview
      {
      background-color: rgb(192,192,192); /* цвет фона текущего месяца */
      selection-background-color: yellow; /* цвет фона выбранного дня */
      selection-color: black; /* цвет текста выбранного дня */
      }
      

      ,其中#qt_calendar_calendarview - 来自d-&gt;m_view-&gt;setObjectName(QLatin1String("qt_calendar_calendarview")); in qcalendarwidget.cpp 的对象名称

      【讨论】:

        猜你喜欢
        • 2019-05-13
        • 2019-04-10
        • 2022-07-11
        • 1970-01-01
        • 1970-01-01
        • 2012-06-25
        • 2019-05-11
        • 1970-01-01
        • 2021-01-04
        相关资源
        最近更新 更多