Got error with QSplitterHandle

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Got error with QSplitterHandle

Gottfried Müller
I wanted to implement the qt example
https://doc.qt.io/qt-5/qsplitterhandle.html with PyQt5. And I've got an
error with my implementation:

TypeError: QSplitterHandle(Qt.Orientation, QSplitter): argument 1 has
unexpected type 'MySplitter'

Can anybody help? Here is the code of my implementation:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring

import sys

from PyQt5.QtWidgets import (
     QApplication, QWidget, QListView, QSplitter, QVBoxLayout,
QSplitterHandle
)
from PyQt5.QtCore import Qt


class MySplitterHandle(QSplitterHandle):

     def __init__(self, orientation, parent=None):
         super().__init__(parent)
         self.orientation(orientation)

     def paintEvent(self, event):
         print("do something")


class MySplitter(QSplitter):

     def __init__(self, orientation, parent=None):
         super().__init__(parent)
         self.setOrientation(orientation)

     def createHandle(self, orientation):
         return MySplitterHandle(orientation, self)


class ApplWindow(QWidget):

     def __init__(self, parent=None):
         super().__init__(parent)
         v01 = QListView(parent=self)
         v02 = QListView(parent=self)
         orientation = Qt.Vertical
         splitter = MySplitter(orientation, parent=self)
         splitter.createHandle(orientation)
         layout = QVBoxLayout()
         layout.addWidget(v01)
         layout.addWidget(splitter)
         layout.addWidget(v02)
         self.setLayout(layout)
         self.resize(400, 400)


def main():
     appl = QApplication(sys.argv)
     applWindow = ApplWindow()
     applWindow.show()
     return appl.exec_()


if __name__ == "__main__":
     main()

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Got error with QSplitterHandle

Maurizio Berti
Il giorno sab 13 lug 2019 alle ore 10:07 Gottfried Müller <
[hidden email]> ha scritto:

> TypeError: QSplitterHandle(Qt.Orientation, QSplitter): argument 1 has
> unexpected type 'MySplitter'
>
[...]

> class MySplitterHandle(QSplitterHandle):
>
>      def __init__(self, orientation, parent=None):
>          super().__init__(parent)
>          self.orientation(orientation)
>

The first and mandatory argument of QSplitterHandle init is a
QtCore.Qt.Orientation, not the parent.
See https://doc.qt.io/qt-5/qsplitterhandle.html#QSplitterHandle
Just add the orientation before the parent in the super.

Maurizio

--
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Got error with QSplitterHandle

Gottfried Müller
In reply to this post by Gottfried Müller
Hi,

thanks also to Maurizio. Now it works fine. But there is a small
question again. Why  is "createHandle" in MySplitter called two times?

Gottfried

Here is my new implementation:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring

import sys

from PyQt5.QtWidgets import (
     QApplication, QWidget, QListView, QSplitter, QVBoxLayout,
QSplitterHandle
)
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt


class MySplitterHandle(QSplitterHandle):

     def paintEvent(self, event):
         print("start paintEvent")
         painter = QPainter(self)
         painter.fillRect(event.rect(), Qt.blue)
         painter.end()


class MySplitter(QSplitter):

     def createHandle(self):
         print("creaMyHdl")
         return MySplitterHandle(self.orientation(), self)


class ApplWindow(QWidget):

     def __init__(self, parent=None):
         super().__init__(parent)
         v01 = QListView(parent=self)
         v02 = QListView(parent=self)
         #orientation = Qt.Vertical
         orientation = Qt.Horizontal
         splitter = MySplitter(orientation, parent=self)
         splitter.addWidget(v01)
         splitter.addWidget(v02)
         layout = QVBoxLayout()
         layout.addWidget(splitter)
         self.setLayout(layout)
         self.resize(400, 400)


def main():
     appl = QApplication(sys.argv)
     applWindow = ApplWindow()
     applWindow.show()
     return appl.exec_()


if __name__ == "__main__":
     main()

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Got error with QSplitterHandle

Barry Scott


> On 14 Jul 2019, at 08:31, Gottfried Müller <[hidden email]> wrote:
>
> Hi,
>
> thanks also to Maurizio. Now it works fine. But there is a small
> question again. Why  is "createHandle" in MySplitter called two times?

Once for each call to splitter.addWidget() maybe?

Barry

>
> Gottfried
>
> Here is my new implementation:
>
> #!/usr/bin/env python3
> # -*- coding: utf-8 -*-
> # pylint: disable=missing-docstring
>
> import sys
>
> from PyQt5.QtWidgets import (
>     QApplication, QWidget, QListView, QSplitter, QVBoxLayout,
> QSplitterHandle
> )
> from PyQt5.QtGui import QPainter
> from PyQt5.QtCore import Qt
>
>
> class MySplitterHandle(QSplitterHandle):
>
>     def paintEvent(self, event):
>         print("start paintEvent")
>         painter = QPainter(self)
>         painter.fillRect(event.rect(), Qt.blue)
>         painter.end()
>
>
> class MySplitter(QSplitter):
>
>     def createHandle(self):
>         print("creaMyHdl")
>         return MySplitterHandle(self.orientation(), self)
>
>
> class ApplWindow(QWidget):
>
>     def __init__(self, parent=None):
>         super().__init__(parent)
>         v01 = QListView(parent=self)
>         v02 = QListView(parent=self)
>         #orientation = Qt.Vertical
>         orientation = Qt.Horizontal
>         splitter = MySplitter(orientation, parent=self)
>         splitter.addWidget(v01)
>         splitter.addWidget(v02)
>         layout = QVBoxLayout()
>         layout.addWidget(splitter)
>         self.setLayout(layout)
>         self.resize(400, 400)
>
>
> def main():
>     appl = QApplication(sys.argv)
>     applWindow = ApplWindow()
>     applWindow.show()
>     return appl.exec_()
>
>
> if __name__ == "__main__":
>     main()
>
> _______________________________________________
> PyQt mailing list    [hidden email]
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Reply | Threaded
Open this post in threaded view
|

Re: Got error with QSplitterHandle

Maurizio Berti
In reply to this post by Gottfried Müller
Il giorno dom 14 lug 2019 alle ore 09:33 Gottfried Müller <
[hidden email]> ha scritto:

> thanks also to Maurizio. Now it works fine. But there is a small
> question again. Why  is "createHandle" in MySplitter called two times?
>

A QSplitterHandle is always created whenever a new widget is added, but the
*first* handle (at index 0) is hidden.
See: https://doc.qt.io/archives/qt-4.8/qsplitter.html#handle

Maurizio

--
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
_______________________________________________
PyQt mailing list    [hidden email]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt