简体   繁体   English

如何更改QCheckBox小部件中的刻度线颜色?

[英]how to change the color of tick in QCheckBox widget?

I want to change the color of TICK in the QCheckBox from black into dark blue, I tried QSS, but it doesn't work: 我想将QCheckBox中的TICK的颜色从黑色更改为深蓝色,我尝试了QSS,但它不起作用:

QCheckBox {
  background-color:blue;
  color:blue;
}

Using QSS only change the background color, but I want to change the color of the tick. 使用QSS仅更改背景颜色,但我想更改刻度的颜色。

Should I have to overload its paintEvent to Do It? 我是否必须重载其paintEvent才能执行此操作?

thanks. 谢谢。

----------- -----------

now I am trying QSS to solve it, 现在我正在尝试QSS来解决它,

 QCheckBox {
   spacing: 5px;
}

 QCheckBox::indicator {
 width: 13px;
 height: 13px;
 }

 QCheckBox::indicator:unchecked {
   image: url(:/images/checkbox_unchecked.png);
 }

 QCheckBox::indicator:unchecked:hover {
   image: url(:/images/checkbox_unchecked_hover.png);
 }

 QCheckBox::indicator:unchecked:pressed {
   image: url(:/images/checkbox_unchecked_pressed.png);
 }

 QCheckBox::indicator:checked {
   image: url(:/images/checkbox_checked.png);
 }

 QCheckBox::indicator:checked:hover {
   image: url(:/images/checkbox_checked_hover.png);
 }

 QCheckBox::indicator:checked:pressed {
   image: url(:/images/checkbox_checked_pressed.png);
 }

 QCheckBox::indicator:indeterminate:hover {
   image: url(:/images/checkbox_indeterminate_hover.png);
 }

 QCheckBox::indicator:indeterminate:pressed {
   image: url(:/images/checkbox_indeterminate_pressed.png);
 }

this is a qss example from Qt ref, how do I get the PATH ? 这是来自Qt ref的qss示例,如何获取PATH? what does ":" mean? 这是什么意思?

Paths in StyleSheets (and in Qt in general) StyleSheets(通常是Qt)中的路径

:/images/checkbox_checked.png

The PATH s in use in the qstylesheet reference are resource files. qstylesheet参考中使用的PATH是资源文件。 When you create a resource file in Qt Creator, it allows you to store images in a built in directory that gets compiled into your exe. 在Qt Creator中创建资源文件时,它允许您将图像存储在内置目录中,该目录被编译到exe中。

So :/ is the root of this resource path in the exe. 因此:/是exe中此资源路径的根。

http://qt-project.org/doc/qt-4.8/resources.html http://qt-project.org/doc/qt-4.8/resources.html

I don't think you can get the PATH of your exe from inside a QStyleSheet. 我认为您无法从QStyleSheet内部获取exe的PATH You can put it in at runtime, by building your QStyleSheet at runtime with something like: 您可以在运行时放入它,方法是在运行时构建QStyleSheet,例如:

widget->setStyleSheet(QString("first part of stylesheet") 
    + path_string + QString("another part of stylesheet"));

The way you probably should handle this is to use relative paths. 您可能应该处理此问题的方法是使用相对路径。 So instead of :/images/image.png , you could have ./images/image.png , where the folder "images" resides next to your exe. 因此,可以使用./images/image.png代替:/images/image.png ,其中“ images”文件夹位于exe旁边。

./application/application.exe
./application/images/image.png

And that is how relative paths work. 这就是相对路径的工作方式。

Also you should note that the working path is probably what is checked, not the application directory: 您还应该注意,工作路径可能是要检查的内容,而不是应用程序目录:

QDir::currentPath();

http://qt-project.org/doc/qt-5.0/qtcore/qdir.html#setCurrent http://qt-project.org/doc/qt-5.0/qtcore/qdir.html#setCurrent

http://en.wikipedia.org/wiki/Working_directory http://en.wikipedia.org/wiki/Working_directory

If your working directory (or in other words, the folder that your exe is ran from) is different from your exe's actual path, you will need to place your images folder in a different directory, for them to be found. 如果您的工作目录(或说运行exe的文件夹)与exe的实际路径不同,则需要将images文件夹放置在其他目录中才能找到它们。

You can also use the . 您也可以使用. and .. notations to describe how to go up and down in the working directory to find a folder or file. ..标记,描述如何在工作目录中向上和向下查找文件夹或文件。 . means the current directory, and .. means one directory up closer to the root. 表示当前目录,而..表示靠近根目录的一个目录。

Read the docs on QDir and QApplication in the static methods for more information on how to get the application directory and the current working directory. 阅读静态方法中有关QDir和QApplication的文档,以获取有关如何获取应用程序目录和当前工作目录的更多信息。

Sometimes I'll put this line in my code and look at the output to see the working directory: 有时,我会将这一行放入代码中,然后查看输出以查看工作目录:

system("dir"); // for windows

or 要么

system("pwd"); // for linux/mac

What does : mean? 是什么:是什么意思?

Also, the : in a QStyleSheet is referencing a sub component or a property of the item to the immediate left. 另外,QStyleSheet中的:指的是紧接左侧的子组件或项目的属性。 It behaves almost identically to css, but in css it is a . 它的行为几乎与CSS相同,但在CSS中是a . instead. 代替。

So it is something like: 因此,它类似于:

MyClassName::component-of-class:property-of-component:option-of-property
{
    property-of-option: setting-for-that-options-property;
}

Kind of like drilling down a big tree of settings. 有点像钻研一棵大树。 You can visualize a lot of them by digging around in Qt Designer on the properties pane. 您可以通过在属性窗格的Qt Designer中进行挖掘来可视化它们中的许多内容。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM