在Qt項目中為了實現不同的樣式,於是把所有控件的樣式都寫為QSS文件中,遇到了QCalendarWidget一臉懵逼
QCalendarWidget的是有好幾個控件組成的,那么為了設置其樣式表,只要分別設置的組成其的所有控件的樣式即可搞定,
為了獲取每個控件的objectName 有兩種方式,1.通過源碼,找到QCalendarWidget的源文件,如果在安裝Qt時,選擇安裝了源碼,可以找到每個控件的名稱,2.如果沒有安裝源碼,還有一種方式,通過循環打印出QCalendarWidget的里面的控件:
(object->metaObject()->className()) //類名
(object->objectName());
//類名對應的控件別名,如果object有子類,則QObjectList list = object->children();循壞輸出下,從而得到所有的控件類名和對應別名,然后分別設置其樣式,函數得詳細內容,可以參考下面這位作者的博客:
https://qtdebug.com/qtbook-qss-calendar/
下面附上從一個Qt centre http://www.qtcentre.org/threads/30478-How-To-Change-Style-Sheet-for-QCalendarWidget()里面找到的一個樣式例子以供參考:
/* navigation bar */ QCalendarWidget QWidget#qt_calendar_navigationbar { background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #cccccc, stop: 1 #333333); } QCalendarWidget QToolButton { height: 60px; width: 150px; color: white; font-size: 24px; icon-size: 56px, 56px; background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #cccccc, stop: 1 #333333); } QCalendarWidget QMenu { width: 150px; left: 20px; color: white; font-size: 18px; background-color: rgb(100, 100, 100); } QCalendarWidget QSpinBox { width: 150px; font-size:24px; color: white; background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #cccccc, stop: 1 #333333); selection-background-color: rgb(136, 136, 136); selection-color: rgb(255, 255, 255); } QCalendarWidget QSpinBox::up-button { subcontrol-origin: border; subcontrol-position: top right; width:65px; } QCalendarWidget QSpinBox::down-button {subcontrol-origin: border; subcontrol-position: bottom right; width:65px;} QCalendarWidget QSpinBox::up-arrow { width:56px; height:56px; } QCalendarWidget QSpinBox::down-arrow { width:56px; height:56px; } /* header row */ QCalendarWidget QWidget { alternate-background-color: rgb(128, 128, 128); } /* normal days */ QCalendarWidget QAbstractItemView:enabled { font-size:24px; color: rgb(180, 180, 180); background-color: black; selection-background-color: rgb(64, 64, 64); selection-color: rgb(0, 255, 0); } /* days in other months */ QCalendarWidget QAbstractItemView:disabled { color: rgb(64, 64, 64); }
原文鏈接:https://blog.csdn.net/u010311553/article/details/80705268