Seite 1 von 1

[gelöst] QMenu / QAction und Konsorten

Verfasst: 4. August 2008 14:07
von ChMaster
Servus zusammen,

ich bräuchte mal wider eure Hilfe. Diesmal geht es bei mir um QMenus und um QActions.

Mein Problem besteht darin das ich ein globales QMenu erzeuge und eine Funktion besitze die mir dieses QMenu zurück gibt. Das hinzufügen des neuen QMenus funktioniert nur das löschen nicht.

Mit getContextMenu(); (eigene Funktion) gebe ich mir das globale QMenu zurück und gehe alle QActions in einer Schleife durch und schaue ob das neue QMenu schon vorhanden ist, wenn ja soll es nicht mehr hinzugefügt werden und genau da liegt mein Problem. Es funktioniert nicht so richtig wie ich es mir vorgestellt habe.

Nun der Quelltext meiner Abgeleiteten Klasse von LDObject:

Code: Alles auswählen

public class LDObjectAccomplishmentPanel extends LDObject
{

    public LDObjectAccomplishmentPanel( QWidget parent, QPoint origin, QPixmap pixmap, String objectId )
    {
        super( parent, origin, pixmap, objectId );
    }

    @Override
    protected void contextMenuEvent( QContextMenuEvent menuEvent )
    {
        List< QAction > newMenuActions = new ArrayList< QAction >();
        List< QAction > globalMenuActions = getContextMenu().actions();
        for( QAction action : globalMenuActions )
        {
            if( action.text().equalsIgnoreCase( tr( "New" ) ) )
            {
                getContextMenu().removeAction( action );
            }

            newMenuActions.add( action );
        }

        getContextMenu().actions().clear();
        getContextMenu().clear();
        getContextMenu().addMenu( tr( "New" ) ).addActions( newMenuActions );
        getContextMenu().addActions( newMenuActions );

        super.contextMenuEvent( menuEvent );
    }
}

Verfasst: 4. August 2008 14:18
von ChMaster
Servus zusammen,

ok, mein Fehler. Ich habe warscheinlich Tomaten vor den Augen gehabt. :D Sorry.

Hier die Lösung:

Code: Alles auswählen

public class LDObjectAccomplishmentPanel extends LDObject
{

    public LDObjectAccomplishmentPanel( QWidget parent, QPoint origin, QPixmap pixmap, String objectId )
    {
        super( parent, origin, pixmap, objectId );
    }

    @Override
    protected void contextMenuEvent( QContextMenuEvent menuEvent )
    {
        List< QAction > newMenuActions = new ArrayList< QAction >();
        List< QAction > globalMenuActions = getContextMenu().actions();
        
        for( Iterator iterator = globalMenuActions.iterator(); iterator.hasNext(); )
        {
            QAction action = (QAction)iterator.next();
            if( !action.text().equalsIgnoreCase( tr( "New" ) ) )
            {
                newMenuActions.add( action );
            }
        }

        getContextMenu().actions().clear();
        getContextMenu().clear();
        getContextMenu().addMenu( tr( "New" ) ).addActions( newMenuActions );
        getContextMenu().addActions( newMenuActions );

        super.contextMenuEvent( menuEvent );
    }
}