在PyQt5和PySide2中覆盖paintEvent

时间:2018-10-30 20:01:46

标签: python pyqt pyqt5 pyside2

我一直在使用PyQt和PySide。今天,我偶然发现了一个奇怪的行为:重新实现paintEvent似乎在Qt5的Python版本中不起作用。我在Qt4中从未遇到过这个问题。

from tkinter import *
from tkinter import ttk

class Application(Frame):

    def widgets(self):

        self.tabcontrol = ttk.Notebook(self)

        self.tab1 = ttk.Frame(self)
        self.tabcontrol.add(self.tab1, text = "Tab-1")
        self.tabcontrol.pack(expand  =1, fill = "both")

        self.frame_canvas = Frame(self.tab1)
        self.frame_canvas.grid(row=0, column=0, sticky=N+S+E+W)
        self.frame_canvas.grid_rowconfigure(0, weight=1)
        self.frame_canvas.grid_columnconfigure(0, weight=1)

        self.canvas = Canvas(self.frame_canvas)
        self.canvas.grid(row=0, column=0, sticky=N+S+E+W)

        self.scroll = Scrollbar(self.frame_canvas, orient="horizontal", command=self.canvas.xview)
        self.scroll.grid(row=3, column=0, sticky=N+S+E+W)
        self.canvas.configure(xscrollcommand=self.scroll.set)        

        self.frame_widgets = Frame(self.canvas)
        self.canvas.create_window((0, 0), window=self.frame_widgets)

        self.photo = PhotoImage(file = "Test.PNG")
        self.title = Label(self.frame_widgets, image = self.photo)
        self.title.grid(row= 0, column=0, columnspan = 15, rowspan =1)

        self.L1  = Label(self.frame_widgets, text = "A", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =0)
        self.L2  = Label(self.frame_widgets, text = "B", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =1)
        self.L3  = Label(self.frame_widgets, text = "C", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =2)
        self.L4  = Label(self.frame_widgets, text = "D", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =3)
        self.L5  = Label(self.frame_widgets, text = "E", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =4)
        self.L6  = Label(self.frame_widgets, text = "F", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =5)
        self.L7  = Label(self.frame_widgets, text = "G", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =6)
        self.L8  = Label(self.frame_widgets, text = "H", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =7)
        self.L9  = Label(self.frame_widgets, text = "I", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =8)
        self.L10 = Label(self.frame_widgets, text = "J", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =9)
        self.L11 = Label(self.frame_widgets, text = "K", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =10)
        self.L12 = Label(self.frame_widgets, text = "L", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =11)
        self.L13 = Label(self.frame_widgets, text = "M", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =12)
        self.L14 = Label(self.frame_widgets, text = "N", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =13)
        self.L15 = Label(self.frame_widgets, text = "O", fg = "white", bg = "steel blue", font = "Helvetica 10 bold").grid(row=1, column =14)

        self.frame_widgets.update_idletasks()
        self.canvas.config(width=1366, height=600, scrollregion=self.canvas.bbox("all"))             

    def __init__(self, initial):
        super(Application, self).__init__(initial)
        self.grid()
        self.widgets()

Window = Tk()
Tool = Application(Window)
Window.mainloop()

因此,我们只是在测试是否调用paintEvent(通过在调用它时引发AssertionError)。

一旦我们将另一个小部件添加到TagWidget所在的相同布局中,paintEvent就不再有效。

太奇怪了。帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

paintEvent()在需要重绘时调用,如果窗口小部件具有size(0, 0)或大小无效或被隐藏,则不调用该方法,这就是您使用时发生的情况一种布局,默认情况下其大小为sizeHint(),默认情况下QWidget sizeHint()QSize(-1, -1),因此无需绘制。

因此解决方案是设置适当的sizeHint()

class TagWidget(QtWidgets.QWidget):
    def paintEvent(self, e): 
        print("paintEvent")
        raise(AssertionError)

    def sizeHint(self):
        print("default sizeHint: ", super(TagWidget, self).sizeHint())
        return QtCore.QSize(640, 480)

我已经用PyQt4PySide进行了尝试,并且发生了相同的问题,所以问题不是Qt,而是示例。