|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如图所示,我的QPainter没有工作在副QWidget窗口,工作在主窗口上面,导致被遮挡,请问应该如何解决这个问题,让QPainter工作与顶部不被遮挡,一下是我的代码demo
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5.QtGui import QPainter, QPen, QColor, QRadialGradient, QBrush
from PyQt5.QtCore import Qt, QPoint
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(250, 70, 301, 261))
self.widget.setObjectName("widget")
self.widget.setStyleSheet("background-color:green;")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class Gomoku(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.setupUi(self)
#self.paintEvent = self.widget.paintEvent
def paintEvent(self, e):
def draw_map():
"""绘制棋盘"""
qp.setPen(QPen(QColor(0, 0, 0), 2, Qt.SolidLine)) # 棋盘的颜色为黑色
# 绘制横线
for x in range(15):
qp.drawLine(40 * (x + 1), 40, 40 * (x + 1), 600)
# 绘制竖线
for y in range(15):
qp.drawLine(40, 40 * (y + 1), 600, 40 * (y + 1))
# 绘制棋盘中的黑点
qp.setBrush(QColor(0, 0, 0))
key_points = [(4, 4), (12, 4), (4, 12), (12, 12), (8, 8)]
for t in key_points:
qp.drawEllipse(QPoint(40 * t[0], 40 * t[1]), 5, 5)
qp = QPainter()
qp.begin(self)
draw_map() # 绘制棋盘
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
game = Gomoku()
game.show()
sys.exit(app.exec_())
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5.QtGui import QPainter, QPen, QColor, QRadialGradient, QBrush
from PyQt5.QtCore import Qt, QPoint
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
class PWidget(QtWidgets.QWidget):
def __init__(self, parent=None, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
def paintEvent(self, e):
def draw_map():
"""绘制棋盘"""
qp.drawRect(self.rect())
qp.setBackground(QColor("green"))
qp.setPen(QPen(QColor(0, 0, 0), 2, Qt.SolidLine)) # 棋盘的颜色为黑色
# 绘制横线
for x in range(15):
qp.drawLine(40 * (x + 1), 40, 40 * (x + 1), 600)
# 绘制竖线
for y in range(15):
qp.drawLine(40, 40 * (y + 1), 600, 40 * (y + 1))
# 绘制棋盘中的黑点
qp.setBrush(QColor(0, 0, 0))
key_points = [(4, 4), (12, 4), (4, 12), (12, 12), (8, 8)]
for t in key_points:
qp.drawEllipse(QPoint(40 * t[0], 40 * t[1]), 5, 5)
qp = QPainter()
qp.begin(self)
qp.fillRect(self.rect(), QColor("green")) # 用此绘制背景
draw_map() # 绘制棋盘
class Gomoku(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.setupUi(self)
# self.paintEvent = self.widget.paintEvent
self.setwidget() # 调用
def setwidget(self):
# 具体的绘制区域有待优化
# 最好self.centralwidget添加一个布局,此布局添加self.widget
self.widget = PWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(250, 70, 301, 261))
# self.widget.setStyleSheet("background-color:green;")
if __name__ == "__main__":
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
game = Gomoku()
game.show()
sys.exit(app.exec_())
|
|