Question

I'm trying to build myself a simple graphics calculator, as a way of teaching myself PyQt. I'd like the calculator to have a pane which lists all of the equations plotted and shows the line style used.

I began with a QListWidget to achieve these ends, the pane contains a scrollable list of equations, but does not show the line style because the QListWidget only allows strings or icons.

So I tried using a QAbstractScrollArea widget instead. For each equation I use QHBoxLayout to produce two Widgets, a label containing the equation string, and a QWidget within which I draw the line style. Then I stack all of the equation QHBoxLayouts within the ScrollArea using QVBoxLayout.

The problem is that QVBoxLayout uses all of the space available to it. So if I have only three equations, they are spread throughout the pane and not listed at the top as I'd like them to be, while if I have too many equations to fit in the pane they are stacked on top of one another rather than causing the area to be scrollable.

This is how the calculator appears with too many equations...

My Graphics Calculator with too many equations.

And this is how it looks with too few...

My Graphics Calculator with too few equations.

Does anyone have any suggestions of better ways to get around these issues? One idea is to generate icons programmatically to have the properties of the lines and to use these in the listwidget, is that possible?

Was it helpful?

Solution

It sounds like what you want is a QListWidget with multiple columns - and a QTreeWidget can be adapted to do exactly that.

Here's a simple demo:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.list = QtGui.QTreeWidget(self)
        self.list.setRootIsDecorated(False)
        self.list.setUniformRowHeights(True)
        self.list.setAllColumnsShowFocus(True)
        self.list.setItemsExpandable(False)
        self.list.header().hide()
        self.list.setColumnCount(2)
        for label, color in (
            ('v=x**2', 'red'),
            ('v=x/2', 'blue'),
            ('v=2*x', 'green'),
            ('v=3*2', 'orange'),
            ('v=5-x', 'purple'),
            ):
            item = QtGui.QTreeWidgetItem([label, '------------'])
            item.setForeground(1, QtGui.QColor(color))
            self.list.addTopLevelItem(item)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.list)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 300, 200)
    window.show()
    sys.exit(app.exec_())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top