Home python How to add content procrut in QGroupbox?

How to add content procrut in QGroupbox?




You need to create a window within which the GROUPBOX will be in which the buttons will be dynamically added.

In case of outputting beyond the boundaries, I want to appear Scroll, but he completely refuses to perceive my attempts and appear on the screen.

At the moment I will not imagine what the problem is, I hope for help, thanks in advance!

# - * - Coding: UTF-8 - * -
from pyqt5 import qtcore, qtwidgets
Import sys.
App = Qtwidgets.Qapplication (SYS.Argv)
Window = qtwidgets.qwidget ()
Window.setWindowTitle ("Scroll")
Window.Resize (300,700)
MainBox = Qtwidgets.qvboxlayout () # here Let's place GROUPBOX
vbox = qtwidgets.qvboxlayout () # for buttons
scroll = qtwidgets.qscrolllarea () # to scroll must groupbox
Group = qtwidgets.qgroupbox ("Check me") # this groupbox
Scroll.SetWidget (Group) # for this I pass it in scrollarea
For i in Range (50): # Put the buttons
  vbox.addwidget (qtwidgets.qpushbutton ("Button {}". Format (i)))
Group.setLayout (Vbox) # We throw the area with the buttons to the group
MainBox.Addwidget (Group) # We throw a group to the main layer
Window.Setlayout (MainBox) # fix the main layer
Window.Show ()
sys.exit (app.exec_ ())

Answer 1, Authority 100%

I led to you a more functional example and noted how to do it.

from pyqt5 import qtwidgets, qtgui, qtcore
Class GroupBox (qtwidgets.qgroupbox):
  clicked = qtcore.pyqtsignal (STR, OBJECT)
  Def __init __ (Self, Title):
    Super (GroupBox, Self) .__ init __ ()
    self.title = title
    Self.Settitle (Self.title)
  Def Mousepressevent (Self, Event):
    Child = Self.Childat (Event.pos ())
    IF Not Child:
      Child = Self.
    self.clicalked.emit (Self.title, Child)
Class Mainwindow (qtwidgets.qmainwindow):
  Def __init __ (Self):
    Super (Mainwindow, Self) .__ init __ ()
    Self.setWindowTitle ("Scroll")
    Self.Resize (300,700)
    centralwidget = qtwidgets.qwidget ()
    layout = qtwidgets.qvboxLayout ()
    Centralwidget.Setlayout (Layout)
    Self.SetCentralWidget (Centralwidget)
    My_Tree = qtwidgets.Qtreewidget ()
    layout.addwidget (My_Tree)
    alpha = qtwidgets.QtreewidGetItem (My_Tree, ['Alpha'])
    Beta = qtwidgets.QtreewidgetItem (My_Tree, ['Beta'])
    alpha.addchild (qtwidgets.qtreewidgetItem (['One']))
    alpha.addchild (qtwidgets.qtreewidgetItem (['Two']))
    beta.addchild (qtwidgets.qtreewidgetItem (['first']))
    Beta.addchild (qtwidgets.QtreewidgetItem (['Second']))
    My_Tree.Expandall ()
    alpha.child (0) .setselected (True)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    scroll = qtwidgets.qscrolllarea ()
    Layout.Addwidget (Scroll)
    SCROLLLAYOUT = qtwidgets.qvboxlayout ()
    Scrollw = qtwidgets.qwidget ()
    Scroll.SetWidget (Scrollw)
    ScrollW.Setlayout (ScrollLayout)
    ScrollLayout.Setalignment (qtcore.qt.alignTop)
    Scroll.SetverticalScrollBarPolicy (qtcore.qt.scrollwayson)
    scroll.sethorizontalscrollbarpolicy (qtcore.qt.scrollbaralwaysOsoff)
    Scroll.setWidgetResizable (True)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    For _ in Range (5):
      FOOGRUP = GroupBox (F'Groupbox _ {_} ')
      FOOGRUP.SetOggjectName (F'Foogroup {_} ')
      fooogroup.clicalked.connect (self.onfoogroupClick)
      FOOLAYOUT = qtwidgets.Qvboxlayout ()
      FOOGRUP.Setlayout (Foolayout)
      FOOTEM1 = qtwidgets.QLabel ("FOOTEM1", OBJECTNAME = "FOOTEM1")
      FOOTEM1.SetStylesheet ('Background: # 44FFFF')
      FOOTEM2 = qtwidgets.QLabel ("FOOTEM2", OBJECTNAME = "FOOTEM2")
      FOOTEM2.SetStyleSheet ('Background: # ffff56;')
      FOOTEM3 = qtwidgets.QLabel ("FOOTEM3", OBJECTNAME = "FOOTEM3")
      FooIeTem3.SetStylesheet ('Background: # FF42FF;') 
      Foolayout.addwidget (FOOTEM2)
      ScrollLayout.addwidget (FOOGroup) # +++
  DEF OnfoogroupClick (Self, Title, OBJ):
    Print (F "Group: {title}; ObjectName =` {Obj.ObjectName ()} `")
if __name__ == '__main__':
  App = Qtwidgets.Qapplication ([])
  Window = MainWindow ()
  Window.Show ()
  app.exec_ ()

If after studying something is not clear, ask.


import sys
from pyqt5.qt import *
Class Widget (QWidget):
  Def __init __ (Self):
    Super () .__ init __ ()
    widget = qwidget ()
    #Layout of Container Widget
    Layout = QvboxLayout (Self)
    For _ in Range (20):
      BTN = QPushButton ("Test")
      layout.addwidget (BTN)
    Widget.SetlaYout (Layout)
    #Scroll Area Properties.
    scroll = qscrolllarea ()
    Scroll.SetverticalScrollBarPolicy (Qt.ScrollBaraLewayson)
    Scroll.SethorizontalscrollBarpolicy (Qt.ScrollBaraLewaysoff)
    Scroll.setWidgetResizable (True)
    Scroll.SetWidget (Widget)
    #Scroll Area Layer Add
    Self.setLayout (VLAYOUT)
if __name__ == '__main__':
  App = Qapplication (SYS.Argv)
  Dialog = widget ()
  dialog.show ()
  app.exec_ ()

Answer 2

As stated in documentation

Note that you must add leiaut widget before calling SetWidget;
If you add it later, the widget will not be visible – even if
Call Show for scrolling area.

Note That You Must Add The Layout Of Widget Before You Call This
FUNCTION; IF You Add It Later, The Widget Will Not Be Visible –
Regardless of what you show () The Scroll Area. In This Case, You Can

That is, for the appearance of the scrolling, the command sequence should be like this:

# 1. Installation of a liauta for a widget
Group.setLayout (Vbox) # We throw the area with the buttons to the group
# 2. then adding a group to scroll
Scroll.SetWidget (Group)
Previous articlemysql won’t start
Next articlefactorial in java

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions