4

Is it possible to nest a QComboBox inside of a Qmenu within QGIS?

Below is my current code and an associated screenshot with the unexpected results.

(orange = current incorrect placement, green = ideal placement)

# CSV attribute editor toolbox layer population(s)
self.projCombo = QComboBox(self.iface.mainWindow())
self.projCombo.addItem('example slot')
self.projCombo.addItem('another example slot')
self.projCombo.addItem('the last example slot')
self.ShapeLayerList = QWidgetAction(self.projCombo)

# build the sub-menus for CSV attribute editor
self.popupMenu2A = QMenu( self.iface.mainWindow() )
self.popupMenu2A.setTitle("Shape Data")
self.popupMenu2A.addAction(self.ShapeLayerList)
self.toolButton2A = QToolButton()
self.toolButton2A.setMenu( self.popupMenu2A )
self.toolButton2A.setDefaultAction( self.editAttribStress )
self.toolButton2A.setPopupMode( QToolButton.InstantPopup )

self.popupMenu2B = QMenu( self.iface.mainWindow() )
self.toolButton2B = QToolButton()
self.toolButton2B.setMenu( self.popupMenu2B )
self.toolButton2B.setDefaultAction( self.editAttribProp)
self.toolButton2B.setPopupMode( QToolButton.InstantPopup )
self.popupMenu2B.setTitle("Grid Data")
self.popupMenu2B.addAction( self.editAttribProp )

# build the main CSV attribute editor dropdown toolbutton
self.popupMenu2 = QMenu( self.iface.mainWindow() )
self.toolButton2 = QToolButton()
self.toolButton2.setMenu( self.popupMenu2 )
self.toolButton2.setDefaultAction( self.editAttributes )
self.toolButton2.setPopupMode( QToolButton.InstantPopup )
self.popupMenu2.addMenu(self.popupMenu2A)
self.popupMenu2.addMenu(self.popupMenu2B)

combobox location?

5

Your problem is with

self.ShapeLayerList = QWidgetAction(self.projCombo) 

That is creating a QWidgetAction with the combo box as its parent widget, not its child widget. Try:

self.ShapeLayerList=QWidgetAction(None)
self.ShapeLayerList.setDefaultWidget(self.projCombo)
  • Error return: 'TypeError: QWidgetAction(QObject): not enough arguments' . Although your parent-child notion makes sense... – Katalpa Dec 23 '14 at 21:47
  • Oops - sorry. That should have been QWidgetAction(None) instead. I've edited to reflect this. – ndawson Dec 23 '14 at 22:47
  • No worries. Is the manner of adding an 'empty' QObject rather then a 'None Type' also reasonable (and pythonic)? – Katalpa Dec 24 '14 at 12:24
2

Thanks to ndawson for his advice; direction in the right direction:

Had to implement a test QObject to fufill the parent-child relations...

        # CSV attribute editor toolbox layer population(s)
    self.projCombo = QComboBox(self.iface.mainWindow())
    self.projCombo.addItem('example slot')
    self.projCombo.addItem('another example slot')
    self.projCombo.addItem('the last example slot')
    !!! > self.objectTest = QObject()
    !!! > self.ShapeLayerList = QWidgetAction(self.objectTest)
    self.ShapeLayerList.setDefaultWidget(self.projCombo)

    # build the sub-menus for CSV attribute editor
    self.popupMenu2A = QMenu( self.iface.mainWindow() )
    self.popupMenu2A.setTitle("Shape Data")
    self.popupMenu2A.addAction(self.ShapeLayerList)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.