Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT 5.15.2 How to display QImage/QPixmap as the background of a QCheckBox

QT 5.15.2 How to display QImage/QPixmap as the background of a QCheckBox

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 753 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GuardianT
    wrote on last edited by
    #1

    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.

    Pl45m4P 1 Reply Last reply
    0
    • G GuardianT

      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.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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 QCheckBox and 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 a QCheckBox and some label to hold your image might be better.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      G 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @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 QCheckBox and 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 a QCheckBox and some label to hold your image might be better.

        G Offline
        G Offline
        GuardianT
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          G 1 Reply Last reply
          1
          • SGaistS SGaist

            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 Offline
            G Offline
            GuardianT
            wrote on last edited by
            #5

            @SGaist I currently have a QScrollArea with a QVBoxLayout, my code adds QCheckBox for the number of .jpg and .mp4 files

            1 Reply Last reply
            0
            • G GuardianT has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved