당신이 쉽게 할 수있는, 나는 코드의 거의 모든 것을 설명했지만, 당신은 질문이있는 경우는 물어 주시기 바랍니다에 대해, 그것이 문제 :
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
는 관련 코드를 보여주세요 내 된 회사 :)에있다. 또한 QGraphicsView는 경우에 따라 더 적합 할 수 있습니다. – Mat
관련 코드가 없습니다. 그냥 내 질문을 읽으십시오. 코딩 문제가 아닙니다. – mrg95
"끔찍하게 뒤지지 않는"물건이 있고 이미지 만 제공합니다. 우리가 그걸 어떻게 도울 수 있니?"버튼 하나로 버튼을 사용하고 싶지 않습니다."기존 코드의 구조에 의문을 제기합니다. "쉬운 방법이 있습니까 [...]?" 맞습니다. 수학은 매우 간단하지만 올바른 솔루션이 아닙니다. 해결책은 지연이 발생하는 모든 문제를 해결하는 것입니다 (또는 QGraphicsView _could_는 더 좋을 수는 있지만 당신이 무엇인지에 대한 세부 사항을 알지 못하고 말하지는 않습니다). 현재하고 있습니다.) – Mat