PyQt layout question: QScrollView and QGridLayout?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Volker Lenhardt

    PyQt layout question: QScrollView and QGridLayout?

    For a QApplication (PyQt) on the small screen of my Zaurus 5500 PDA I
    try to layout my data output in a QScrollView as the central widget. I'd
    prefer to use QGridLayout, but cannot add it to the scroll view.

    sc=QScrollView( self)
    layout=QGridLay out(..., sc.viewport())
    sc.addChild(lay out)

    results in a TypeError.

    Is there a way to get it to work? Filling a box viewport with lots of
    padding boxes and white space labels to establish grids is very
    cumbersome. And I need 4 different layouts to change places.

    Best wishes
    Volker

    --
    Volker Lenhardt
    E-Mail: volker.lenhardt @uni-duisburg-essen.de
  • Phil Thompson

    #2
    Re: PyQt layout question: QScrollView and QGridLayout?

    On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:[color=blue]
    > For a QApplication (PyQt) on the small screen of my Zaurus 5500 PDA I
    > try to layout my data output in a QScrollView as the central widget. I'd
    > prefer to use QGridLayout, but cannot add it to the scroll view.
    >
    > sc=QScrollView( self)
    > layout=QGridLay out(..., sc.viewport())
    > sc.addChild(lay out)
    >
    > results in a TypeError.
    >
    > Is there a way to get it to work? Filling a box viewport with lots of
    > padding boxes and white space labels to establish grids is very
    > cumbersome. And I need 4 different layouts to change places.[/color]

    QGridLayout is not a sub-class of QWidget, which is what addChild() is
    expecting. You probably want QGrid.

    Phil

    Comment

    • Phil Thompson

      #3
      Re: PyQt layout question: QScrollView and QGridLayout?

      On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:[color=blue]
      > For a QApplication (PyQt) on the small screen of my Zaurus 5500 PDA I
      > try to layout my data output in a QScrollView as the central widget. I'd
      > prefer to use QGridLayout, but cannot add it to the scroll view.
      >
      > sc=QScrollView( self)
      > layout=QGridLay out(..., sc.viewport())
      > sc.addChild(lay out)
      >
      > results in a TypeError.
      >
      > Is there a way to get it to work? Filling a box viewport with lots of
      > padding boxes and white space labels to establish grids is very
      > cumbersome. And I need 4 different layouts to change places.[/color]

      QGridLayout is not a sub-class of QWidget, which is what addChild() is
      expecting. You probably want QGrid.

      Phil

      Comment

      • Volker Lenhardt

        #4
        Re: PyQt layout question: QScrollView and QGridLayout?

        Phil Thompson schrieb:[color=blue]
        > On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
        >[color=green]
        >>prefer to use QGridLayout, but cannot add it to the scroll view.
        >>
        >>sc=QScrollVie w(self)
        >>layout=QGridL ayout(..., sc.viewport())
        >>sc.addChild(l ayout)
        >>
        >>results in a TypeError.
        >>
        >>Is there a way to get it to work? Filling a box viewport with lots of
        >>padding boxes and white space labels to establish grids is very
        >>cumbersome. And I need 4 different layouts to change places.[/color]
        >
        >
        > QGridLayout is not a sub-class of QWidget, which is what addChild() is
        > expecting. You probably want QGrid.
        >
        > Phil[/color]

        I hoped to find a more assuring answer. There's no MultiCellWidget , no
        Col/RowStretching, no Col/RowSpacing in QGrid. I've got to patch up one
        VBox with a whole bunch of QV/QHBoxes and QGrids not to mention the
        white space QLabels to fill not used grid cells.

        And I have to delete all of them to change to another data layout.

        Are you sure that there's no way to fill a QScrollView with the help of
        some QLayout?

        Still hopefully
        Volker

        Comment

        • Volker Lenhardt

          #5
          Re: PyQt layout question: QScrollView and QGridLayout?

          Phil Thompson schrieb:[color=blue]
          > On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
          >[color=green]
          >>prefer to use QGridLayout, but cannot add it to the scroll view.
          >>
          >>sc=QScrollVie w(self)
          >>layout=QGridL ayout(..., sc.viewport())
          >>sc.addChild(l ayout)
          >>
          >>results in a TypeError.
          >>
          >>Is there a way to get it to work? Filling a box viewport with lots of
          >>padding boxes and white space labels to establish grids is very
          >>cumbersome. And I need 4 different layouts to change places.[/color]
          >
          >
          > QGridLayout is not a sub-class of QWidget, which is what addChild() is
          > expecting. You probably want QGrid.
          >
          > Phil[/color]

          I hoped to find a more assuring answer. There's no MultiCellWidget , no
          Col/RowStretching, no Col/RowSpacing in QGrid. I've got to patch up one
          VBox with a whole bunch of QV/QHBoxes and QGrids not to mention the
          white space QLabels to fill not used grid cells.

          And I have to delete all of them to change to another data layout.

          Are you sure that there's no way to fill a QScrollView with the help of
          some QLayout?

          Still hopefully
          Volker

          Comment

          • dwelch

            #6
            Re: PyQt layout question: QScrollView and QGridLayout?

            Volker Lenhardt wrote:[color=blue]
            > Phil Thompson schrieb:
            >[color=green]
            >> On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
            >>[color=darkred]
            >>> prefer to use QGridLayout, but cannot add it to the scroll view.
            >>>
            >>> sc=QScrollView( self)
            >>> layout=QGridLay out(..., sc.viewport())
            >>> sc.addChild(lay out)
            >>>
            >>> results in a TypeError.
            >>>
            >>> Is there a way to get it to work? Filling a box viewport with lots of
            >>> padding boxes and white space labels to establish grids is very
            >>> cumbersome. And I need 4 different layouts to change places.[/color]
            >>
            >>
            >>
            >> QGridLayout is not a sub-class of QWidget, which is what addChild() is
            >> expecting. You probably want QGrid.
            >>
            >> Phil[/color]
            >
            >
            > I hoped to find a more assuring answer. There's no MultiCellWidget , no
            > Col/RowStretching, no Col/RowSpacing in QGrid. I've got to patch up one
            > VBox with a whole bunch of QV/QHBoxes and QGrids not to mention the
            > white space QLabels to fill not used grid cells.
            >
            > And I have to delete all of them to change to another data layout.
            >
            > Are you sure that there's no way to fill a QScrollView with the help of
            > some QLayout?
            >
            > Still hopefully
            > Volker[/color]

            I _think_ I have code that does waht you want. This creates multiple
            layouts that contain an icon, a button, and some text on a scrollview.
            The code is awkward and ugly, but it works.


            class ScrollToolView( QScrollView):
            def __init__(self,p arent = None,name = None,fl = 0):
            QScrollView.__i nit__(self,pare nt,name,fl)
            self.items = {}
            self.setStaticB ackground(True)
            self.enableClip per(True)

            self.viewport() .setPaletteBack groundColor(qAp p.palette().col or(QPalette.Act ive,
            QColorGroup.Bac kground))
            self.row_height = 120

            def viewportResizeE vent(self, e):
            for x in self.items:
            self.items[x].resize(e.size( ).width(), self.row_height )

            def addItem(self, name, title, pix, text, button_text, button_func):
            num_items = len(self.items)
            LayoutWidget = QWidget(self.vi ewport(),"layou twidget")
            LayoutWidget.se tSizePolicy(QSi zePolicy(QSizeP olicy.Expanding ,
            QSizePolicy.Min imum))
            LayoutWidget.se tGeometry(QRect (0, 0, self.width(),
            self.row_height ))
            self.addChild(L ayoutWidget)

            if num_items:
            self.moveChild( LayoutWidget, 0, self.row_height *num_items)

            layout = QGridLayout(Lay outWidget,1,1,1 0,10,"layout")

            pushButton = QPushButton(Lay outWidget,"push Button")

            pushButton.setS izePolicy(QSize Policy(QSizePol icy.Maximum,QSi zePolicy.Fixed, 0,0,

            pushButton.size Policy().hasHei ghtForWidth()))
            self.connect(pu shButton,SIGNAL ("clicked()" ), button_func)

            layout.addWidge t(pushButton,2, 2)

            textLabel = QLabel(LayoutWi dget,"textLabel ")

            layout.addWidge t(textLabel,1,1 )

            pixmap = QLabel(LayoutWi dget,"pixmapLab el2")

            pixmap.setSizeP olicy(QSizePoli cy(QSizePolicy. Fixed,QSizePoli cy.Fixed,0,0,
            pixmap.sizePoli cy().hasHeightF orWidth()))
            pixmap.setMinim umSize(QSize(32 ,32))
            pixmap.setMaxim umSize(QSize(32 ,32))
            pixmap.setPixma p(pix)
            pixmap.setScale dContents(1)

            layout.addWidge t(pixmap,1,0)

            textLabel2 = QLabel(LayoutWi dget,"textLabel 2")
            textLabel2.setA lignment(QLabel .WordBreak | QLabel.AlignTop )

            textLabel2.setS izePolicy(QSize Policy(QSizePol icy.Minimum,QSi zePolicy.Expand ing))

            layout.addWidge t(textLabel2,2, 1)

            if num_items:
            line = QFrame(LayoutWi dget,"line")
            line.setFrameSh adow(QFrame.Sun ken)
            line.setFrameSh ape(QFrame.HLin e)
            layout.addMulti CellWidget(line ,0,0,0,2)

            textLabel.setTe xt(title)
            textLabel2.setT ext(text)
            pushButton.setT ext(button_text )
            self.resizeCont ents(self.width (), num_items*self. row_height*2)

            LayoutWidget.sh ow()

            try:
            self.items[name]
            except KeyError:
            self.items[name] = LayoutWidget
            else:
            print "ERROR: Duplicate button name:", name

            def clear(self):
            if len(self.items) :
            for x in self.items:
            self.removeChil d(self.items[x])
            self.items[x].hide()

            self.items.clea r()
            self.resizeCont ents(self.width (), 0)



            -Don

            Comment

            • dwelch

              #7
              Re: PyQt layout question: QScrollView and QGridLayout?

              Volker Lenhardt wrote:[color=blue]
              > Phil Thompson schrieb:
              >[color=green]
              >> On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:
              >>[color=darkred]
              >>> prefer to use QGridLayout, but cannot add it to the scroll view.
              >>>
              >>> sc=QScrollView( self)
              >>> layout=QGridLay out(..., sc.viewport())
              >>> sc.addChild(lay out)
              >>>
              >>> results in a TypeError.
              >>>
              >>> Is there a way to get it to work? Filling a box viewport with lots of
              >>> padding boxes and white space labels to establish grids is very
              >>> cumbersome. And I need 4 different layouts to change places.[/color]
              >>
              >>
              >>
              >> QGridLayout is not a sub-class of QWidget, which is what addChild() is
              >> expecting. You probably want QGrid.
              >>
              >> Phil[/color]
              >
              >
              > I hoped to find a more assuring answer. There's no MultiCellWidget , no
              > Col/RowStretching, no Col/RowSpacing in QGrid. I've got to patch up one
              > VBox with a whole bunch of QV/QHBoxes and QGrids not to mention the
              > white space QLabels to fill not used grid cells.
              >
              > And I have to delete all of them to change to another data layout.
              >
              > Are you sure that there's no way to fill a QScrollView with the help of
              > some QLayout?
              >
              > Still hopefully
              > Volker[/color]

              I _think_ I have code that does waht you want. This creates multiple
              layouts that contain an icon, a button, and some text on a scrollview.
              The code is awkward and ugly, but it works.


              class ScrollToolView( QScrollView):
              def __init__(self,p arent = None,name = None,fl = 0):
              QScrollView.__i nit__(self,pare nt,name,fl)
              self.items = {}
              self.setStaticB ackground(True)
              self.enableClip per(True)

              self.viewport() .setPaletteBack groundColor(qAp p.palette().col or(QPalette.Act ive,
              QColorGroup.Bac kground))
              self.row_height = 120

              def viewportResizeE vent(self, e):
              for x in self.items:
              self.items[x].resize(e.size( ).width(), self.row_height )

              def addItem(self, name, title, pix, text, button_text, button_func):
              num_items = len(self.items)
              LayoutWidget = QWidget(self.vi ewport(),"layou twidget")
              LayoutWidget.se tSizePolicy(QSi zePolicy(QSizeP olicy.Expanding ,
              QSizePolicy.Min imum))
              LayoutWidget.se tGeometry(QRect (0, 0, self.width(),
              self.row_height ))
              self.addChild(L ayoutWidget)

              if num_items:
              self.moveChild( LayoutWidget, 0, self.row_height *num_items)

              layout = QGridLayout(Lay outWidget,1,1,1 0,10,"layout")

              pushButton = QPushButton(Lay outWidget,"push Button")

              pushButton.setS izePolicy(QSize Policy(QSizePol icy.Maximum,QSi zePolicy.Fixed, 0,0,

              pushButton.size Policy().hasHei ghtForWidth()))
              self.connect(pu shButton,SIGNAL ("clicked()" ), button_func)

              layout.addWidge t(pushButton,2, 2)

              textLabel = QLabel(LayoutWi dget,"textLabel ")

              layout.addWidge t(textLabel,1,1 )

              pixmap = QLabel(LayoutWi dget,"pixmapLab el2")

              pixmap.setSizeP olicy(QSizePoli cy(QSizePolicy. Fixed,QSizePoli cy.Fixed,0,0,
              pixmap.sizePoli cy().hasHeightF orWidth()))
              pixmap.setMinim umSize(QSize(32 ,32))
              pixmap.setMaxim umSize(QSize(32 ,32))
              pixmap.setPixma p(pix)
              pixmap.setScale dContents(1)

              layout.addWidge t(pixmap,1,0)

              textLabel2 = QLabel(LayoutWi dget,"textLabel 2")
              textLabel2.setA lignment(QLabel .WordBreak | QLabel.AlignTop )

              textLabel2.setS izePolicy(QSize Policy(QSizePol icy.Minimum,QSi zePolicy.Expand ing))

              layout.addWidge t(textLabel2,2, 1)

              if num_items:
              line = QFrame(LayoutWi dget,"line")
              line.setFrameSh adow(QFrame.Sun ken)
              line.setFrameSh ape(QFrame.HLin e)
              layout.addMulti CellWidget(line ,0,0,0,2)

              textLabel.setTe xt(title)
              textLabel2.setT ext(text)
              pushButton.setT ext(button_text )
              self.resizeCont ents(self.width (), num_items*self. row_height*2)

              LayoutWidget.sh ow()

              try:
              self.items[name]
              except KeyError:
              self.items[name] = LayoutWidget
              else:
              print "ERROR: Duplicate button name:", name

              def clear(self):
              if len(self.items) :
              for x in self.items:
              self.removeChil d(self.items[x])
              self.items[x].hide()

              self.items.clea r()
              self.resizeCont ents(self.width (), 0)



              -Don

              Comment

              • David Boddie

                #8
                Re: PyQt layout question: QScrollView and QGridLayout?

                Volker Lenhardt wrote:[color=blue]
                > Phil Thompson schrieb:[color=green]
                > > On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:[/color][/color]

                [Using a QGridLayout in a QScrollView]
                [color=blue][color=green][color=darkred]
                > >>Is there a way to get it to work? Filling a box viewport with lots of
                > >>padding boxes and white space labels to establish grids is very
                > >>cumbersome. And I need 4 different layouts to change places.[/color]
                > >
                > > QGridLayout is not a sub-class of QWidget, which is what addChild() is
                > > expecting. You probably want QGrid.[/color]
                >
                > I hoped to find a more assuring answer. There's no MultiCellWidget , no
                > Col/RowStretching, no Col/RowSpacing in QGrid. I've got to patch up one
                > VBox with a whole bunch of QV/QHBoxes and QGrids not to mention the
                > white space QLabels to fill not used grid cells.[/color]

                You can still use QGridLayout, but you need to put a widget into the
                QScrollView in what the documentation describes as "Using One Big
                Widget" (http://doc.trolltech.com/3.3/qscrollview.html).

                Something along these lines should do basically what you want:

                sv = QScrollView()
                w = QWidget(sv.view port())
                sv.addChild(w)

                grid = QGridLayout(w, 3, 3)

                for i in range(0,3):
                for j in range(0,3):
                grid.addWidget( QLabel("(%i,%i) " % (i,j), w), i, j)

                Of course, the widget that contains the layout is only as large as it
                needs to be, so you may need to call its setMinimumSize( ) method if
                you want it to be larger. However, it's often a better idea to ensure
                that the child widgets have the sizes they need - their parent will
                then be as large as they require.

                Hope this helps,

                David

                Comment

                • David Boddie

                  #9
                  Re: PyQt layout question: QScrollView and QGridLayout?

                  Volker Lenhardt wrote:[color=blue]
                  > Phil Thompson schrieb:[color=green]
                  > > On Thursday 17 November 2005 2:56 pm, Volker Lenhardt wrote:[/color][/color]

                  [Using a QGridLayout in a QScrollView]
                  [color=blue][color=green][color=darkred]
                  > >>Is there a way to get it to work? Filling a box viewport with lots of
                  > >>padding boxes and white space labels to establish grids is very
                  > >>cumbersome. And I need 4 different layouts to change places.[/color]
                  > >
                  > > QGridLayout is not a sub-class of QWidget, which is what addChild() is
                  > > expecting. You probably want QGrid.[/color]
                  >
                  > I hoped to find a more assuring answer. There's no MultiCellWidget , no
                  > Col/RowStretching, no Col/RowSpacing in QGrid. I've got to patch up one
                  > VBox with a whole bunch of QV/QHBoxes and QGrids not to mention the
                  > white space QLabels to fill not used grid cells.[/color]

                  You can still use QGridLayout, but you need to put a widget into the
                  QScrollView in what the documentation describes as "Using One Big
                  Widget" (http://doc.trolltech.com/3.3/qscrollview.html).

                  Something along these lines should do basically what you want:

                  sv = QScrollView()
                  w = QWidget(sv.view port())
                  sv.addChild(w)

                  grid = QGridLayout(w, 3, 3)

                  for i in range(0,3):
                  for j in range(0,3):
                  grid.addWidget( QLabel("(%i,%i) " % (i,j), w), i, j)

                  Of course, the widget that contains the layout is only as large as it
                  needs to be, so you may need to call its setMinimumSize( ) method if
                  you want it to be larger. However, it's often a better idea to ensure
                  that the child widgets have the sizes they need - their parent will
                  then be as large as they require.

                  Hope this helps,

                  David

                  Comment

                  • Volker Lenhardt

                    #10
                    Re: PyQt layout question: QScrollView and QGridLayout?

                    Thank you Don, thank you David,

                    I was convinced that there must be a simple solution at hand. A dummy
                    widget!

                    It does work to my needs. Don's ScrollToolView is very interesting
                    though not yet the right tool for my actual application, but I've got
                    some more ideas for long winter nights ...

                    All the best
                    Volker

                    Comment

                    • Volker Lenhardt

                      #11
                      Re: PyQt layout question: QScrollView and QGridLayout?

                      Thank you Don, thank you David,

                      I was convinced that there must be a simple solution at hand. A dummy
                      widget!

                      It does work to my needs. Don's ScrollToolView is very interesting
                      though not yet the right tool for my actual application, but I've got
                      some more ideas for long winter nights ...

                      All the best
                      Volker

                      Comment

                      Working...