Reputation: 419
I was wondering how do you add text inside QGraphicsPolygonItem
?
In my script , I use QPolygonF
and setPolygon
for drawing items and I was wondering if you can insert text inside it ?
I was doing some research, but all I could find is that some people use QGraphicsTextItem
instead of polygon item or using QPainter
which I don't know how to combine with my QPolygonF
. Can somebody advice me how to solve my problem ?
Edit:
Sorry for not providing any example. Here is example of polygon item -
from PySide2.QtGui import QColor, QPolygonF, QPen, QBrush
from PySide2.QtCore import Qt, QPointF, QPoint
from PySide2.QtWidgets import QDialog, QVBoxLayout, QGraphicsView, QGraphicsScene, QGraphicsPolygonItem, QApplication, \
QFrame, QSizePolicy
points_list = [[60.1, 19.6, 0.0], [60.1, 6.5, 0.0], [60.1, -6.5, 0.0], [60.1, -19.6, 0.0], [60.1, -19.6, 0.0],
[20.0, -19.6, 0.0], [-20, -19.6, 0.0], [-60.1, -19.6, 0.0], [-60.1, -19.6, 0.0], [-60.1, -6.5, 0.0],
[-60.1, 6.5, 0.0], [-60.1, 19.6, 0.0], [-60.1, 19.6, 0.0], [-20.0, 19.6, 0.0], [20.0, 19.6, 0.0],
[60.1, 19.6, 0.0]]
class MainWindow(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent=parent)
self.create()
def create(self, **kwargs):
main_layout = QVBoxLayout()
graphics = MainGraphicsWidget()
main_layout.addWidget(graphics)
self.setLayout(main_layout)
class MainGraphicsWidget(QGraphicsView):
def __init__(self, parent=None):
super(MainGraphicsWidget, self).__init__(parent)
self._scene = QGraphicsScene(backgroundBrush=Qt.gray)
self.setScene(self._scene)
self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
self.setResizeAnchor(QGraphicsView.AnchorUnderMouse)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setBackgroundBrush(QBrush(QColor(30, 30, 30)))
self.setFrameShape(QFrame.NoFrame)
self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
self.testButton = GraphicsButton()
self._scene.addItem(self.testButton)
class GraphicsButton(QGraphicsPolygonItem):
def __init__(self, parent=None):
super(GraphicsButton, self).__init__(parent)
self.myPolygon = QPolygonF([QPointF(v1, v2) for v1, v2, v3 in points_list])
self.setPen(QPen(QColor(0, 0, 0), 0, Qt.SolidLine, Qt.FlatCap, Qt.MiterJoin))
self.setPolygon(self.myPolygon)
self.setBrush(QColor(220, 40, 30))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.setGeometry(500, 100, 500, 900)
window.show()
sys.exit(app.exec_())
So here should be red square like item in center and does anybody know how to put some text inside ?
Here is screenshot of shapes and text which I would like to get:
Upvotes: 2
Views: 2287
Reputation: 48384
The simplest solution is to add a QGraphicsSimpleTextItem that is a children of the graphics item.
For simple shapes as rectangles and regular polygons you can then place the item at the center of the item. Remember that the text will not consider the parent item shape, and you'll have to take care of it in some way; this means that you have to consider the text width and height, and parent item shape (that's true for irregular shapes, but also for triangles and rotated squares).
class GraphicsButton(QGraphicsPolygonItem):
def __init__(self, parent=None):
# ...
self.textItem = QGraphicsSimpleTextItem('I am a very large rectangle', self)
rect = self.textItem.boundingRect()
rect.moveCenter(self.boundingRect().center())
self.textItem.setPos(rect.topLeft())
As you can see, the result is outside the parent boundaries:
A possible alternative is to use the QGraphicsTextItem and set its textWidth:
self.textItem = QGraphicsTextItem(self)
self.textItem.setHtml('<center>I am a very large rectangle</center>')
self.textItem.setTextWidth(self.boundingRect().width())
rect = self.textItem.boundingRect()
rect.moveCenter(self.boundingRect().center())
self.textItem.setPos(rect.topLeft())
Note that, opposite to the QGraphicsSimpleTextItem (which uses the default black color for painting), QGraphicsTextItem uses the current palette WindowText role of the widget, inherited from the application, from the view, or from any of the view's parent that has previously set it (once the default text color is set, it won't be changed even if the palette has changed).
Upvotes: 4