Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QWidget inheriter in QScrollArea : problem of scroll clipping
Forum Update on Monday, May 27th 2025

QWidget inheriter in QScrollArea : problem of scroll clipping

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 3 Posters 665 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mika-L
    wrote on last edited by
    #1

    Dear All,
    I am quite new to QT5 / QT5 for Python (here on Linux). I created in inheriter of QWidget which is placed inside a QScrollArea. In ma QWidget, I override the paintEvent an I draw stuff inside. It works well, and the things I paint follow the scrollbars. Good.
    Now I want to draw an "overlay" element, which position does not follow the scrollbars (it is "fixed" even when I move the scrollbars). About the coordinates system, I played successfully I guess withmapFromParent to get the good coordinates. And it works but I need to resize the window (for example) to obtain the good result. If I only play with the scrollbars, I have only a part of the element that is painted, and sometimes it disappears too.

    I think I have a problem with the clipping, but I did not understand what I should do to put the full viewport area as clipping area (or perhaps is there a better way to do this ?).

    Can someone help ?

    Thanks a lot,
    Mike

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Without any code, it's hard to tell. Would it be possible to see it ?

      Depending on what you want to do, you might want to check the Graphics View Framework.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mika-L
        wrote on last edited by
        #3

        Sorry for the delay I was not notified about your kind message.
        I wrote a simplified code that exhibits the same problem. First I made a global UI design in "designer". The part that intersts us is pictured here :
        designer.png
        Then I wrote a "MImage.py" file that contains the following code (simplified a lot but working example) :

        from PyQt5 import QtWidgets, QtCore, uic, QtGui
        
        class MImage(QtWidgets.QWidget):
            def __init__(self, *arg):
                super().__init__()
                self.image = QtGui.QImage("path_to_some_picture_here..jpg")	
                self.MRedBrush = QtGui.QBrush(QtGui.QColor(200,0,0))    
         
            def paintEvent(self, event):
                self.resize(self.image.width(), self.image.height())  
                # below : not sure to understand why I need the parent of the parent widget, it is not similar as in Designer.
                self.parentWidget().parentWidget().setHorizontalScrollBarPolicy( QtCore.Qt.ScrollBarAsNeeded )
                self.parentWidget().parentWidget().setVerticalScrollBarPolicy( QtCore.Qt.ScrollBarAsNeeded )
        
                painter = QtGui.QPainter(self)
                # draw some scrollable stuff    	
                pixMap = QtGui.QPixmap.fromImage(self.appData.image)
                painter.drawPixmap(QtCore.QRect(0, 0, self.image.width(), self.image.height()), pixMap)
        
                # draw some non scrollable stuff  << == here is the problem
                topleft=self.mapFromParent(QtCore.QPoint(20,20))
                painter.fillRect(topleft.x(),topleft.y(),500,20,self.MRedBrush)   
        

        When I do this, I get this :
        ---> At program startup :
        initial.png
        ---> After a horizontal scroll :
        scrolled1.png
        ---> After resizing a bit the window to force an repaint :
        scrolled2.png

        So it seems my code is correct regarding the drawing coordinates. However, during the scroll, it seems a full repaint is not performed. I guess it is due to clipping rectangle (I guess Qt uses hardware acceleration to bitmap copy the contents of a part of the view). So my question is : is there a way to enforce the clipping rectangle to the whole view for example (or to exclude the part that interests me from the clipping region), or is there a better design for this (perhaps superimposing two views, one for scroll, another one for static display).

        Thanks a lot,
        Mikhaël

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why are you calling resize in your paintEvent ?

          Accessing the parent of the parent is a bad idea and even more in the paintEvent. Accessing parent is already a bad idea.

          Since that rectangle shall not move, why not put it in its own widget that you manually place at that fixed position ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mika-L
            wrote on last edited by
            #5

            I am not a skilled user and I don't know how to add a widget superimposed to another one. Can you give some advice about it ?

            I however found a solution. My interpretation was not correct, clipping does not seems to come from system areas to update. So what I did was the following : let's call scrollerwidget the scrollarea, I added an update to the event related to scrollers position change :
            self.scrollerWidget.horizontalScrollBar().valueChanged.connect(self.update)
            self.scrollerWidget.verticalScrollBar().valueChanged.connect(self.update)

            It works. But I would like to know, as you suggest, how to add another widget superimposed ?

            Thanks a lot,
            Mike

            M 1 Reply Last reply
            0
            • M Mika-L

              I am not a skilled user and I don't know how to add a widget superimposed to another one. Can you give some advice about it ?

              I however found a solution. My interpretation was not correct, clipping does not seems to come from system areas to update. So what I did was the following : let's call scrollerwidget the scrollarea, I added an update to the event related to scrollers position change :
              self.scrollerWidget.horizontalScrollBar().valueChanged.connect(self.update)
              self.scrollerWidget.verticalScrollBar().valueChanged.connect(self.update)

              It works. But I would like to know, as you suggest, how to add another widget superimposed ?

              Thanks a lot,
              Mike

              M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              @Mika-L said in QWidget inheriter in QScrollArea : problem of scroll clipping:

              how to add another widget superimposed ?

              To add a fixed widget above the scroll area, create a new widget with the scroll area (image_area_scroller) as parent.
              It will be at fixed position on the left top corner of the scroll area, regardless the scroll bars.

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Mika-L
                wrote on last edited by
                #7

                ok great ! I will try this. Thanks a lot !

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved