[英]How to set the size of a QCheckBox's indicator?
這段代碼:
QCheckBox* selection = new QCheckBox(backgroundEditor);
selection->setGeometry(
0 , //Left
0 , //Top
buttonHeight , //Width
buttonHeight //Height
);
selection->setStyleSheet(QString(
"background-color: white;"
" color: black;"
"QCheckBox::indicator "
"{"
" width: %1px;"
" height: %1px;"
"}"
).arg(buttonHeight));
生產:
白色區域具有正確的幾何形狀,但是我希望指示器框(以及單擊目標)充滿它。 我怎么做?
進行一些實驗表明,顏色規格消除了尺寸。 刪除它可以使尺寸正確,但是顏色當然是錯誤的。 我怎么都可以?
嘗試這個:
ui->checkBox->setStyleSheet("QCheckBox::indicator {width:10px;height: 10px;}");
您必須為每個狀態手動設置圖標,如下所示:
QCheckBox::indicator:checked
{
image: url(../Checkbox_checked_normal.png);
}
QCheckBox::indicator:unchecked
{
image: url(../Checkbox_unchecked_normal.png);
}
QCheckBox::indicator:checked:hover
{
image: url(../Checkbox_checked_hovered.png);
}
QCheckBox::indicator:unchecked:hover
{
image: url(../Checkbox_unchecked_hovered.png);
}
QCheckBox::indicator:checked:pressed
{
image: url(../Checkbox_checked_pressed.png);
}
QCheckBox::indicator:unchecked:pressed
{
image: url(../Checkbox_unchecked_pressed.png);
}
QCheckBox::indicator:checked:disabled
{
image: url(../Checkbox_checked_disabled.png);
}
感謝@aghilpro最終使我明白了這一點,但是不幸的是,我無法接受他的回答,因為尚未對其進行編輯以包括實際問題。 (盡管我確實對此表示贊同),所以這是發生的事情:
我之所以使用此代碼,是因為“松散”的顏色以前一直奏效-它們僅適用於對象的每個部分,這對我所做的事情是很好的:
QCheckBox* selection = new QCheckBox(backgroundEditor);
selection->setStyleSheet(QString(
"background-color: white;"
" color: black;"
"QCheckBox::indicator "
"{"
" width: %1px;"
" height: %1px;"
"}"
).arg(buttonHeight));
如您所見,我只是將我的google搜索結果附加到已經起作用的內容上...添加的內容完全沒有任何作用,因為全局說明符使它無效了。 基本上,我創建了一種全局樣式,該樣式指定了除顏色以外的所有內容的默認值,並且它覆蓋了indicator
的本地顏色。 相反,我需要這樣做:
QCheckBox* selection = new QCheckBox(backgroundEditor);
selection->setStyleSheet(QString(
"QCheckBox"
"{"
" background-color: white;"
" color: black;"
"}"
"QCheckBox::indicator"
"{"
" width: %1px;"
" height: %1px;"
"}"
).arg(buttonHeight));
基本上,將顏色設置為應該應用的顏色,從而完全沒有全局樣式。
如果某人對實際發生的事情有更好的了解,請告訴我,以便我可以解決此問題,或者編寫您自己的更好的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.