Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add clickable text with outline and color to QGraphicsScene



  • I am trying to make a clickable button like text appear on my QGraphicsScene.

    So far I have this. I want the T to be clickable and have an outline of my pen.I'd rather have an image imported and be clickable such as this
    And scale it to the size I like without losing much quality. I have no knowledge on how to import images and how to manipulate them so I chose to work with a text T as an alternative.

    import sys
    from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QPushButton, QAction,
                                 QComboBox, QDialog, QDialogButtonBox, QFormLayout, QGridLayout, QHBoxLayout,
                                 QLabel, QPushButton,
                                 QVBoxLayout, QFileDialog, QGroupBox, QGraphicsScene, QGraphicsView)
    from PyQt5.QtGui import QFont, QPixmap,QPainter, QPen, QBrush
    from PyQt5.QtCore import Qt,QRect, QSize, QDir
    
    class Test(QWidget):
        def __init__(self):
            super().__init__()
            self.title = 'Test '
            self.width = 1000
            self.height = 800
            self.gridSize = 20
    
            self.setGeometry(100, 100, self.width, self.height)
            self.setWindowTitle(self.title)
            self.initBoard()
            self.show()
    
        def initBoard(self):
            self.scene = QGraphicsScene()
            self.grayBrush = QBrush(Qt.black)
            self.pen = QPen(Qt.red)
            graphicsView = QGraphicsView(self.scene, self)
            graphicsView.setGeometry(0, 0, 800, 800)
            a = self.scene.addText("T", QFont("Sanserif", 100 * 0.66))
            a.setDefaultTextColor(Qt.gray)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = Test()
        app.exec_()
    

    I am new to PyQt5. I don't really know how to weave multiple widgets like QPainter into QGraphics. I tried searching the documentation for making the text have an outline, but I couldn't find anything. I found this but its in C++ and I don't understand how to implement it. When he talks about paths, I don't know what that means. I also don't know how to implement QPainter inside of my QGraphics.

    I'm basically stuck because I don't know where to look and where I do find stuff, I can't make sense of it. Could someone give me a hand where I could find information on how to achieve my goal? Or tell me how I could go about it?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you mean you want a button or a text like a hyperlink ?


Log in to reply