Qt QDateEdit自定义样式表

时间:2019-09-08 14:13:58

标签: qt qtstylesheets qdateedit

我想在QCalendarPopup激活的情况下将自定义样式表应用于QDateEdit小部件。

我当前的CSS:

my current css

它是什么样的:

what it looks like

我需要解决三件事:

  • 日期名称的背景颜色和字体颜色(即周一,周二,周日,周日)
  • 更改周末的颜色
  • 更改左右箭头的背景颜色

谢谢!

---------修改后的.ui代码---

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>450</width>
    <height>230</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Swap Fly Analyzer</string>
  </property>
  <property name="windowIcon">
   <iconset>
    <normaloff>../icons/iapp.svg</normaloff>../icons/iapp.svg</iconset>
  </property>
  <property name="unifiedTitleAndToolBarOnMac">
   <bool>false</bool>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>450</width>
      <height>250</height>
     </rect>
    </property>
    <property name="styleSheet">
     <string notr="true"> QTabWidget::pane { /* The tab widget frame */
     border-top: 2px solid #C2C7CB;
     position: absolute;
     top: -0.5em;
 }

 QTabWidget::tab-bar {
     alignment: center;
 }

 /* Style the tab using the tab sub-control. Note that
     it reads QTabBar _not_ QTabWidget */
 QTabBar::tab {
    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #ffffff, stop: 1 #454545);                
border: 2px solid #C4C4C3;
     border-bottom-color: #C2C7CB; /* same as the pane color */
     border-top-left-radius: 4px;
     border-top-right-radius: 4px;
     min-width: 8ex;
     padding: 2px;
 }

 QTabBar::tab:selected, QTabBar::tab:hover {
    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #152c4a, stop: 1 #000000);
    color:white;
 }

 QTabBar::tab:selected {
     border-color: #9B9B9B;
     border-bottom-color: #C2C7CB; /* same as pane color */
 }

</string>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <widget class="QWidget" name="analysis">
     <attribute name="title">
      <string>Analysis</string>
     </attribute>
     <widget class="QPushButton" name="PushButton_Analysis_Go">
      <property name="geometry">
       <rect>
        <x>170</x>
        <y>190</y>
        <width>82</width>
        <height>15</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true"> QPushButton {
     border: 1px solid #152c4a;
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #152c4a, stop: 1 #000000);
     min-width: 80px;
     color: white;
 }

 QPushButton:pressed {
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #000000, stop: 1 #152c4a);
 }

 QPushButton:flat {
     border: none; /* no border for a flat push button */
 }

 QPushButton:default {
     border-color: navy; /* make the default button prominent */
 }</string>
      </property>
      <property name="text">
       <string>&lt; GO &gt;</string>
      </property>
     </widget>
     <widget class="QWidget" name="layoutWidget">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>21</y>
        <width>411</width>
        <height>52</height>
       </rect>
      </property>
      <layout class="QGridLayout" name="gridLayout_3">
       <item row="0" column="0">
        <widget class="QLabel" name="Label_Analysis_Currency">
         <property name="text">
          <string>Currency</string>
         </property>
        </widget>
       </item>
       <item row="0" column="1" rowspan="2">
        <spacer name="HorizontalSpacer_Analysis_Currency">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="0" column="2">
        <widget class="QComboBox" name="ComboBox_Analysis_Currency">
         <property name="styleSheet">
          <string notr="true"> QComboBox {
     border: 1px solid #152c4a;
}
</string>
         </property>
         <property name="editable">
          <bool>false</bool>
         </property>
        </widget>
       </item>
       <item row="1" column="5" rowspan="2">
        <spacer name="HorizontalSpacer_Analysis_EndDate">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="1" column="6" rowspan="2">
        <widget class="QDateEdit" name="DateEdit_Analysis_EndDate">
         <property name="styleSheet">
          <string notr="true">  QDateEdit {
     border: 1px solid #152c4a;
}
QDateEdit QAbstractItemView:hover
  {
    color:black;
    background-color: white;  

  }

  /* normal days */
QDateEdit QAbstractItemView:enabled 
  {
    color:white;
    background-color: #152c4a;  
    selection-background-color: white; 
    selection-color: #152c4a; 
  }

  /* days in other months */
  /* navigation bar */
QDateEdit QWidget#qt_calendar_navigationbar
{ 
background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #152c4a, stop: 1 #000000); 
}

QDateEdit QAbstractItemView:disabled 
{ 
color:gray; 
}
QCalendarWidget QTableView 
{
   alternate-background-color: black;
}
QCalendarWidget QTableView QLabel 
{
    color: &quot;white&quot;;
}
QCalendarWidget QToolButton#qt_calendar_prevmonth 
{
    qproperty-icon: url(&quot;icons/calendar_caretToLeft.png&quot;);
}
QCalendarWidget QToolButton#qt_calendar_nextmonth 
{
    qproperty-icon: url(&quot;icons/calendar_caretToRight.png&quot;);

}

 </string>
         </property>
         <property name="calendarPopup">
          <bool>true</bool>
         </property>
        </widget>
       </item>
       <item row="2" column="0">
        <widget class="QLabel" name="Label_Analysis_StartDate">
         <property name="text">
          <string>Start Date</string>
         </property>
        </widget>
       </item>
       <item row="2" column="1">
        <spacer name="HorizontalSpacer_Analysis_StartDate">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="2" column="2">
        <widget class="QDateEdit" name="DateEdit_Analysis_StartDate">
         <property name="styleSheet">
          <string notr="true">  QDateEdit {
     border: 1px solid #152c4a;
}
QDateEdit QAbstractItemView:hover
  {
    color:black;
    background-color: white;  

  }

  /* normal days */
QDateEdit QAbstractItemView:enabled 
  {
    color:white;
    background-color: #152c4a;  
    selection-background-color: white; 
    selection-color: #152c4a; 
  }

  /* days in other months */
  /* navigation bar */
QDateEdit QWidget#qt_calendar_navigationbar
{ 
background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #152c4a, stop: 1 #000000); 
}

QDateEdit QAbstractItemView:disabled 
{ 
color:gray; 
}
QCalendarWidget QTableView 
{
   alternate-background-color: black;
}
QCalendarWidget QTableView QLabel 
{
    color: &quot;white&quot;;
}
QCalendarWidget QToolButton#qt_calendar_prevmonth 
{
    qproperty-icon: url(&quot;icons/calendar_caretToLeft.png&quot;);
}
QCalendarWidget QToolButton#qt_calendar_nextmonth 
{
    qproperty-icon: url(&quot;icons/calendar_caretToRight.png&quot;);

}

 </string>
         </property>
         <property name="calendarPopup">
          <bool>true</bool>
         </property>
        </widget>
       </item>
       <item row="2" column="3">
        <spacer name="HorizontalSpacer_Analysis_DateMiddle">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="2" column="4">
        <widget class="QLabel" name="Label_Analysis_EndDate">
         <property name="text">
          <string>End Date</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
     <widget class="QWidget" name="layoutWidget">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>100</y>
        <width>411</width>
        <height>74</height>
       </rect>
      </property>
      <layout class="QGridLayout" name="gridLayout_4">
       <item row="0" column="0">
        <spacer name="horizontalSpacer_11">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="0" column="1">
        <spacer name="horizontalSpacer_10">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="0" column="2">
        <widget class="QLabel" name="Label_Analysis_Left">
         <property name="text">
          <string>Left</string>
         </property>
        </widget>
       </item>
       <item row="0" column="3">
        <spacer name="horizontalSpacer_9">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="0" column="4">
        <widget class="QLabel" name="Label_Analysis_Center">
         <property name="text">
          <string>Center</string>
         </property>
        </widget>
       </item>
       <item row="0" column="5">
        <spacer name="horizontalSpacer_8">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="0" column="6">
        <widget class="QLabel" name="Label_Analysis_Right">
         <property name="text">
          <string>Right</string>
         </property>
        </widget>
       </item>
       <item row="1" column="0">
        <widget class="QLabel" name="Label_Analysis_Rate">
         <property name="text">
          <string>Rate</string>
         </property>
        </widget>
       </item>
       <item row="1" column="1">
        <spacer name="horizontalSpacer_2">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="1" column="2">
        <widget class="QComboBox" name="ComboBox_Analysis_LeftRate">
         <property name="styleSheet">
          <string notr="true"> QComboBox {
     border: 1px solid #152c4a;
}
</string>
         </property>
         <property name="editable">
          <bool>false</bool>
         </property>
        </widget>
       </item>
       <item row="1" column="3">
        <spacer name="horizontalSpacer_3">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="1" column="4">
        <widget class="QComboBox" name="ComboBox_Analysis_CenterRate">
         <property name="styleSheet">
          <string notr="true"> QComboBox {
     border: 1px solid #152c4a;
}
</string>
         </property>
         <property name="editable">
          <bool>false</bool>
         </property>
        </widget>
       </item>
       <item row="1" column="5">
        <spacer name="horizontalSpacer_4">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="1" column="6">
        <widget class="QComboBox" name="ComboBox_Analysis_RightRate">
         <property name="styleSheet">
          <string notr="true"> QComboBox {
     border: 1px solid #152c4a;
}
</string>
         </property>
         <property name="editable">
          <bool>false</bool>
         </property>
        </widget>
       </item>
       <item row="2" column="0">
        <widget class="QLabel" name="Label_Analysis_Coefficient">
         <property name="text">
          <string>Coefficient</string>
         </property>
        </widget>
       </item>
       <item row="2" column="1">
        <spacer name="horizontalSpacer_5">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="2" column="3">
        <spacer name="horizontalSpacer_6">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="2" column="5">
        <spacer name="horizontalSpacer_7">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="2" column="2">
        <widget class="QLineEdit" name="LineEdit_Analysis_LeftCoefficient">
         <property name="styleSheet">
          <string notr="true">QLineEdit {
     border: 1px solid #152c4a;
 }</string>
         </property>
        </widget>
       </item>
       <item row="2" column="4">
        <widget class="QLineEdit" name="LineEdit_Analysis_CenterCoefficient">
         <property name="styleSheet">
          <string notr="true">QLineEdit {
     border: 1px solid #152c4a;
 }</string>
         </property>
        </widget>
       </item>
       <item row="2" column="6">
        <widget class="QLineEdit" name="LineEdit_Analysis_RightCoefficient">
         <property name="styleSheet">
          <string notr="true">QLineEdit {
     border: 1px solid #152c4a;
 }</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
     <widget class="QProgressBar" name="ProgressBar_Analysis_Status">
      <property name="geometry">
       <rect>
        <x>310</x>
        <y>190</y>
        <width>118</width>
        <height>20</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true"> QProgressBar {
     border: 1px solid #454545;
     text-align: center;
 }

 QProgressBar::chunk {
     background-color: #152c4a;
 }
</string>
      </property>
      <property name="value">
       <number>1</number>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="settings">
     <attribute name="title">
      <string>Settings</string>
     </attribute>
     <widget class="QWidget" name="layoutWidget">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>30</y>
        <width>320</width>
        <height>100</height>
       </rect>
      </property>
      <layout class="QGridLayout" name="gridLayout">
       <item row="0" column="0">
        <widget class="QLabel" name="Label_Settings_InputFile">
         <property name="text">
          <string>Input File</string>
         </property>
        </widget>
       </item>
       <item row="0" column="2">
        <widget class="QLineEdit" name="LineEdit_Settings_InputFile">
         <property name="styleSheet">
          <string notr="true">QLineEdit {
     border: 1px solid #152c4a;
 }</string>
         </property>
        </widget>
       </item>
       <item row="2" column="0">
        <widget class="QLabel" name="Label_Settings_BenchmarkSheetName">
         <property name="text">
          <string>Benchmark Sheet Name</string>
         </property>
        </widget>
       </item>
       <item row="3" column="2">
        <widget class="QLineEdit" name="LineEdit_Settings_RegressionListSheetName">
         <property name="styleSheet">
          <string notr="true">QLineEdit {
     border: 1px solid #152c4a;
 }</string>
         </property>
        </widget>
       </item>
       <item row="3" column="0">
        <widget class="QLabel" name="Label_Settings_RegressionListSheetName">
         <property name="text">
          <string>Regression List Sheet Name</string>
         </property>
        </widget>
       </item>
       <item row="1" column="0">
        <widget class="QLabel" name="Label_Settings_FSIRSListSheetName">
         <property name="text">
          <string>FS IRS List Sheet Name</string>
         </property>
        </widget>
       </item>
       <item row="2" column="2">
        <widget class="QLineEdit" name="LineEdit_Settings_BenchmarkSheetName">
         <property name="styleSheet">
          <string notr="true">QLineEdit {
     border: 1px solid #152c4a;
 }</string>
         </property>
        </widget>
       </item>
       <item row="1" column="2">
        <widget class="QLineEdit" name="LineEdit_Settings_FSIRSListSheetName">
         <property name="styleSheet">
          <string notr="true">QLineEdit {
     border: 1px solid #152c4a;
 }</string>
         </property>
        </widget>
       </item>
       <item row="1" column="1">
        <spacer name="HorizontalSpacer_Settings_FSIRSListSheetName">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="3" column="1">
        <spacer name="HorizontalSpacer_Settings_RegressionListSheetName">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="0" column="1">
        <spacer name="HorizontalSpacer_Settings_InputFile">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="2" column="1">
        <spacer name="HorizontalSpacer_Settings_BenchmarkSheetName">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
      </layout>
     </widget>
     <widget class="QWidget" name="layoutWidget">
      <property name="geometry">
       <rect>
        <x>20</x>
        <y>130</y>
        <width>321</width>
        <height>41</height>
       </rect>
      </property>
      <layout class="QGridLayout" name="gridLayout_2">
       <item row="0" column="1">
        <spacer name="verticalSpacer">
         <property name="orientation">
          <enum>Qt::Vertical</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>20</width>
           <height>40</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="1" column="0">
        <widget class="QPushButton" name="PushButton_Settings_Cancel">
         <property name="styleSheet">
          <string notr="true"> QPushButton {
     border: 1px solid #152c4a;
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #ffffff, stop: 1 #454545);
     min-width: 80px;
    color: #152c4a;
 }

 QPushButton:pressed {
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #454545, stop: 1 #ffffff);
 }

 QPushButton:flat {
     border: none; /* no border for a flat push button */
 }

 QPushButton:default {
     border-color: navy; /* make the default button prominent */
 }</string>
         </property>
         <property name="text">
          <string>Cancel</string>
         </property>
        </widget>
       </item>
       <item row="1" column="1">
        <spacer name="horizontalSpacer">
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
         <property name="sizeHint" stdset="0">
          <size>
           <width>40</width>
           <height>20</height>
          </size>
         </property>
        </spacer>
       </item>
       <item row="1" column="2">
        <widget class="QPushButton" name="PushButton_Settings_Save">
         <property name="styleSheet">
          <string notr="true"> QPushButton {
     border: 1px solid #152c4a;
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #152c4a, stop: 1 #000000);
     min-width: 80px;
     color: white;
 }

 QPushButton:pressed {
     background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                       stop: 0 #000000, stop: 1 #152c4a);
 }

 QPushButton:flat {
     border: none; /* no border for a flat push button */
 }

 QPushButton:default {
     border-color: navy; /* make the default button prominent */
 }</string>
         </property>
         <property name="text">
          <string>Save</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
    </widget>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
enter code here

1 个答案:

答案 0 :(得分:0)

带有日期名称的标题的背景色可以通过设置

QCalendarWidget QTableView 
{
    alternate-background-color: #6D6D6D;  /* day name/week number background */
}

非周末日期名称是默认的QLabel颜色,因此可以使用:

QCalendarWidget QTableView QLabel 
{
    color: "white";
}

可以使用C ++设置周末名称的颜色(标题和日历中的颜色),但我看不到从CSS更改颜色的方法。 (当然,与CSS相比,其他工作日颜色也可以用这种方式设置。)

QTextCharFormat fmt;
fmt.setForeground(QBrush(Qt::blue));
dateEdit->calendarWidget()->setWeekdayTextFormat(Qt::Saturday, fmt);
dateEdit->calendarWidget()->setWeekdayTextFormat(Qt::Sunday, fmt);

前进/后退按钮实际上是图标(假设您用箭头表示实际的绿色部分)。这对我来说在Qt 5.12中有效(我读过一些报告,该报告以前是错误的,但是一定已经修复了。)

QCalendarWidget QToolButton#qt_calendar_prevmonth 
{
    qproperty-icon: url(back.png);
}
QCalendarWidget QToolButton#qt_calendar_nextmonth 
{
    qproperty-icon: url(forward.png);
}

请注意,我在这里设置QCalendarWidget的样式,这是实际的显示类(我认为它可以与QDateEdit一起使用,因为日历小部件成为它的子类,但是我将对实际的日历小部件进行样式设置)

此外,在#qt_calendar_navigationbar中,您可能具有用于月份列表的QMenu和用于编辑年份的QSpinBox。您可能还希望/需要对它们进行样式设置。例如

QCalendarWidget QWidget#qt_calendar_navigationbar QMenu,
QCalendarWidget QWidget#qt_calendar_navigationbar QSpinBox 
{
    background-color: #152C4A;
    color: white;
}

HTH。

P.S。尽管此示例未使用CSS,但确实演示了如何从C ++自定义日历视图的某些部分:https://doc.qt.io/qt-5/qtwidgets-widgets-calendarwidget-example.html