Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. pyside2 qPushButton shape
Forum Updated to NodeBB v4.3 + New Features

pyside2 qPushButton shape

Scheduled Pinned Locked Moved Solved Qt for Python
pyside2pythonqt for python
10 Posts 3 Posters 4.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blossomsg
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • KazuoAsanoK Offline
      KazuoAsanoK Offline
      KazuoAsano
      Qt Champions 2018
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      1
      • B Offline
        B Offline
        blossomsg
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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

          B 1 Reply Last reply
          1
          • mrjjM mrjj

            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

            B Offline
            B Offline
            blossomsg
            wrote on last edited by
            #5

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

            mrjjM 1 Reply Last reply
            0
            • B blossomsg

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

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0
              • B Offline
                B Offline
                blossomsg
                wrote on last edited by
                #7

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

                mrjjM 1 Reply Last reply
                0
                • B blossomsg

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @blossomsg

                  Ok, pretty much the same as without :)

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    blossomsg
                    wrote on last edited by
                    #9

                    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.

                    mrjjM 1 Reply Last reply
                    0
                    • B blossomsg

                      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.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved