【发布时间】:2021-03-02 13:13:51
【问题描述】:
我需要能够在另一个小部件上绘制一个圆/线,但每次我尝试时,它都会落后。我已经阅读了很多关于在小部件上使用 QPainter 的帖子,但我仍然无法让它工作。
以下是我的应用程序的一个最小示例,我只是想弄清楚在哪里放置一个paintevent函数以使其正常工作。
我的最终目标是让用户画出这样的热数独 - thermo
但我认为,如果我能弄清楚如何在我的 SudokuGrid 上绘制任何东西,我就可以解决剩下的问题
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
class SudokuCell(QLineEdit):
def __init__(self, cell_size, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cell_size = cell_size
font = self.font()
font.setPointSize(24)
self.setFont(font)
self.setAlignment(Qt.AlignCenter)
self.setFixedSize(cell_size, cell_size)
self.setAutoFillBackground(True)
class SudokuRegion(QWidget):
def __init__(self, cell_size, narrow_line_width, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cell_size = cell_size
self.narrow_line_width = narrow_line_width
layout = QGridLayout()
layout.setSpacing(narrow_line_width)
layout.setContentsMargins(0,0,0,0)
self.setLayout(layout)
for i in range(3):
for j in range(3):
new_cell = SudokuCell(cell_size, objectName=f"C{i}{j}")
layout.addWidget(new_cell, i, j)
class SudokuGrid(QWidget):
def __init__(self, cell_size, wide_line_width, narrow_line_width, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cell_size = cell_size
self.wide_line_width = wide_line_width
self.narrow_line_width = narrow_line_width
layout = QGridLayout()
layout.setContentsMargins(wide_line_width,wide_line_width, 0, 0)
layout.setSpacing(wide_line_width)
self.setLayout(layout)
for i in range(3):
for j in range(3):
new_region = SudokuRegion(cell_size, narrow_line_width, objectName=f"Region{i * 3 + j}")
layout.addWidget(new_region, i, j)
class MainWindow(QMainWindow):
def __init__(self, window_w, window_h, orthogonal_intersection_size, cell_size, wide_line_width, narrow_line_width):
super().__init__()
self.window_w = window_w
self.window_h = window_h
self.orthogonal_intersection_size = orthogonal_intersection_size
self.cell_size = cell_size
self.wide_line_width = wide_line_width
self.narrow_line_width = narrow_line_width
# a sudoku grid is exactly this large. Google a sudoku grid if you dont understand
self.frame_size = 9 * cell_size + 4 * wide_line_width + 6 * narrow_line_width
self.initUi()
def initUi(self):
self.setWindowTitle("Sudoku Solver")
self.setGeometry(100, 100, self.window_w, self.window_h)
widget = QWidget(self)
self.setCentralWidget(widget)
hor_box = QHBoxLayout()
widget.setLayout(hor_box)
self.frame = QFrame(widget)
self.frame.setFixedSize(self.frame_size, self.frame_size)
self.frame.setStyleSheet(".QFrame {background-color: black}")
self.grid = SudokuGrid(self.cell_size, self.wide_line_width, self.narrow_line_width, self.frame)
hor_box.addWidget(self.frame)
# other widgets are added to the hor_box later but arent important for this question
self.show()
def main():
app = QApplication(sys.argv)
window = MainWindow(
window_w=1000,
window_h=750,
orthogonal_intersection_size=25,
cell_size=80,
wide_line_width=8,
narrow_line_width=2
)
window.show()
app.exec_()
if __name__ == "__main__":
main()
【问题讨论】: