pyside2 qPushButton shape
-
Hi, @blossomsg
Please refer as below.
It's sample of push button with image using QPixmap / QIcon.import sys from PySide2.QtWidgets import QApplication, QPushButton from PySide2.QtGui import QPixmap, QIcon from PySide2.QtCore import QSize def clicked(): print("Button clicked!") app = QApplication(sys.argv) # Set Push Button button = QPushButton("Qt Chan") # Set image in Push Button pixmap = QPixmap("QtChan.png") button_icon = QIcon(pixmap) button.setIcon(button_icon) button.setIconSize(QSize(100,100)) button.show() button.clicked.connect(clicked) app.exec_()
-
Thanks for the reply,
The answer that i am seeking for is how to change the shape of the button but not just add a picture to the button,
i want a shape but not the whole box shape, QPushButton tends to provide the whole button,
i even tried using setMask but no success.below are eg the kind of shapes i want my button to look
These buttons are references of 3d package - Maya.
Thank You.
-
Hi
QToolbutton has a autoraise property that hides buttons borders until activated
providing a flat look.
Alternatively, you can apply style sheet to PushButton containing
border:0 to disable normal drawing and only image will be visible.
If you want a press down effect,
you can also use stylesheet to supply a second image to be shown when pressed
http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qpushbutton -
@mrjj
Hello,
sorry for late reply was busy related to the project.below is the reference code i wrote
from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCorewindow_wid = QtWidgets.QWidget()
vlayout_wid = QtWidgets.QVBoxLayout()
test_button_1 = QtWidgets.QPushButton()
test_button_2 = QtWidgets.QPushButton()
test_button_1_flat = test_button_1.setFlat(True)
test_button_1.setIcon(QtGui.QIcon("D:\All_Projs\NitinProj\anim_conglomeration\chess-piece.png"))
test_button_1.setStyleSheet("QPushButton:pressed{image:url(D:\All_Projs\NitinProj\anim_conglomeration\chess.png); border:none} QPushButton:hover{image:url(D:\All_Projs\NitinProj\anim_conglomeration\chess.png); border:none}")
vlayout_wid.addWidget(test_button_1)
vlayout_wid.addWidget(test_button_2)
window_wid.setLayout(vlayout_wid)
window_wid.show()the result that i am seeking for i was able to achieve it in linux or rather see in linux the highlight whenever i hover mouse on the image,
but windows is not able to show the stylesheet hover and pressed image url(highlights - darkening of the button).
But i am fine with the result and can continue right now.
am open for suggestionsThank You.
-
@blossomsg
Hi
Normally you would put the images in a qresource file.
Are those available with python ?
http://doc.qt.io/qt-5/resources.html -
this is with toggle
from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCorewindow_wid = QtWidgets.QWidget()
vlayout_wid = QtWidgets.QVBoxLayout()
test_button_1 = QtWidgets.QPushButton()
test_button_2 = QtWidgets.QPushButton()
test_button_1_flat = test_button_1.setFlat(True)
test_button_1.setCheckable(True)
test_button_1.setIcon(QtGui.QIcon("D:\All_Projs\NitinProj\anim_conglomeration\chess-piece.png"))
test_button_1.setStyleSheet("QPushButton:pressed{image:url(D:\All_Projs\NitinProj\anim_conglomeration\chess-piece.png); border:none}")
vlayout_wid.addWidget(test_button_1)
vlayout_wid.addWidget(test_button_2)
window_wid.setLayout(vlayout_wid)
window_wid.show() -
Ok, pretty much the same as without :)
-
@blossomsg
Hi
That is fine, please marked as solved then :)