Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13921

Qt QGraphicsTextItem Set Item To Stick To Dragged Position

$
0
0

I'm trying position a text and I want it to stick to where ever the user has dragged it for example bottom right it will stay there and when the view gets resized the text item stays bottom right, so it dynamically changes position with the view.

I've tried to SetPos but I think due to the maths I'm having a hard time to figure out how to set the correct POS for the text as the view is resizing. I've created a simpler version of the code I have that allows text dragging, if anyone has solved this in their project I would love to hear how, thanks.

import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QGraphicsScene, QGraphicsView, QGraphicsTextItemfrom PyQt5.QtCore import Qtclass MyWidget(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        layout = QVBoxLayout()        self.setLayout(layout)        # Create a QGraphicsScene        self.scene = QGraphicsScene()        # Create a QGraphicsTextItem and add it to the scene        self.text_item = QGraphicsTextItem("Hello, World!")        self.text_item.setTextInteractionFlags(Qt.TextEditorInteraction)        self.text_item.setFlags(QGraphicsTextItem.ItemIsSelectable | QGraphicsTextItem.ItemIsMovable | QGraphicsTextItem.ItemIsFocusable)        self.scene.addItem(self.text_item)        # Set initial font size        self.font_size = 12        # Create a QGraphicsView and set the scene        self.view = QGraphicsView(self.scene)        layout.addWidget(self.view)if __name__ == '__main__':    app = QApplication(sys.argv)    widget = MyWidget()    widget.show()    sys.exit(app.exec_())

Viewing all articles
Browse latest Browse all 13921

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>