Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-104559

There is a problem of selecting an area of QTextEdit with mouse when they are horizontally located.

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 6.3.0
    • GUI: Text handling
    • None
    • Windows

    Description

      I have a QGraphicsView(MAIN) and QGraphicsScene(MAIN). The QGraphicsScene(MAIN) has a number of QGraphicsView(SUB).QGraphicsView(SUB) shows QTextEdit by QGraphicsScene(SUB).Each QGraphicsScene(SUB) shows each page of one QTextEdit respectively.

      I'm layouting these QGraphicsView(SUB) with flow-layout-style. I can select a range of texts  with mouse in one page without problem. At the time of the layouting is vertically, so is it.

      But once the layouting becomes horizontally, the selection goes bad. 

      For example, there are  three pages in one line horizontally. I put mouse on the second page.

      The mouse event position returns the range of the second page of QTextEdit. Until here, there is no problem. Then I drag and go to the next page, the position x is over the document range.  On the other hand, I drag and go back to the previous page, the position x becomes less than 0.

      (Edit:)Moreover, the y position is unchanged. I want y position to change itself to the range of the first page height when I go back to the previous page, and the range of the third page height when I go to the next page. Only when I put mouse on an arbitrary page, the y position is loyally in the range of height of the page.

      The point where I think this behavior is strange is as follows:

      y coordinate is decided by the QTextEdit coordinate. Page 1 returns 0 ~ A4 height. Page 2 returns A4 height ~ A4 height*2. Page3 --.
      x coordinate is also decided by the QTextEdit coordinate. Page 1 returns 0 ~ A4 width. Page2 returns 0 ~ A4 width. Page3 --.

      But once, when I drag and move to the other pages, why aren't the positions decided by the QTextEdit coordinate?

      If I drag and move under (vertically), I can select the next page in spite of horizontal direction. ( So I can select without problem in the case of vertical direction. This behavior seems to be unchanged in the case of horizontal direction.)

      In a word , Qt doesn't allow us to select texts with mouse in the case of horizontal direction.
      In the case of keyboard, there is no problem. Shift + direction key.

      sample code

      from PySide6.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsProxyWidget, QTextEdit
      from PySide6.QtGui import QTextDocument, QPageSize, QPainter, QBrush, QTextCharFormat, QTextOption
      from PySide6.QtCore import Qt, Signal, QRect, QRectF, QEvent, QSize, QSizeF, QPointF
      
      
      
      class MainGraphicsView(QGraphicsView):
      
          
          def __init__(self, parent=None):
      
              super(MainGraphicsView, self).__init__(parent)
      
      
              self._mainscene = MainGraphicsScene(1)
              self._mainscene._mainview = self
              self.setScene(self._mainscene)          
      
      class MainGraphicsScene(QGraphicsScene):  
      
          def __init__(self, orientation = 1, parent=None):
      
              super(MainGraphicsScene, self).__init__(parent)
              
              if orientation == 0:
      
                  self.rootView = TextGraphicsView()        
                  self.rootView.main_scene = self
                  self.root_proxywidget = self.addWidget(self.rootView)
                  self.rootView.setSceneRect(0, 0, self.document()._pageWidth, self.document()._pageHeight)
      
                  self.secondaryView = SecondaryTextGraphicsView(2, self.rootView.scene())
                  self.secondaryView.setSceneRect(0, self.document()._pageHeight, self.document()._pageWidth, self.document()._pageHeight)
                  self.secondary_proxywidget = self.addWidget(self.secondaryView)
                  self.secondary_proxywidget.setPos(0, self.document()._pageHeight)
      
                  self.thirdView = SecondaryTextGraphicsView(3, self.rootView.scene())
                  self.thirdView.setSceneRect(0, self.document()._pageHeight*2, self.document()._pageWidth, self.document()._pageHeight)
                  self.third_proxywidget = self.addWidget(self.thirdView)
                  self.third_proxywidget.setPos(0, self.document()._pageHeight*2)
      
                  
              if orientation == 1:
                  
                  self.rootView = TextGraphicsView()        
                  self.rootView.main_scene = self
                  self.root_proxywidget = self.addWidget(self.rootView)
                  self.rootView.setSceneRect(0, 0, self.document()._pageWidth, self.document()._pageHeight)
      
                  self.secondaryView = SecondaryTextGraphicsView(2, self.rootView.scene())
                  self.secondaryView.setSceneRect(0, self.document()._pageHeight, self.document()._pageWidth, self.document()._pageHeight)
                  self.secondary_proxywidget = self.addWidget(self.secondaryView)
                  self.secondary_proxywidget.setPos(self.document()._pageWidth, 0)
      
                  self.thirdView = SecondaryTextGraphicsView(3, self.rootView.scene())
                  self.thirdView.setSceneRect(0, self.document()._pageHeight*2, self.document()._pageWidth, self.document()._pageHeight)
                  self.third_proxywidget = self.addWidget(self.thirdView)
                  self.third_proxywidget.setPos(self.document()._pageWidth*2, 0)
      
              self.setBackgroundBrush(QBrush(Qt.gray))      
         
          def view(self):
      
              return self._mainview
      
          def textedit(self):
      
              return self.document().parent()
      
          def document(self):
      
              return self.rootView.document()
          
          
      class TextGraphicsView(QGraphicsView):
          
          def __init__(self,  parent=None):
      
              super().__init__(parent)
      
              self.page = 1
      
              self.initScene()
      
          def mousePressEvent(self, event):
      
              SecondaryTextGraphicsView.selectedPage = self.page
              return QGraphicsView.mousePressEvent(self, event)
      
          def initScene(self):
              
              self.common_scene = TextGraphicsScene()
              self.common_scene.root_view = self
              
              self.setScene(self.common_scene)
              self.setSceneRect(0, self.document()._pageHeight*(self.page -1), self.document()._pageWidth, self.document()._pageHeight)
              self.resize(int(self.document()._pageWidth), int(self.document()._pageHeight))
      
              self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
              self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
      
          def view(self):
      
              return self.main_scene._mainview
          
          def textedit(self):
      
              return self.common_scene.textedit
          
          def document(self):
      
              return self.common_scene.textedit.document()       
      
      class SecondaryTextGraphicsView(QGraphicsView):
      
          def __init__(self, page, scene, parent=None):
      
              super().__init__(scene, parent)
      
              self.page = page
      
              self.initScene()
      
              self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
              self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)    
      
          def initScene(self):
              
              self.setSceneRect(0, self.document()._pageHeight*(self.page-1), self.document()._pageWidth, self.document()._pageHeight)
              self.resize(int(self.document()._pageWidth), int(self.document()._pageHeight))
      
          def view(self):
      
              return self.scene().root_view.view()
      
          def textedit(self):
      
              return self.scene().textedit()
          
      
          def document(self):
      
              return self.scene().textedit.document()
      
      
      class TextGraphicsScene(QGraphicsScene):
      
      
          def __init__(self, parent=None):
      
              super().__init__(parent)        
      
              self.textedit = TextEdit(scene=self)        
             
              self.addWidget(self.textedit)
      
      
          def view(self):
      
              return self.root_view
      
          def document(self):
      
              return self.textedit.document()     
          
      
      class TextEdit(QTextEdit):
          
          def __init__(self,  scene, parent=None):
      
              super().__init__(parent)
              
              self.setAttribute(Qt.WA_InputMethodEnabled, True)
              self.setMouseTracking(False)
              self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
              self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)       
            
              self.scene = scene
              self.setDocument(TextDocument(parent=self))
      
              self.setWordWrapMode(QTextOption.WrapAtWordBoundaryOrAnywhere)
              self.setLineWrapMode(self.LineWrapMode.FixedPixelWidth)
              self.setLineWrapColumnOrWidth(self.document()._pageWidth)
              self.setAttribute(Qt.WA_InputMethodEnabled, True)
              self.setInputMethodHints(Qt.ImhMultiLine)
              self.resize(self.document()._pageSize.toSize().width(), self.document()._pageSize.toSize().height()*3)
              self.setWordWrapMode(QTextOption.WrapAtWordBoundaryOrAnywhere)
      
          def mouseMoveEvent(self, event):
      
      
              print(event.position())
              return QTextEdit.mouseMoveEvent(self, event)
      
          
          def view(self):
      
              return self.scene.root_view
          
          def pageSize(self):
      
              return self.document()._pageSize    
              
      
      class TextDocument(QTextDocument):
          
          def __init__(self, parent=None):
              super().__init__(parent)
      
              self._pageSize = QPageSize.size(QPageSize.A4, QPageSize.Point)       
       
              self._pageHeight = self._pageSize.height()
              self._pageWidth  = self._pageSize.width()
              self.setDocumentMargin(30.5) 
      
      
      def main():
          import sys
          
          app = QApplication([]) if QApplication.instance() is None else QApplication.instance()
          m = MainGraphicsView()
          m.show()
          sys.exit(app.exec())
      
      if __name__ == "__main__":
          main()
      
      

       

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            nori Harunori Fujimoto
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes