仅从QWidget捕获鼠标位置,而不从QMainWindow PyQT5捕获鼠标位置

时间:2018-03-03 06:37:07

标签: python python-3.x pyqt5

我目前正在开发一个使用PyQt5的应用程序,我需要从指针中获取坐标。

我正在使用带有StatusBar和菜单的QMainWindow以及我的中央小部件作为QWidget,我在其中绘制背景。

我的问题是,菜单正在从窗口顶部添加23个像素,这会在我获得鼠标位置时造成问题。有没有办法让我忽略不是来自绘图的所有其他鼠标位置?我喜欢它所以它从图纸的左上角开始0,0并在右下角结束,

我附上了一个问题的图像。鼠标位于同一位置(左上角的第一个方块),但在将其转换为坐标后,它显示的是另一个位置。

问题本身

我不确定自己是否清楚明了,所以最后除了QWidget之外,我怎么能忽略鼠标的位置?

修改

以下是代码:

project.py

import sys
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow, QMenu, qApp, QDialog, QPushButton, QFileDialog, QInputDialog, QAction
from PyQt5.QtGui import QPainter, QColor, QFont, QIcon
from PyQt5.QtCore import Qt, QPointF

from Punto import Punto

coordinates = []
mask = []
initial = []
final = []

class Settings():

    WIDTH = 25
    HEIGHT = 25
    NUM_BLOCKS_X = 15
    NUM_BLOCKS_Y = 15

    type = ["Wall", "Land", "Water", "Sand", "Forest", "Swamp", "Snow"]
    flags = Qt.WindowFlags(QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint)

class Main(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.buildMenu()
        self.setWindowTitle("Labyrinth")
        self.setWindowIcon(QtGui.QIcon('icon.png'))

        self.openFile()

        self.grid = Grid()
        self.setCentralWidget(self.grid)

        self.setMouseTracking(True)
        self.centralWidget().setMouseTracking(True)

        self.resize((Settings.WIDTH*Settings.NUM_BLOCKS_X)+1, (Settings.HEIGHT*Settings.NUM_BLOCKS_Y)+45)
        self.setFixedSize(self.size())

    def buildMenu(self):

        exitAct = QAction('&Exit', self)        
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(qApp.quit)

        openAct = QAction('&Open', self)        
        openAct.setShortcut('Ctrl+O')
        openAct.setStatusTip('Open a new file')
        openAct.triggered.connect(self.openFile)

        setInitial = QAction('&Initial point', self)
        setInitial.setShortcut('Ctrl+I')
        setInitial.setStatusTip("Set the initial point in the labyrinth")
        setInitial.triggered.connect(self.setInitial)

        setFinal = QAction('&Final point', self)
        setFinal.setShortcut('Ctrl+F')
        setFinal.setStatusTip("Set the final point in the labyrinth")
        setFinal.triggered.connect(self.setFinal)

        menubar = self.menuBar()

        fileMenu = menubar.addMenu('&File')
        setMenu = menubar.addMenu('&Set...')

        fileMenu.addAction(openAct)
        fileMenu.addAction(exitAct)
        setMenu.addAction(setInitial)
        setMenu.addAction(setFinal)

    def setInitial(self, coords):
        global initial
        initial = coords
        self.centralWidget().update()

    def setFinal(self, coords):
        global final
        final = coords
        self.centralWidget().update()

    def openFile(self):
        filename = "laberinto.txt"
        archivo = open(filename, "r")

        global coordinates

        puntos = []
        for linea in archivo:
            for x in (linea.strip().split(",")):
                puntos.append(Punto(x))
            coordinates.append(list(puntos))
            puntos.clear()

        coordinates = list(zip(*coordinates))


        Settings.NUM_BLOCKS_X = len(coordinates)
        Settings.NUM_BLOCKS_Y = len(coordinates[0])

        temp = [0] * Settings.NUM_BLOCKS_Y

        for x in coordinates:
            mask.append([1] * len(coordinates))


    def mouseMoveEvent(self, e):    
        x = e.x()
        y = e.y()

        self.coordX = (x-(x%Settings.WIDTH))/Settings.WIDTH
        self.coordY = (y-(y%Settings.HEIGHT))/Settings.HEIGHT

        if self.coordX == Settings.NUM_BLOCKS_X:
            self.coordX = Settings.NUM_BLOCKS_X-1

        if self.coordY == Settings.NUM_BLOCKS_Y:
            self.coordY = Settings.NUM_BLOCKS_Y-1

        text = "[{0}, {1}] - {2}".format(int(self.coordX), int(self.coordY), 
            Settings.type[coordinates[int(self.coordX)][int(self.coordY)].type])
        self.statusBar().showMessage(text)


class Grid(QWidget):
    def __init__(self):
        super().__init__()

    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        self.drawBackground(event, painter)
        painter.end()

    def getColor(self, x, y):
            type = int(coordinates[x][y].type)
            if int(type) == 0:
                return QColor(128, 128, 128)
            if int(type) == 1:
                return QColor(255, 153, 51)
            if int(type) == 2:
                return QColor(51, 153, 255)
            if int(type) == 3:
                 return QColor(255, 255, 102)
            if int(type) == 4:
                return QColor(0, 153, 0)
            if int(type) == 5:
                return QColor(25, 0, 51)
            if int(type) == 6:
                return QColor(255, 204, 204)

    def drawBackground(self, event, painter):

        painter.setPen(Qt.black)
        for x in range (0, Settings.NUM_BLOCKS_X+1):
            painter.drawLine(0, x*Settings.WIDTH, Settings.WIDTH*Settings.NUM_BLOCKS_X, x*Settings.WIDTH)
        for x in range (0, Settings.NUM_BLOCKS_Y+1):
            painter.drawLine(x*Settings.HEIGHT, 0, x*Settings.HEIGHT, Settings.HEIGHT*Settings.NUM_BLOCKS_Y)

        for x in range (0, Settings.NUM_BLOCKS_X):
            for y in range (0, Settings.NUM_BLOCKS_Y):
                if mask[x][y] == 1:
                    painter.setBrush(self.getColor(x, y))
                    painter.drawRect((x*Settings.HEIGHT),(y*Settings.WIDTH), Settings.WIDTH, Settings.HEIGHT)
                    if coordinates[x][y].visited :
                        margin = [int(Settings.HEIGHT*3/4), int(Settings.WIDTH*3/4)]
                        painter.setBrush(QColor(255, 255, 255))
                        painter.drawRect(
                            (x*Settings.HEIGHT)+int(margin[0]/2),
                            (y*Settings.WIDTH)+int(margin[0]/2),
                            Settings.WIDTH-margin[1],
                            Settings.HEIGHT-margin[1])
        if initial:
            painter.drawText(QPointF(10+initial[0]*Settings.WIDTH, 20+initial[1]*Settings.HEIGHT), "I")
        if final:
            painter.drawText(QPointF(10+final[0]*Settings.WIDTH, 20+final[1]*Settings.HEIGHT), "F")

if __name__ == '__main__':

    app = QApplication(sys.argv)

    program = Main()
    program.show()
    sys.exit(app.exec_())

Punto.py

class Punto:
    def __init__(self, type):
        self.type = int(type)
        self.visited = 0

    def visit(self):
        if self.visited==0:
            self.visited = 1
            return 1

程序正在打开的文件(laberinto.txt):

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,1,1,1,0,1,1,1,1,1,1,1,1,1
0,1,0,1,0,0,1,0,1,0,0,0,0,0,0
0,0,1,1,1,1,1,0,1,0,0,0,0,0,0
0,0,0,1,0,1,0,0,1,0,0,0,0,0,0
0,0,0,1,0,0,0,0,1,0,0,0,0,0,0
0,1,1,1,1,1,1,0,1,0,0,0,0,0,0
0,1,0,1,0,0,0,0,0,0,0,0,0,0,0
0,1,0,1,1,1,1,1,1,0,0,0,0,0,0
1,1,0,1,0,0,0,1,0,0,0,0,0,0,0
0,0,0,1,0,0,0,1,0,0,0,0,0,0,0
0,0,0,1,0,0,0,1,0,0,0,0,0,0,0
0,0,0,1,0,0,0,1,0,0,0,0,0,0,0
0,0,0,1,0,0,0,1,0,0,0,0,0,0,0
0,0,0,1,0,0,0,1,0,0,0,0,0,0,0

谢谢!

0 个答案:

没有答案