pyside2 qPushButton shape



  • i downloaded an image black and white and hollow from middle
    i want yo use that as my button
    but when i use QIcon or QPixmap, none of the permutations seem to work
    can any one suggest a way i am using this for my proj


  • Qt Champions 2018

    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_()
    

    0_1546069284507_Sample_QpushButton.png



  • 3_1546191062219_button_crop_for_qt_not_like_this.png
    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
    2_1546191062219_button_crop_for_qt_hlep_03.png1_1546191062219_button_crop_for_qt_hlep_02.png 0_1546191062218_button_crop_for_qt_hlep_01.png

    These buttons are references of 3d package - Maya.

    Thank You.


  • Lifetime Qt Champion

    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 QtCore

    window_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 suggestions

    Thank You.


  • Lifetime Qt Champion

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

    window_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()


  • Lifetime Qt Champion

    @blossomsg

    Ok, pretty much the same as without :)



  • Hi,
    i'll look into the qresource and will check and keep you posted

    but for now i think so i'll resolve it if its ok.
    otherwise the project will lag.

    Thanks anyways but will open a new thread for qresource.


  • Lifetime Qt Champion

    @blossomsg
    Hi
    That is fine, please marked as solved then :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.