[gelöst] QMenu / QAction und Konsorten

Alles zum Qt Framework für Java
Antworten
ChMaster
Beiträge: 252
Registriert: 23. Februar 2005 14:44
Wohnort: RP -> Alzey
Kontaktdaten:

[gelöst] QMenu / QAction und Konsorten

Beitrag 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 );
    }
}
Zuletzt geändert von ChMaster am 4. August 2008 14:19, insgesamt 2-mal geändert.
ChMaster
------------ Projekte------------
DBoxFE
DMS
First4 (Plugin-Develper)
ChMaster
Beiträge: 252
Registriert: 23. Februar 2005 14:44
Wohnort: RP -> Alzey
Kontaktdaten:

Beitrag 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 );
    }
}
ChMaster
------------ Projekte------------
DBoxFE
DMS
First4 (Plugin-Develper)
Antworten