Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Replacing a widget with another one containing splitter

Replacing a widget with another one containing splitter

Scheduled Pinned Locked Moved Unsolved Language Bindings
python
6 Posts 2 Posters 3.3k 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.
  • S Offline
    S Offline
    ShinSat
    wrote on last edited by ShinSat
    #1

    Hi,
    I’m looking into an odd issue on BoxLayout.replaceWidget in PyQt5.9
    It would be great if anybody would point me to a right direction.

    I have the following sandbox code and the goal is to split one of the exiting widget into two widgets (on one widget) without changing the other widget in the initial screen.
    The idea is to replace existing widget with another widget which contains both a new widget and the existing widget with a splitter.
    In split_widget(), it returns a splitter with new widget and the existing one.
    And then, main class App.split_wgt() performs the replacement by calling self.layoutV.replaceWidget(self.canvas, container_wgt).
    The problem is the replaced widget size is too big. I expect the replacement area is the same size as the original widget’s width&height, but containing additional widget. As a result, each new item should have 1/2 height of the original widget.
    Why I can’t control the replacement area??

    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    import sys
    
    class BaseClass(QFrame):
        def __init__(self, base=None, parent=None):
            super(BaseClass, self).__init__(parent)
            self.parent = parent
            self.base = base
    
            self.setMouseTracking(True)
            self.mouse_pos = QPoint()
            self.setFrameStyle(QFrame.Box)
            self.show()
    
        def mouseMoveEvent(self, event):
            self.mouse_pos = event.pos()
    
        def mousePressEvent(self, event):
            print('mouse pressed at ', event.pos().x(), ',', event.pos().y())
            if event.pos().y() > self.height() /2:
                print("Bottom insert")
                self.base.split_signal.emit(self, 'B')
            else:
                print("Top insert")
                self.base.split_signal.emit(self, 'T')
    
    
    
    class App(QWidget):
        split_signal = pyqtSignal(object, object)
    
        def __init__(self):
            super().__init__()
    
            self.setGeometry(50, 50, 500, 600)
            self.layoutV = QVBoxLayout()
    
            self.split_signal.connect(self.split_wgt)
    
            # Upper
            self.upper_widget = QFrame()
            self.upper_widget.setFrameStyle(QFrame.StyledPanel)
            self.pb = QPushButton('top', self.upper_widget)
            self.pb.resize(self.upper_widget.width(), self.upper_widget.height())
            self.pb.clicked.connect(self.place_base)
    
            # Lower
            self.canvas = QWidget()
            self.layoutV.addWidget(self.upper_widget)
            self.layoutV.addWidget(self.canvas)
    
    
            self.setLayout(self.layoutV)
    
    
        def place_base(self):
            container_wgt = BaseClass(base=self, parent=self.canvas)   # Container widget
            container_wgt.resize(self.canvas.width(), self.canvas.height())
    
    
        @pyqtSlot(object, object)
        def split_wgt(self, wgt, orientation):
            if orientation =='T':
                container_wgt = QWidget()
    
                self.new_wgt = BaseClass(base=self, parent=container_wgt)
                new_items_spltr = self.split_widget(wgt, self.new_wgt, 'V')
    
                self.new_layout = QVBoxLayout()
    
                self.new_splitter = QSplitter(Qt.Vertical)
                self.new_layout.addWidget(self.new_splitter)
    
                self.new_splitter.addWidget(self.new_wgt)
                self.new_splitter.addWidget(wgt)
    
                container_wgt.setLayout(self.new_layout)
    
                aaa = self.layoutV.replaceWidget(self.canvas, container_wgt)
                container_wgt.show()
    
    
        def destroy_layout(self, wgt):
            www = wgt.layout()
            QWidget().setLayout(wgt.layout())  # remove the wgt's layout
    
    
        def split_widget(self, current_wgt, new_wgt, spltr):
            splitter = QSplitter(Qt.Vertical) if spltr == 'V' else QSplitter(Qt.Horizontal)
    
            current_wgt.resize(current_wgt.width() /2, current_wgt.height() /2)
    
            new_wgt.resize(current_wgt.width() /2, current_wgt.height() /2)
            splitter.addWidget(current_wgt)
            splitter.addWidget(new_wgt)
    
            return splitter
    
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
    
        ex = App()
    
        ex.show()
    
        sys.exit(app.exec_())
    

    Many thanks in advance for your help.

    Regards,
    Sat

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

      Hi,

      Wouldn't QSplitter::setSizes do what you want ?

      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
      • S Offline
        S Offline
        ShinSat
        wrote on last edited by ShinSat
        #3

        Thanks for an update, SGaist.

        Yes, it's what I wanted! but not 100%. I still have an issue on positioning a splitter on my "main widget".
        To isolate widgets, I place each splitter on different widget(let's say container widget) on the main widget, which is something like a main window or canvas. That's safe on building complex layouts as every widget is isolated.
        Looks like the isolation doesn't work when I replace an existing widget with new one containing QSplitter...

        What I want to achieve is like this. Click on a widget splits the widget either vertically or horizontally based on the click position.
        0_1509415582997_half_split.jpg

        The current problem is that the main widget shows a different behavior when I place a container widget with qsplitter. Non-splitter on the container widget can position appropriately on the main widget but qsplitter container widget can't.
        I guess there should be something I'm missing when I position a widget containing QSplitter, but have no idea what it is... :<

        Any ideas?

        Thanks,
        Sat

        S 2 Replies Last reply
        0
        • S ShinSat

          Thanks for an update, SGaist.

          Yes, it's what I wanted! but not 100%. I still have an issue on positioning a splitter on my "main widget".
          To isolate widgets, I place each splitter on different widget(let's say container widget) on the main widget, which is something like a main window or canvas. That's safe on building complex layouts as every widget is isolated.
          Looks like the isolation doesn't work when I replace an existing widget with new one containing QSplitter...

          What I want to achieve is like this. Click on a widget splits the widget either vertically or horizontally based on the click position.
          0_1509415582997_half_split.jpg

          The current problem is that the main widget shows a different behavior when I place a container widget with qsplitter. Non-splitter on the container widget can position appropriately on the main widget but qsplitter container widget can't.
          I guess there should be something I'm missing when I position a widget containing QSplitter, but have no idea what it is... :<

          Any ideas?

          Thanks,
          Sat

          S Offline
          S Offline
          ShinSat
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • S ShinSat

            Thanks for an update, SGaist.

            Yes, it's what I wanted! but not 100%. I still have an issue on positioning a splitter on my "main widget".
            To isolate widgets, I place each splitter on different widget(let's say container widget) on the main widget, which is something like a main window or canvas. That's safe on building complex layouts as every widget is isolated.
            Looks like the isolation doesn't work when I replace an existing widget with new one containing QSplitter...

            What I want to achieve is like this. Click on a widget splits the widget either vertically or horizontally based on the click position.
            0_1509415582997_half_split.jpg

            The current problem is that the main widget shows a different behavior when I place a container widget with qsplitter. Non-splitter on the container widget can position appropriately on the main widget but qsplitter container widget can't.
            I guess there should be something I'm missing when I position a widget containing QSplitter, but have no idea what it is... :<

            Any ideas?

            Thanks,
            Sat

            S Offline
            S Offline
            ShinSat
            wrote on last edited by ShinSat
            #5

            @ShinSat By the way, I'm also looking for a framework which helps me implement thisw whether it's paid one or not.
            Does anybody know some frameworks?

            Sat

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

              What behaviour do you get with QSplitter ?

              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

              • Login

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