【发布时间】: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 以备将来的机会。