GLViewWidget won't display in a simple Qt script

1,029 views
Skip to first unread message

Greg Nordin

unread,
Mar 3, 2015, 10:59:28 AM3/3/15
to pyqt...@googlegroups.com
I took the example from http://www.pyqtgraph.org/documentation/qtcrashcourse.html and made 2 modifications: (1) import pyqtgraph.opengl as gl and (2) substituted gl.GLViewWidget for plot instead of pg.plotWidget. However, unlike the original pg.plotWidget, the GLViewWidget doesn't show up in the Qt window when I run the script. After extensive searching, I can't find a reason for this behavior. Can anyone enlighten me as to how to get this to work? My ultimate objective is to embed a GLViewWidget with a 3D data animation in a Qt window that has controls that link to how the 3D data is displayed.

# Example from http://www.pyqtgraph.org/documentation/qtcrashcourse.html

from PyQt4 import QtGui  # (the example applies equally well to PySide)
import pyqtgraph as pg
import pyqtgraph.opengl as gl

## Always start by initializing Qt (only once per application)
app = QtGui.QApplication([])

## Define a top-level widget to hold everything
w = QtGui.QWidget()

## Create some widgets to be placed inside
btn = QtGui.QPushButton('press me')
text = QtGui.QLineEdit('enter text')
listw = QtGui.QListWidget()
plot = gl.GLViewWidget()

## Create a grid layout to manage the widgets size and position
layout = QtGui.QGridLayout()
w.setLayout(layout)

## Add widgets to the layout in their proper positions
layout.addWidget(btn, 0, 0)   # button goes in upper-left
layout.addWidget(text, 1, 0)   # text edit goes in middle-left
layout.addWidget(listw, 2, 0)  # list widget goes in bottom-left
layout.addWidget(plot, 0, 1, 3, 1)  # plot goes on right side, spanning 3 rows

## Display the widget as a new window
w.show()

## Start the Qt event loop
app.exec_()

Luke Campagnola

unread,
Mar 3, 2015, 11:44:44 AM3/3/15
to pyqt...@googlegroups.com
It has something to do with the default size policy for QGLWidget. I've never fully understood Qt's layout system, but in this case just changing the policy to Expanding seems to help (although you will also need to make the window larger):

    plot.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)




--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/22536973-105f-499a-84f8-d407628ed21c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Greg Nordin

unread,
Mar 3, 2015, 12:48:44 PM3/3/15
to pyqt...@googlegroups.com
Thanks, Luke. That helps a lot. I modified the code (see below) and the GLViewWidget shows up (I also added a grid so there's something to see inside the GLViewWidget).

Now there's a new problem. When the following code is run, the GLViewWidget column (Column 1) is the same width as the column containing the button and text & list boxes (Column 0). I would like the GLViewWidget column to be much wider than the other column. I have tried changing the column span (layout.addWidget(plot, 0, 1, 3, 2), but the GLViewWidget then disappears. I have also tried layout.columnStretch(1), but it has no effect on the columnwidth. Any suggestions?

# Example from http://www.pyqtgraph.org/documentation/qtcrashcourse.html

from PyQt4 import QtGui  # (the example applies equally well to PySide)
import pyqtgraph as pg
import pyqtgraph.opengl as gl

## Always start by initializing Qt (only once per application)
app = QtGui.QApplication([])

## Define a top-level widget to hold everything
w = QtGui.QWidget()
w.resize(1000,600)


## Create some widgets to be placed inside
btn = QtGui.QPushButton('press me')
text = QtGui.QLineEdit('enter text')
listw = QtGui.QListWidget()
plot = gl.GLViewWidget()
plot.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
g = gl.GLGridItem()
plot.addItem(g)


## Create a grid layout to manage the widgets size and position
layout = QtGui.QGridLayout()
w.setLayout(layout)

## Add widgets to the layout in their proper positions
layout.addWidget(btn, 0, 0)   # button goes in upper-left
layout.addWidget(text, 1, 0)   # text edit goes in middle-left
layout.addWidget(listw, 2, 0)  # list widget goes in bottom-left
layout.addWidget(plot, 0, 1, 3, 1)  # plot goes on right side, spanning 3 rows

## Display the widget as a new window
w.show()

## Start the Qt event loop
app.exec_()

Greg Nordin

unread,
Mar 3, 2015, 3:38:37 PM3/3/15
to pyqt...@googlegroups.com
After poking around some more I found a solution using the setColumnStretch method of QGridLayout:


# Example from http://www.pyqtgraph.org/documentation/qtcrashcourse.html

from PyQt4 import QtGui  # (the example applies equally well to PySide)
import pyqtgraph as pg
import pyqtgraph.opengl as gl

## Always start by initializing Qt (only once per application)
app = QtGui.QApplication([])

## Define a top-level widget to hold everything
w = QtGui.QWidget()
w.resize(1000,600)

## Create some widgets to be placed inside
btn = QtGui.QPushButton('press me')
text = QtGui.QLineEdit('enter text')
listw = QtGui.QListWidget()
plot = gl.GLViewWidget()
plot.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
g = gl.GLGridItem()
#g.rotate(90,1,0,0)

plot.addItem(g)

## Create a grid layout to manage the widgets size and position
layout = QtGui.QGridLayout()
w.setLayout(layout)
layout.setColumnStretch (1, 2)

Gregoire Mercier

unread,
Jun 8, 2020, 9:08:09 AM6/8/20
to pyqtgraph
I should be enough to use setMinimumSize:

plot.setMinimumSize(10, 15)   # for instance
Reply all
Reply to author
Forward
0 new messages