Python Forum
[PyQt] PyQt trigger leaveEvent in parent
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] PyQt trigger leaveEvent in parent
#4
Okay first you will need to upgrade to Python3 as Python2.7 will be deprecated in January of next year either move now or be forced to move later.

Next since you have the full version above I am just going to dissect the pieces I changed if I miss something just let me know
layout = QVBoxLayout(widget)
Yes while you can write the above in that manner it actually goes back to the old school process of cramming as much as one could on a single line because you had to which btw turned out to be a very bad idea once you could do it without issue in a more verbose easier to comprehend at a glance method. So why go back to the way it had to be done that proved to be a bad coding style once it was no longer required. I mean what are you saving? One additional line of code for more complexity? Big rule and best rule of programming is K.I.S.S. (Keep It Simple and Smart) which means you always ask yourself do I need to add more complexity or is the simple approach just as effective -- further what is more clear about what I am doing this method or that method. The following 2 liner of the above 1 liner is both less complex and more straight forward as in its obvious what you are doing.
vbxDescShrtNam = QVBoxLayout()
vbxDescShrtNam.addWidget(MyWidget)

from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout
from PyQt5.QtWidgets import QFrame, QLabel
Next when importing libraries ONLY import what you need the from PyQt import QtWidgets imports a HUGE library that you most likely are only going to use a fraction of further importing QDialog (for example) imports all its dependencies anyway
class CustomWidget(QFrame):
    def __init__(self, Name):
        QFrame.__init__(self)
Next do not use the Super( ) function unless you are using it for the rare and specific case it was designed for because using it for any other purpose brings in various other potential issues and you are thus adding more potential issues than you are solving when you use it. Again K.I.S.S. do not add more complexity unless its necessary and using Super() does just that adds extra unnecessary complexity
        self.WdgtName = Name
I did this instead of setting the .ObjectName() because calling that method to set a parent variable is slightly more complex when all you need it for is to handle a local class variable
        VBox = QVBoxLayout()
        VBox.addWidget(self.lblName)
Again the above is cleaner less complex than cramming all within one line of code further what if you want to and multiple Widgets to that VBox?

Another element of my changes was to group like sections of code together rather sprinkle them throughout as this facilitates being able to see it all at a glance. Also arrange them top-down in order of usage for instance you need to define and set all the widgets that you plan to put into a BoxLayout prior to even creating that BoxLayout so do those first then do the BoxLayout adding each of its pre-defined Widgets/Layouts
if __name__ == '__main__':
    MainEventThread = QApplication([])

    MainApp = TestWindow()
    MainApp.show()

    MainEventThread.exec()
Okay this is a sort of packaged deal -- first QApplication([]) is what creates the Main Event Thread in Qt -- so name it as such to remind yourself and to let anyone else, at glance, know what its purpose is. Next you do not use sys.argv in the code so do not include it as that is adding unused code to your project -- further if you were going to use Command Line arguments then do not use sys.argv anyway as the Python argparser library handles it much cleaner and already exists

Next MainEventThread.exec() is the PyQt5 version of this line using what you did was the old PyQt4 way of doing it and is only supported for backwards compatibility which means eventually it will not be supported so do not add it to new code unless you absolutely need it which imho is going to be extremely unlikely

Lastly these 5 lines of code are always a copy/paste/minor-tweak for me with every program I write using Python-PyQt and the only thing I ever tweak (change) is the class name being created (aka in this case TestWindow())
Reply


Messages In This Thread
PyQt trigger leaveEvent in parent - by kainev - Dec-01-2019, 08:15 AM
RE: PyQt trigger leaveEvent in parent - by Denni - Dec-02-2019, 07:18 PM
RE: PyQt trigger leaveEvent in parent - by kainev - Dec-03-2019, 01:27 AM
RE: PyQt trigger leaveEvent in parent - by Denni - Dec-03-2019, 03:53 PM
RE: PyQt trigger leaveEvent in parent - by Denni - Dec-03-2019, 04:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] What causes this pop-up to trigger? Oshadha 2 1,797 Dec-17-2020, 08:37 AM
Last Post: Oshadha
  Ho can I get the first parent of a class? panoss 2 3,627 Jan-10-2017, 08:10 AM
Last Post: panoss

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020