【问题标题】:How can i add the columns in top of the grid in pyqt4如何在 pyqt4 的网格顶部添加列
【发布时间】:2019-03-25 06:37:41
【问题描述】:

这是我的程序,因为我有一个网格单元格,当我单击添加行和列按钮时,我想为该网格添加空列和行,当我单击添加列按钮时,我想添加 no网格顶部的列而不是网格的底部。但我只得到网格的底部,我怎样才能改变在网格顶部添加列的方式。提前谢谢你。 这是我的代码:

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]

【问题讨论】:

  • 你试过什么?我看到你添加了一个空函数,你有没有尝试实现一些东西?
  • 是的,我试过了,我得到了我的输出,但问题是当我点击 cmd 提示符时,只有它正在执行
  • 什么是scrollArea
  • 这只是示例代码,实际上我的代码超过 1000 行,因为我使用的是 scrollArea。但我认为这里没有必要
  • 没错,没有必要,但是清理不必要的东西是你的责任:-),所以你必须提供更好的minimal reproducible example 以备将来的机会。

标签: python pyqt4


【解决方案1】:

避免使用全局变量,这些可能难以调试,因此必须限制使用,在这种情况下没有必要,因为只需要创建类的属性。另一方面,最简单的解决方案是调用 update 或建立一个新的 sceneRect,它会在内部调用 update 并且后者将强制重绘。

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_())

【讨论】:

  • @navyasri 为什么你没有勾选我的答案是正确的?
  • 这里我添加的列被添加到网格的底部,但我想添加网格的顶部如何更改添加列的方式
  • 我更改了 y 位置和主要 drawBackground 设置,但我仍然将我的列添加到网格底部
  • @navyasri 为什么你认为它们被向下或向右添加?你的参考系统是什么?如果它被添加到您所看到的左侧或上方?,在我的解决方案的情况下,我将行数增加 1,即: [0, r] -> [0, r + 1] 在for 循环,但你应该做 [0, r] -> [-1, r],然后 -> [- 2, r] 等等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2018-01-20
  • 2019-07-23
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多