Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make QWidget contents scrollable in PyQt?

Tags:

python

qt4

pyqt

First, I started using PyQt few hours ago. So far so good - im writing rss client to familiarize myself with PyQt I got QApplication, QMainWindow and two custom widgets. First custom widget is:

class RssItem(QWidget):
    __pyqtSignals__ = ("articleViewed(bool)",
                       "articleOpened(bool)",
                       "articleMarkedGood(bool)")

    def __init__(self, title, date, parent = None):
        super(RssItem, self).__init__(parent)
        self.initWidget(title, date)

    def initWidget(self, title, date):
        title = QLabel(title)
        date = QLabel(date)
        titleBox = QHBoxLayout()
        titleBox.addWidget(title)
        titleBox.addWidget(date)
        self.setLayout(titleBox)

That displays (for now) title and date in single row Second one accepts array of RssItem widgets and display them in vertical list:

class ItemsList(QWidget):
    def __init__(self, items, parent=None):
        super(ItemsList, self).__init__(parent)
        self.initWidget(items)

    def initWidget(self, items):
        listBox = QVBoxLayout(self)
        for item in items:
            listBox.addWidget(item)
            listBox.addStretch(1)
        self.setLayout(listBox)

How do I make this list scrollable? Keep in mid I'm planing to have multiple ItemList's in one window each should have it's own scrollbar.

Main app function as for now is only for testing these 2 widgets:

class MainApp(Qt.QApplication):
    def __init__(self, args):
        super(MainApp, self).__init__(args)
        self.addWidgets()
        self.exec_()

    def addWidgets(self):
        self.window = MainWindow()


class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.initUI()

    def initUI(self):
        self.statusBar().showMessage("ok")
        self.resize(640, 480)
        self.setWindowTitle("Smart Rss")
        items=[]
        for x in range(0, 200):
            items.append(RssItem("Title no %s" % x, "2000-1-%s" %x))
        self.setCentralWidget(ItemsList(items))
        self.show()

EDIT:Getting closer, changed ItemList.initWidget to

def initWidget(self, items):
    scroll= QScrollArea(self)
    wrap = QWidget(self)
    listBox = QVBoxLayout(self)
    for item in items:
        listBox.addWidget(item)
        listBox.addStretch(1)
    wrap.setLayout(listBox)
    scroll.setWidget(wrap)

But now I cant figure out how to make QScrollArea fill all available space and auto resize when it's changed.

like image 880
Lord_JABA Avatar asked Feb 17 '15 16:02

Lord_JABA


People also ask

How do I make labels scrollable?

Place a panel in location where you want the label to be, set it's AutoScroll property to true. Then place the label in the panel, anchor it and set it's AutoSize property to true. This will make the panel provide the scroll bars if the label's text extends outside of the panel.

What is scroll area in Qt Designer?

A scroll area is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed. The child widget must be specified with setWidget().

What is Qwidget in PyQt?

The widget is the atom of the user interface: it receives mouse, keyboard and other events from the window system, and paints a representation of itself on the screen. Every widget is rectangular, and they are sorted in a Z-order. A widget is clipped by its parent and by the widgets in front of it.

What is QVBoxLayout?

QVBoxLayout organizes your widgets vertically in a window. Instead of organizing all the widgets yourself (specifying the geographic location), you can let PyQt take care of it. Every new widget you add with . addWidget() , is added vertically. Basically you get a vertical list of your widgets.


1 Answers

Try scroll.setWidgetResizable(True) like in here:

def initWidget(self, items):
    listBox = QVBoxLayout(self)
    self.setLayout(listBox)

    scroll = QScrollArea(self)
    listBox.addWidget(scroll)
    scroll.setWidgetResizable(True)
    scrollContent = QWidget(scroll)

    scrollLayout = QVBoxLayout(scrollContent)
    scrollContent.setLayout(scrollLayout)
    for item in items:
        scrollLayout.addWidget(item)
    scroll.setWidget(scrollContent)
like image 120
Alvaro Fuentes Avatar answered Oct 21 '22 05:10

Alvaro Fuentes