0

Here is my program in that i have a cell of the grid, When i click add rows and columns button, i want to add empty columns and rows for that grid, when i click add columns button, i want to add no of columns in top of the grid not bottom of the grid.But i am getting the bottom of the grid only how can i change the way of adding the columns on top of the grid.Thank you in advance. This is my code:

import sys
from pyface.qt import QtGui, QtCore



class Setting:
    WIDTH = 80
    HEIGHT = 80



class QS(QtGui.QGraphicsScene):
    def __init__(self, x=3, y=4,parent=None):
        super(QS, self).__init__( parent)
        self.x = x
        self.y = y


        pixmap = QtGui.QPixmap("./img/tick.png").scaled(Setting.WIDTH, Setting.HEIGHT,
            QtCore.Qt.IgnoreAspectRatio,
            QtCore.Qt.SmoothTransformation)




        for i in range(self.x):
            p = QtCore.QPointF(Setting.WIDTH*i, 0)
            for j in range(self.y):
                item = self.addPixmap(pixmap)
                item.setPos(p)
                p += QtCore.QPointF(0, Setting.HEIGHT)


    def drawBackground(self, painter, rect):
        width = self.x * Setting.WIDTH
        height = self.y * Setting.HEIGHT

        l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))

        for _ in range(self.y+1):
            painter.drawLine(l)
            l.translate(0, Setting.HEIGHT)


        l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))

        for _ in range(self.x+1):
            painter.drawLine(l)
            l.translate(Setting.WIDTH, 0)

    def Add_columns(self):

            self.y = self.y + 1
            print ("hai")

            self.updateRect()
            print("hello")
            # self.a.drawBackground(painter,rect)
            print 'Columns value of Y is :',self.y



    def Add_rows(self):

            self.x = self.x + 1
            self.updateRect()
            # self.a.drawBackground(painter,rect)
            print 'Row value of  X is :', self.x
    def updateRect(self):
        self.setSceneRect(QtCore.QRectF(0, 0,self.x * Setting.WIDTH,self.y* Setting.HEIGHT))


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        scene = QS(parent=self)
        view = QtGui.QGraphicsView(scene)
        widget = QtGui.QWidget()
        self.setCentralWidget(widget)
        glayout1 = QtGui.QGridLayout(widget)
        addRowBtn = QtGui.QPushButton("Add")
        addRowBtn.setStyleSheet("QPushButton {background-color:red;border: none;color: white;width: 50px;padding: 15px;text-align: center;text-decoration: none;font-size: 16px;margin: 4px 2px;}")
        menu = QtGui.QMenu(self)
        menu.addAction('Add a column', scene.Add_columns)
        menu.addAction('Add a row', scene.Add_rows)
        addRowBtn.setMenu(menu)
        glayout1.addWidget(view, 0, 0)
        glayout1.addWidget(addRowBtn, 1, 1)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.showMaximized()
    sys.exit(app.exec_())

this is my image:[![enter image description here][1]][1]


  [![enter image description here][1]][1]
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
navya sri
  • 428
  • 5
  • 18
  • What have you tried? I see that you have added an empty function, have you tried to implement something? – eyllanesc Oct 20 '18 at 17:16
  • yes i tried,i got my output but the problem is when i click cmd prompt only it is executing – navya sri Oct 20 '18 at 17:23
  • what is `scrollArea`? – eyllanesc Oct 20 '18 at 17:34
  • This is sample code only, actually my code is above 1000 lines in that i am using scrollArea. but i think it is no need here – navya sri Oct 20 '18 at 17:38
  • Exactly, it is not necessary, but the duty to clean the unnecessary is yours :-), so you must provide a better [mcve] for future opportunities. – eyllanesc Oct 20 '18 at 17:40
  • thank you ...can i ask one thing – navya sri Oct 20 '18 at 17:41
  • i have csv file it consist of 4 values i want to create a cell of the grid by tacking unique x,y values from that csv file is it possible? because when i click any one of the cell in grid i want to show the x,y value of the grid, can you please guide me, i want to learn.... is there any method for that . – navya sri Oct 20 '18 at 17:45
  • Do you know how to read the csv file ?, if you do not know then learn it, there are thousands of solutions in the internet and SO. After loading them the normal thing is to create items, a while ago I answered a question with the same code as your samples where you added images one by one, the same logic you should use to do what you want. there is no direct method, the method is: upload files from python, get every x and y, create an item that shows the value of x and y and finally set it in the scene. – eyllanesc Oct 20 '18 at 17:49
  • yes i know by using pandas i done,ok thank you.. – navya sri Oct 20 '18 at 17:51
  • If your goal is just to show the pandas I have created a code to show data: https://stackoverflow.com/questions/44603119/how-to-display-a-pandas-data-frame-with-pyqt5/44605011#44605011 – eyllanesc Oct 20 '18 at 17:53
  • my goal is to show the unique x,y values in grid, when you click any cell of the grid it has to show like (x=3,y=0) – navya sri Oct 20 '18 at 17:56
  • i am very thankful to you because i gained a lot of things from your code – navya sri Oct 20 '18 at 17:59

1 Answers1

1

Avoid the use of global variables, these can be difficult to debug so use must be limited, in this case it is not necessary since it is only necessary to create attributes of the class. On the other hand the simplest solution is to call update or establish a new sceneRect that will call update internally and that the latter will force the repaint.

import sys
from PyQt4 import QtCore, QtGui


class Setting:
    WIDTH = 80
    HEIGHT = 80


class QS(QtGui.QGraphicsScene):
    def __init__(self, x=7, y=5, parent=None):
        super(QS, self).__init__(parent)
        self.x = x
        self.y = y
        self.updateRect()

    def updateRect(self):
        self.setSceneRect(QtCore.QRectF(0, 0, self.x * Setting.WIDTH, self.y * Setting.HEIGHT))

    def drawBackground(self, painter, rect):
        width = self.x * Setting.WIDTH
        height = self.y * Setting.HEIGHT

        l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))
        for _ in range(self.y+1):
            painter.drawLine(l)
            l.translate(0, Setting.HEIGHT)

        l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))
        for _ in range(self.x+1):
            painter.drawLine(l)
            l.translate(Setting.WIDTH, 0)

        pixmap = QtGui.QPixmap("p1.png").scaled(Setting.WIDTH, 
            Setting.HEIGHT, 
            QtCore.Qt.IgnoreAspectRatio,
            QtCore.Qt.SmoothTransformation)

        p = QtCore.QPointF()
        for i in range(self.x):
            p = QtCore.QPointF(Setting.WIDTH*i, 0)
            for j in range(self.y):
                painter.drawPixmap(p, pixmap)
                p += QtCore.QPointF(0, Setting.HEIGHT)

    def Add_columns(self):
        self.x += 1
        self.updateRect()

    def Add_rows(self):
        self.y += 1
        self.updateRect()


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        scene = QS(parent=self)
        view = QtGui.QGraphicsView(scene)
        widget = QtGui.QWidget()
        self.setCentralWidget(widget)
        glayout1 = QtGui.QGridLayout(widget)
        addRowBtn = QtGui.QPushButton("Add")
        addRowBtn.setStyleSheet("QPushButton {background-color:red;border: none;color: white;width: 50px;padding: 15px;text-align: center;text-decoration: none;font-size: 16px;margin: 4px 2px;}")
        menu = QtGui.QMenu(self)
        menu.addAction('Add a column', scene.Add_columns)
        menu.addAction('Add a row', scene.Add_rows)
        addRowBtn.setMenu(menu)
        glayout1.addWidget(view, 0, 0)
        glayout1.addWidget(addRowBtn, 1, 1)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.showMaximized()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • @navyasri Why have you unchecked my answer as correct? – eyllanesc Oct 22 '18 at 05:35
  • here my adding columns are added to bottom of the grid,but i want to add top of the grid how can i change the way of adding_columns – navya sri Oct 22 '18 at 06:28
  • i changed the y positions and main drawBackground setting but still i am getting the my columns are added to bottom of the grid only – navya sri Oct 22 '18 at 06:36
  • @navyasri Why do you think they are added down or to the right? What is your reference system? And if it is added to the left or above as you see it ?, in the case of my solution I increase in 1 the number of rows, that is: [0, r] -> [0, r + 1] in the for loop, but you should do [0, r] -> [-1, r], then -> [- 2, r] and so on. – eyllanesc Oct 22 '18 at 06:39