[PyKDE] context menu in QHeader

Andrew Dalke adalke at mindspring.com
Wed Apr 17 14:14:13 BST 2002


Hello,

  I'm evaluating the different Python GUI toolkits (Qt, GTK, Tkinter,
and wxWindows) for a project I'm working on.  I have a set of widgets
I'm implementing as test cases.  I've been impressed with Qt so far,
but I'm stuck right now because I can't figure out how to do a context
menu for a column in a QHeader, as part of a QTable.

  Here's the details of what I'm doing and what I'm looking for. 
BTW, this is with the most recent everything (Python, free QT/X, sip,
PyQT), which I just installed this evening.

  My first test widget is to show a spreadsheet containing compound
data (compounds == small molecules, as for drug research) from a
database search.  There can be up to a million results in the search.
Molecules have many properties - weight, CLogP, total charge,
commercial names (which is a list of possible values), CAS # (if
available; this is a unique identifier assigned by a third party), etc.

  Because of the number of compounds, it seems that QGrid is the
right widget to use.  QListView seems to want me to instantiate an
object for each cell, while QGrid allows me to have a single handler
for everything, if I read it correctly.

  I want to show an arbitrary number of columns.  The first column
always contains the row number.  Again, this is easy in a QGrid.

  The second column contains the molecule, either as a "SMILES" string
(which is a unique name determined by the chemical structure) or as a
picture of the compound.  I can generate the image by writing to a
QCanvas.

  I want to toggle between showing the string and showing the image.
I want to do this for the column as a whole, using a context menu
on the column header.  I can't figure out how to do this.

  There's a handy contextMenuRequested signal, but that only works
for cells.  It doesn't work for the horizontal or vertical headers.
Nor do the other mouse signals in QTable.

  If I get the QHeader, it has a 'pressed(int section)' signal, but
that doesn't support "context" mouse presses, only "left" button
pressed.

  I find it hard to believe I'm the only one who wants this
functionality.  I also want people to be able to insert or remove
columns from the table, and do so by doing a context click on the
header and seeing a popup like

   Insert mw
   Insert CLogP
   Insert --> submenus
   ------
   Delete

Using an 'Insert' option adds a new column after the current column
and containing the appropriate data (which might be a QComboTableItem
for fields containing a list).

I first saw this feature on NeXTStep about 8 years ago, so I know
it isn't anything new.

That all said, how do I support creating a context pop-up menu from
the QHeader?

                    Andrew
                    dalke at dalkescientific.com







More information about the PyQt mailing list