python - 如何在 pyqt4 的网格顶部添加列
问题描述
这是我的程序,因为我有一个网格单元格,当我单击添加行和列按钮时,我想为该网格添加空列和行,当我单击添加列按钮时,我想在网格的顶部不是网格的底部。但我只得到网格的底部,我怎样才能改变在网格顶部添加列的方式。提前谢谢你。这是我的代码:
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]
解决方案
避免使用全局变量,这些可能难以调试,因此必须限制使用,在这种情况下没有必要,因为只需要创建类的属性。另一方面,最简单的解决方案是调用 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_())
推荐阅读
- java - 无法在 feign builder 中引发期望
- php - 在 PHP 中从 SFTP 获取最新文件(使用 CURL)
- salt-stack - 状态应用期间的 Saltstack 发送事件
- c - 将地址或指针传递给 C 函数
- facebook - Facebook - 如何从 Instant Games 中删除加载屏幕
- php - 如何在我的服务中使用学说方法(Symfony 4)?
- javascript - 在服务器调用中获取部分 redux 状态
- node.js - Node.js - 使用 MSSQL 包动态添加多个参数
- javascript - 打印多个 iframe 内容
- php - 在表单上使用 Guzzle 登录 - 在 test.de 上发布请求