简体   繁体   中英

Subclass & override PySide2 widget method; where do I find source to reference?

When inheriting a class and overriding a method you don't always know what's inside the original class. And if I understand correctly, various aspects of how this is handled in Python are intentionally designed to accommodate this. But I have to think that all or most of the Qt code is documented somewhere. I've found the Qt for Python documentation to be refreshing (compared to PyQt) and lists all methods for a class with a fair amount of detail but I've not been able to find the actual code for specific methods and descriptions are not always very complete. For instance, consider if I want to override the mousePressEvent for a combobox.

class mycombo(QtWidgets.QComboBox):
    def __init__(self, parent = None):
        super(mycombo, self).__init__()
        self.setAcceptDrops(True)
        self.setFocusPolicy(QtCore.Qt.NoFocus)

    def mousePressEvent(self,event):

In the Qt for Python documentation I see that QComboBox inherits from QWidget which has this to say about mousePressEvent:

https://doc.qt.io/qtforpython/PySide2/QtWidgets/QWidget.html#PySide2.QtWidgets.PySide2.QtWidgets.QWidget.mousePressEvent

There are some useful things written there but, how do I determine what's actually happening in the 'original' mousePressEvent? I don't want to interfere with other parts of the operation that are actually desired. In this case, perhaps the handling of popup widgets like the description mentions, is desired. But I also might not want to super the original method. Is there more documentation for this that I've somehow missed? It seems that some of the widgets are subclassed and modified in Python and others are only in C++?

TL; DR; There is no documentation or need as the source code can change without notifying developers.


Actually you should not know what each method does since that can change in a new version. If your goal is to add functionalities then do not override the implementation of the parent class by calling it to super().foo_method() , so you can add logic before or after the original implementation

class MyCombo(QtWidgets.QComboBox):
    def __init__(self, parent = None):
        super(MyCombo, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setFocusPolicy(QtCore.Qt.NoFocus)

    def mousePressEvent(self,event):
        # stuff
        super(MyCombo, self).mousePressEvent(event)
        # another stuff

If you still want to know what happens in that function then you should check the source code :

void QComboBox::mousePressEvent(QMouseEvent *e)
{
    Q_D(QComboBox);
    if (!QGuiApplication::styleHints()->setFocusOnTouchRelease())
        d->showPopupFromMouseEvent(e);
}

Qt has 2 types of API since it implements PIMPL/D-Pointer so analyzing the source code of the class will not be enough if you want to understand the logic, the public API rarely changes for what it is, but instead the private API has changes continuously.

In conclusion, if you want to add functionalities then just analyze the documentation as this is respected even when the code changes. Another way of understanding how it works is by modifying and analyzing the changes that occur in the GUI.

What PyQt/Qt For Python does is a wrapper to handle C++ classes, so in general what can be done in C++ can be done in Python except for some minor things that can be done in one and not in another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM