QLabel with QPixmap, setScaledContents working?
-
@SGaist here's some test code with a picture i found on the net:
def show_image(self): # self.textEdit.setHtml('<img height="800" width="800" src="img/SGaist.png">test</img>') testL = QtWidgets.QLabel(self.textEdit) testP = QtGui.QPixmap('img/SGaist.png') # testP2 = testP.scaledToWidth(1000) # testL.setPixmap(testP2) testL.setPixmap(QtGui.QPixmap(testP)) testL.setScaledContents(1) testL.show() print(testL.hasScaledContents()) print(testL.size())
The QTextEdit text content uses all available space. The testP image stays at it's original size. The testP2 image does scale. The html image also, i guess the html approach doesn't support auto scaling to 100% like regular CSS.
-
@Tink said in QLabel with QPixmap, setScaledContents working?:
testL.setPixmap(QtGui.QPixmap(testP)) testL.setScaledContents(1)
I would invert these two lines. It would also be better to use
testL.setScaledContents(True)
as the original uses a boolean value. -
Hmm... so what is weird is that when i add a label in Qt Designer, it works as expected with default settings. The difference seems to be in having another parent frame (it doesn't allow placing it in the QtextEdit). Also the relation with other frames and layouts influences it. I can't get it to work from code.
Now i'm able to create something similar to what i had in mind by hiding and showing a couple of frames by using the QLabel from Qt Designer However it would be nice to know what is missing when doing this from code and in relation to other frames/widgets.
Guess i can mark it as solved?
-
Can you show a minimal complete python example that shows that behavior ?
-
@SGaist check this:
import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * class MainW(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): label_style = 'background-color:white; border:2 solid blue' f1 = QFrame() f1.setFixedSize(100, 100) f1.setMaximumSize(9999, 9999) f1.setStyleSheet('background-color:red; border:2 solid black') f2 = QWidget() f2.setMinimumSize(100, 100) f2.setMaximumSize(9999, 9999) f2.setStyleSheet('background-color:green; border:2 solid black') te = QTextEdit() te.setMaximumSize(9999, 9999) te.setStyleSheet('background-color:grey; border:2 solid black') img = QPixmap('small_image.jpg') lbl_1 = QLabel() lbl_1.setStyleSheet(label_style) lbl_1.setScaledContents(True) lbl_1.setPixmap(img) lbl_2 = QLabel() lbl_2.setStyleSheet(label_style) lbl_2.setScaledContents(True) lbl_2.setFixedWidth(900) lbl_2.setPixmap(img) lbl_3 = QLabel() lbl_3.setStyleSheet(label_style) lbl_3.setScaledContents(True) lbl_3.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) lbl_3.setPixmap(img) lbl_4 = QLabel() lbl_4.setStyleSheet(label_style) lbl_4.setScaledContents(True) lbl_4.setPixmap(img) vbox = QVBoxLayout() vbox.addWidget(f1) lbl_1.setParent(f1) vbox.addWidget(f2) lbl_2.setParent(f2) vbox.addWidget(te) lbl_3.setParent(te) vbox.addWidget(lbl_4) central_widget = QWidget() central_widget.setLayout(vbox) central_widget.setStyleSheet('background-color:yellow') self.setCentralWidget(central_widget) self.resize(1000, 1000) self.show() if __name__ == '__main__': app = QApplication(sys.argv) mw = MainW() sys.exit(app.exec_())
It looks like the label itself is not scaling when not directly in a layout. Or am i doing something wrong?
-
This minimal sample
if __name__ == '__main__': app = QApplication(sys.argv) label = QLabel() label.setScaledContents(True) label.setPixmap(QPixmap("Path/to/image.extension")) label.show() sys.exit(app.exec_())
will show a QLabel with the image in it, if you resize the QLabel, the pixmap will follow.
-
@Tink said in QLabel with QPixmap, setScaledContents working?:
@SGaist yes indeed. The pixmap in your example scales just as fine as in my examples. As i said it's not that the scaling of the image is failing but that the label is not always expanding when i assumed it would do so.
that is right,
without a layout or manual resizing, the QLable will, when a pixmap is asigned, scale itself to the content width and height of said pixmap. -
@J.Hilk Thanks for confirming / explaining. I still have much to learn. Started with QtDesigner and got some quick results, but it's not the best way to learn how things like layouts work. And it can be confusing. E.g. in designer i can't give a textedit a layout or child widget (at least i don't know how). Well perhaps it's best for me to recreate the ui from code only and get a better understanding that way.
-
Because it’s not really the purpose of QTextEdit. What exactly is your goal with it ?
-
Oh, this started with me trying to use insertHtml to get an auto scaled image in a QTextEdit. Well that didn't work... so i tried a Qlabel. Which i have separate at the moment. But also learned how to make it work as a child widget from code. So yeah, life is good right now.