这是我的程序,因为我有一个网格单元格,当我单击“添加行和列”按钮时,我想为该网格添加空列和行,当我单击“添加列”按钮时,我想不添加网格顶部的列而不是网格底部。但是我只能在网格底部找到如何更改在网格顶部添加列的方式。在此先感谢您。 这是我的代码:
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]
答案 0 :(得分: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_())