displaying a text using QLabel ontop of another QLabel showing image has become such a pain

StackOverflow https://stackoverflow.com/questions/20591105

  •  01-09-2022
  •  | 
  •  

سؤال

Its simple thing, but I can't figure out why the overlay text is not displayed ontop of another QLabel,

here is the code I have that sets the overlay label that has text onto another existing label displaying image

def _buildUi(self):
    self.label = QtGui.QLabel()
    self.overlayExifText = QtGui.QLabel(self.label)
    self.overlayExifText.setSizePolicy(QtGui.QSizePolicy.Ignored,
        QtGui.QSizePolicy.Ignored)
    self.overlayExifText.setStyleSheet("QLabel { color : blue; }")
    self.overlayExifText.setAlignment(QtCore.Qt.AlignTop)
    self.label.setBackgroundRole(QtGui.QPalette.Base)
    self.label.setSizePolicy(QtGui.QSizePolicy.Ignored,
        QtGui.QSizePolicy.Ignored)
    self.label.setAlignment(QtCore.Qt.AlignCenter)
    self.setCentralWidget(self.label)

here is the method that updates the text for current image

def showImageByPath(self, path):
    if path:
        self.overlayExifText.setText("\n".join(list(utils.getExifData((path)))))
        image = QtGui.QImage(path)
        pp = QtGui.QPixmap.fromImage(image)
        self.label.setPixmap(pp.scaled(
                self.label.size(),
                QtCore.Qt.KeepAspectRatio,
                QtCore.Qt.SmoothTransformation))

only first letter of the text gets visible. also I tried setting some default text then it displays text with black background then that area also shows the little bit more of data the is yielded by the second method above. For full code have a look at this repo

هل كانت مفيدة؟

المحلول

To display image and text simultaneously following approach can be useful:

  • Set the image as background image on label:

    label.setStyleSheet("background-image: url(:/1.png);")

  • Set the text you want to display on top of the image:

    label.setText("text") ..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top