[PyQt5] KeepAspectRatio ans StyleSheet
-
Hi,
I am designing my own widget and I am looking for a way to keep the aspect ratio of an image I included using setStyleSheet.My widget is a QWidget, I set the background color and an image that fills the widget using setStyleSheet and border-image.
self.setStyleSheet("height: auto;background-color: "+color+" ;border-image:url("+self.img+"); ")
Then a Qlabel is set above the image using setWordWrap(True) and a QVBoxLayout.
Thank you for your help
-
Hi,
I am designing my own widget and I am looking for a way to keep the aspect ratio of an image I included using setStyleSheet.My widget is a QWidget, I set the background color and an image that fills the widget using setStyleSheet and border-image.
self.setStyleSheet("height: auto;background-color: "+color+" ;border-image:url("+self.img+"); ")
Then a Qlabel is set above the image using setWordWrap(True) and a QVBoxLayout.
Thank you for your help
@anto1ne This looks like what you want to do: https://stackoverflow.com/questions/30005540/keeping-the-aspect-ratio-of-a-sub-classed-qwidget-during-resize
-
HI @jsulm
I am not sure this is what I am looking for.
I am to keep the aspect ratio of the image inside the widget, not the aspect ratio of the widget.Here is more details about my code
import sys from PyQt5.QtWidgets import QLabel, QApplication, QWidget, QVBoxLayout from PyQt5.QtCore import Qt class MyWidget(QWidget): def __init__(self, text, image, color): QWidget.__init__(self) self.setMinimumSize(100, 100) self.img = image self.setStyleSheet("height: auto;background-color: "+color+" ;border-image:url("+self.img+"); ") vbox = QVBoxLayout() self.label = QLabel(self) self.label.setText(text) self.label.setStyleSheet("font: 18pt; color: white;") self.label.setAlignment(Qt.AlignCenter) self.label.setWordWrap(True) vbox.addWidget(self.label) self.setLayout(vbox) def set_text(self,text): "modifier le texte" self.label.setText(text) def set_color(self,color): "change la couleur" self.setStyleSheet("height: auto;background-color: %s ;" + "border-image:url(%s);" %color %self.img) def window(): app = QApplication(sys.argv) win = MyWidget("Hello","image.svg","green") win.show() sys.exit(app.exec_()) if __name__ == '__main__': window()
-
Hi
@jsulm , @Denni-0 : thank you for your help
Maybe, my explanation is not clear.
I mean that I want to keep the aspect ratio of the picture inside the widgetHere is an example :
When I display the widget for the first time
and once I resized the widget
this is not really sexy, that is why I want the aspect ratio of the picture to remain the same
-
Hi
@jsulm , @Denni-0 : thank you for your help
Maybe, my explanation is not clear.
I mean that I want to keep the aspect ratio of the picture inside the widgetHere is an example :
When I display the widget for the first time
and once I resized the widget
this is not really sexy, that is why I want the aspect ratio of the picture to remain the same