1

Here is my sample program. When I need to draw a line for x axis and y label for y axis .so can any one help me how to add a line and labels to scene.i tried different ways but i didn't get the proper output.

How can I add labels to this scene?

Given below is my sample code:

import sys
from pyface.qt import QtGui, QtCore
# class ScanView(QtGui.QGraphicsView):
#     def __init__(self,X=5, Y=5,parent=None):
#         super(ScanView, self).__init__(parent)
class DemoApp(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(DemoApp, self).__init__()
        self.w= QtGui.QGridLayout()
        self.v= QtGui.QGraphicsView()
        self.w.addWidget(self.v)
        self.widget = QtGui.QWidget()
        self.widget.setLayout(self.w)
        self.setCentralWidget(self.widget)
        self._squares = []

        n_rows, n_cols = 3, 2
        squareLB = 50
        label = QtGui.QLabel("xaxis")
        label1 = QtGui.QLabel("yaxis")
        self._scene = QtGui.QGraphicsScene()
        mytext1 = QtGui.QGraphicsSimpleTextItem('label')
        self._scene.addItem(mytext1)
        mytext2 = QtGui.QGraphicsSimpleTextItem('label1')
        self._scene.addItem(mytext2)
        self.v.setScene(self._scene)

        self.pen = QtGui.QPen(QtCore.Qt.DotLine)
        self.pen.setColor(QtCore.Qt.red)

        width, height = (2 + 2)*squareLB, (3 + 2)*squareLB
        self._scene = QtGui.QGraphicsScene(0, 0, max(708, width), height)

        p = squareLB if width > 708 else (708.0-2*squareLB)/2.0

        for i in range(n_rows):
            for j in range(n_cols):
                it = self._scene.addRect(QtCore.QRectF(0,0,squareLB,squareLB),self.pen)
                it.setPos(p + j*squareLB, i*squareLB)

                self._squares.append(it)

        self.v.setScene(self._scene)
class Settings(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Settings, self).__init__(parent)

        self.folder = QtGui.QPushButton("Folder", clicked=self.showSettings)
        central_widget = QtGui.QWidget()
        self.setCentralWidget(central_widget)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.folder)
        self.scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.scrollArea.setBackgroundRole(QtGui.QPalette.Light)
        hlay = QtGui.QHBoxLayout(central_widget)
        hlay.addLayout(vbox)
        hlay.addWidget(self.scrollArea)
        self.setGeometry(200, 100, 300, 300)
    def showSettings(self):
        self.view = DemoApp()
        self.newwidget = QtGui.QWidget()
        hlay = QtGui.QHBoxLayout(self.newwidget)
        hlay.addWidget(self.view)
        self.scrollArea.setWidget(self.newwidget)
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Settings()
    ex.show()
    sys.exit(app.exec_())
if __name__ == '__main__':
    main()

enter image description here

closed as off-topic by PolyGeo Dec 12 '18 at 3:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Have you tried calling setPos(qreal x, qreal y) on your label variables before adding to the scene? e.g. mytext1.setPos (0,0). Also note that the axes in a pyQT scene the origin is top left and y axis positive down. – JimT Dec 6 '18 at 10:04
  • There does not appear to be a GIS component to this pure PyQt question. My understanding is that PyQt is not a spatial library. – PolyGeo Dec 12 '18 at 3:50
0

As answered in comments:

The QGraphicsSimpleTextItem class inherits from the QGraphicsItem class which features a method setPos(qreal x, qreal y) to position the item.

Therefore position of your text items can be changed by calling setPos. For example:

mytext1.setPos (0,0)

Not the answer you're looking for? Browse other questions tagged or ask your own question.