PushButton的巨大“网格”滞后,无法启用按钮

时间:2013-08-20 05:30:47

标签: c++ qt grid lag qpushbutton

在我的项目中,我在一个16x16网格布局中有256个微小的PushButton。 (是的,这需要永远。)现在编辑和运行我的程序是非常滞后的。另外,由于一些奇怪的原因,Qt不会让我启用任何按钮,但是旁边的其他按钮工作得很好吗?

有没有简单的方法可以确定在没有一堆按钮的情况下点击了哪个方格的网格? (就像将光标跟在图像上一样?)

此外,当点击网格的每个“正方形”时,它变为“选择”,并且它必须是所选择的唯一“正方形”。 (想想它就像一个巨大的棋盘)

这是一张照片:http://gyazo.com/988cdbb59b3d1f1873c41bf91b1408fd

稍后,我需要再次为54x54尺寸的网格(2916个按钮)执行此操作,我真的不想按钮一直按这样做。

感谢您的时间,我希望您理解我的问题:)

3 个答案:

答案 0 :(得分:1)

你可以这么简单的方式,我已经在代码中解释了几乎所有内容,但是如果你有任何问题可以随意提出,请接受这个答案,如果它解决了你的问题:)

import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import * 

class DrawImage(QMainWindow): 
    def __init__(self, parent=None):
        super(QMainWindow, self).__init__(parent)
        self.setWindowTitle('Select Window')

        #you can set grid size here ... 8x8, 16x16 , for bigger numbers (54x54) be sure your image is big enough, because QWidget can't be smaller then ~20 pixels
        self.gridSize = 16

        mainWidget = QWidget()
        self.setCentralWidget(mainWidget)
        self.scene = QGraphicsScene()
        view = QGraphicsView(self.scene)
        layout = QVBoxLayout()
        layout.addWidget(view)
        mainWidget.setLayout(layout)
        self.image = QImage('image.JPG')# put your image name here, image (suppose to be grid) must be at the same folder or put full path
        pixmapItem = QGraphicsPixmapItem(QPixmap(self.image), None, self.scene)
        pixmapItem.mousePressEvent = self.pixelSelect


    def pixelSelect( self, event ):
        #add whatever you want to this widget,any functionality or you can add image for example, I've simply colored it
        wdg = QWidget()
        layout = QVBoxLayout()
        palette =  QPalette(wdg.palette())
        palette.setBrush(QPalette.Background, QColor(200,255,255))
        wdg.setPalette(palette)
        wdg.setLayout(layout)
        self.scene.addWidget(wdg)

        #calculate size and position for added widget
        imageSize = self.image.size()
        width = imageSize.width()
        height = imageSize.height() 

        #size
        wgWidth = float(width)/self.gridSize  
        wgHeight =  float(height)/self.gridSize
        wdg.setFixedSize(wgWidth,wgHeight)

        #position       
        wgXpos = int(event.pos().x()/wgWidth) * wgWidth
        wgYpos = int(event.pos().y()/wgHeight) * wgHeight
        wdg.move(wgXpos,  wgYpos)

        #which square is clicked?
        print "square at row ", int(event.pos().y()/wgHeight)+1,", column ",int(event.pos().x()/wgWidth)+1, "is clicked"

def main():
    app = QtGui.QApplication(sys.argv)
    form = DrawImage()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

此外,如果您想在网格图像上显示简单的方形图像,请查看我的问题/解决方案:QGraphicsPixmapItem won't show over the other QGraphicsPixmapItem

答案 1 :(得分:1)

如果你真的不需要按钮的外观,我会创建一个QWidget子类,它实现一个自定义的paintEvent并呈现一个所需大小的网格(考虑到小部件的大小)。然后,在鼠标事件(向上,向下,移动等)中,您可以使用简单的公式计算单击了哪个网格项。您可以使用不同颜色渲染单元格以指示选择或突出显示。

P.S。:我真的想从我的实现中发布一些代码(我已经完成了两到三次),但是源代码在我的旧公司:)

答案 2 :(得分:0)

您只需创建自己的QGridLayout即可轻松添加按钮。 我发布了一个answer to another question,向您展示如何按顺序用一堆小部件填充自定义的QGridLayout。根据您指定的最大列数添加按钮。 (注意:这只是一个非常粗略的例子,但足以从中开始)

在您的示例中,您将创建包含16列的自定义网格布局,只需添加按钮。

要找出按下了哪个按钮(并使连接更容易),您可以使用QSignalMapper

要调查延迟,您可以检查应用程序的(GDI- / User-)句柄数量(例如,使用ProcessExplorer)。句柄数不应超过10.000。

我不知道为什么你不能启用按钮。