0

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

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

此外,当单击网格的每个“正方形”时,它就变成了“选择”,并且它必须是唯一被选中的“正方形”。(把它想象成一个巨大的棋盘)

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

稍后,我将需要为 54x54 大小的网格(2916 个按钮)再次执行此操作,我真的不想一个接一个地执行此操作。

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

4

3 回答 3

1

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

PS:我真的很想从我的实现中发布一些代码(我已经这样做了两三次)但是源代码在我的老公司:)

于 2013-09-02T08:24:54.737 回答
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

于 2013-08-29T11:05:16.540 回答
0

您只需创建自己的 QGridLayout 以便能够轻松添加按钮。我发布了另一个问题的答案,向您展示了如何用一堆小部件顺序填充定制的 QGridLayout。根据您指定的最大列数添加按钮。(注意:这只是一个非常粗略的例子,但足以开始)

在您的示例中,您将创建具有 16 列的自定义网格布局并简单地添加您的按钮。

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

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

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

于 2013-08-20T05:56:26.023 回答