QT 5.15.2 How to display QImage/QPixmap as the background of a QCheckBox
-
I'm trying to create a list of check boxes with images on background, but i want to show the first frame of a mp4 file too.
When i try to set a QIcon with the frame from the video file, the icon does not appear because the indicator is on top of it. I try to change the background of the indicator to transparent, but this not work, the background persist with the default color.
The only way i found to show the QPixmap i have, is convert to a QIcon and set it on the QCheckBox object.if (fileInfo.suffix().toLower() == "mp4") { QPixmap pixmap; pixmap = get_first_frame(fileInfo.filePath()); QIcon icon(pixmap); imageCheckBox->setIcon(icon); imageCheckBox->setIconSize(QSize(240, 135)); imageCheckBox->setStyleSheet( QString("QCheckBox::indicator {" " width: 240px;" " height: 135px;" " background-color: transparent;" "}" "QCheckBox::indicator:checked {" " background-image: url(/path/to/check_gallery.png);" "}" ) ); }Has any other way to change the background of a QCheckBox or indicator using QImage / QPixmap? Or other way to set the indicator to transparent?
Note: The
background-image: url(/path/to/check_gallery.png);is just a custom border i have created, i have tested without this border, and nothing different happens. The indicator is this size because the intention is that it is possible to click on any part of the image and mark the QCheckBox. -
I'm trying to create a list of check boxes with images on background, but i want to show the first frame of a mp4 file too.
When i try to set a QIcon with the frame from the video file, the icon does not appear because the indicator is on top of it. I try to change the background of the indicator to transparent, but this not work, the background persist with the default color.
The only way i found to show the QPixmap i have, is convert to a QIcon and set it on the QCheckBox object.if (fileInfo.suffix().toLower() == "mp4") { QPixmap pixmap; pixmap = get_first_frame(fileInfo.filePath()); QIcon icon(pixmap); imageCheckBox->setIcon(icon); imageCheckBox->setIconSize(QSize(240, 135)); imageCheckBox->setStyleSheet( QString("QCheckBox::indicator {" " width: 240px;" " height: 135px;" " background-color: transparent;" "}" "QCheckBox::indicator:checked {" " background-image: url(/path/to/check_gallery.png);" "}" ) ); }Has any other way to change the background of a QCheckBox or indicator using QImage / QPixmap? Or other way to set the indicator to transparent?
Note: The
background-image: url(/path/to/check_gallery.png);is just a custom border i have created, i have tested without this border, and nothing different happens. The indicator is this size because the intention is that it is possible to click on any part of the image and mark the QCheckBox.@GuardianT said in QT 5.15.2 How to display QImage/QPixmap as the background of a QCheckBox:
any other way to change the background of a QCheckBox or indicator using QImage / QPixmap?
Subclass
QCheckBoxand create your own checkbox which support that (using integrated functions to set the first frame as background).May I ask why you want to do this? This seems very odd to do on a
QCheckBox.
If the only thing you want is a checkable "button" with some customizable background, I think using a checkable button or even a combination of aQCheckBoxand some label to hold your image might be better. -
@GuardianT said in QT 5.15.2 How to display QImage/QPixmap as the background of a QCheckBox:
any other way to change the background of a QCheckBox or indicator using QImage / QPixmap?
Subclass
QCheckBoxand create your own checkbox which support that (using integrated functions to set the first frame as background).May I ask why you want to do this? This seems very odd to do on a
QCheckBox.
If the only thing you want is a checkable "button" with some customizable background, I think using a checkable button or even a combination of aQCheckBoxand some label to hold your image might be better.@Pl45m4 this is to make an image/video gallery. I want to be able to select multiple files and save the selected files to a pendrive, for example. For this, I thought it would be a good idea to use a QCheckBox list, because i can return the objects of the list and can get the path of the selected files.
This is the first time i try to make something like that, looking at performance, i though use checkbox is better then use QWidget with click event. -
Hi,
A bit more complex but it seems that a QListView with checkable items would be better suited for your task.
You would need a custom model though. -
Hi,
A bit more complex but it seems that a QListView with checkable items would be better suited for your task.
You would need a custom model though. -
G GuardianT has marked this topic as solved on