3

I want to make a specific button in PyQt toolbar appeared as pressed(with blue background). Suppose when I hit the toolbar button I want it to be appeared as pressed

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Window(QtGui.QMainWindow):   
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 700, 700)
        self.setWindowTitle('Rich Text Editor')
        self.statusBar = QStatusBar()
        self.textEdit = QtGui.QTextEdit()
        self.setCentralWidget(self.textEdit)
        self.setStatusBar(self.statusBar)
        self.home()

    def home(self):
        changeBoldActionTB = \
        QtGui.QAction(QtGui.QIcon('bold-text-option.png'),
                      'Make the text bold', self)
        changeBoldActionTB.triggered.connect(self.changeBold)

        self.formatbar = QToolBar()
        self.addToolBar(Qt.TopToolBarArea, self.formatbar)
        self.formatbar.addAction(changeBoldActionTB)
        self.show()

    def changeBold(self):
         pass
         #I think this does't matter        

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()

I have two toolbars.I am planning to use cursorPositionChanged to do this but still is there a way in PyQt to do this enter image description here

reproduible code: https://files.fm/u/h4c2amdx

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Burhan Asif
  • 228
  • 5
  • 13

2 Answers2

6

Instead of using QAction you must use QToolButton and set the checkable property to True:

toolButton.setCheckable(True)

Example:

class Window(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setWindowTitle('Rich Text Editor')
        self.statusBar = QStatusBar(self)
        self.textEdit = QtGui.QTextEdit(self)
        self.setCentralWidget(self.textEdit)
        self.setStatusBar(self.statusBar)

        self.home()

    def home(self):
        toolButton = QToolButton(self)
        toolButton.setIcon(QtGui.QIcon('bold-text-option.png'))
        toolButton.setCheckable(True)
        toolButton.toggled.connect(self.onToggled)

        self.formatbar = QToolBar(self)
        self.addToolBar(Qt.TopToolBarArea, self.formatbar)
        self.formatbar.addWidget(toolButton)

    def onToggled(self, checked):
        print(checked)

Screenshots:

enter image description here

enter image description here

Plus: To set the value manually and to obtain the status the following instructions are used:

toolButton.setChecked(True) # set State
print(toolButton.isChecked()) # get State
toolButton.toggle() # change state 
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
0

Actions also have a checable flag and they can act as a toggle switch. Sorry, I don’t know English. So I showed it with code.

# add this code before self.formatbar = QToolBar()
# and create img file blue_bold-text-option.png
self.changeBoldActionTB.setCheckable(True)
icon = QIcon()
icon.addFile('bold-text-option.png', QSize(), QIcon.Normal, QIcon.Off)
icon.addFile('blue_bold-text-option.png', QSize(), QIcon.Normal, QIcon.On)
self.changeBoldActionTB.setIcon(icon)
Павел
  • 111
  • 2
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Dec 11 '20 at 09:46