Wie kann ich den Knopf implementieren, die eine Aktion (QAction) hält und mit einem in Entwurfszeit in QtDesigner herstellen kann?

StackOverflow https://stackoverflow.com/questions/4149117

  •  30-09-2019
  •  | 
  •  

Frage

Ich mag einen Ansatz verwenden, wenn die meisten Anwendungsbefehle in QActions halten, so dass ich Aktionen leicht in Menü ziehen könnte, oder in der Symbolleiste oder Taste, oder irgendetwas. Also, ich brauche eine solche Taste implementieren. Es ist leicht, eine Klasse zu schreiben, die sie halten, nehmen Sie Symbol, Text, Verknüpfung und Tooltip von ihm und verbinden geklickt (), um ausgelöst (). Aber ich kann nicht einmal meine Schaltfläche ‚Aktion‘ Eigenschaft in Designer zwingen. Es scheint, dass nur QVariant haltbare Typen in seiner Eigenschaftseditor angezeigt werden.

ABER! Troll tat es irgendwie, so dass die Aufgabe erreichbar sein sollte. Also, irgendwelche Vorschläge?

War es hilfreich?

Lösung

Sie hier für jedermann Zugabe für ähnliche Lösung suchen

https://qt-project.org/wiki/PushButton_Based_On_Action

Header

#ifndef ACTIONBUTTON_H
#define ACTIONBUTTON_H

#include <QPushButton>
#include <QAction>

/*!
  *\brief An extension of a QPushButton that supports QAction.
  * This class represents a QPushButton extension that can be
  * connected to an action and that configures itself depending
  * on the status of the action.
  * When the action changes its state, the button reflects
  * such changes, and when the button is clicked the action
  * is triggered.
  */
class ActionButton : public QPushButton
{
    Q_OBJECT

private:

    /*!
      * The action associated to this button.
      */
    QAction* actionOwner;


public:
    /*!
      * Default constructor.
      * \param parent the widget parent of this button
      */
    explicit ActionButton(QWidget *parent = 0);

    /*!
      * Sets the action owner of this button, that is the action
      * associated to the button. The button is configured immediatly
      * depending on the action status and the button and the action
      * are connected together so that when the action is changed the button
      * is updated and when the button is clicked the action is triggered.
      * \param action the action to associate to this button
      */
    void setAction( QAction* action );


signals:

public slots:
    /*!
      * A slot to update the button status depending on a change
      * on the action status. This slot is invoked each time the action
      * "changed" signal is emitted.
      */
    void updateButtonStatusFromAction();


};

#endif // ACTIONBUTTON_H

Klasse

#include "actionbutton.h"

ActionButton::ActionButton(QWidget *parent) :
    QPushButton(parent)
{
    actionOwner = NULL;
}

void ActionButton::setAction(QAction *action)
{

    // if I've got already an action associated to the button
    // remove all connections

    if( actionOwner != NULL && actionOwner != action ){
        disconnect( actionOwner,
                    SIGNAL(changed()),
                    this,
                    SLOT(updateButtonStatusFromAction()) );

        disconnect( this,
                    SIGNAL(clicked()),
                    actionOwner,
                    SLOT(trigger()) );
    }


    // store the action
    actionOwner = action;

    // configure the button
    updateButtonStatusFromAction();



    // connect the action and the button
    // so that when the action is changed the
    // button is changed too!
    connect( action,
             SIGNAL(changed()),
             this,
             SLOT(updateButtonStatusFromAction()));

    // connect the button to the slot that forwards the
    // signal to the action
    connect( this,
             SIGNAL(clicked()),
             actionOwner,
             SLOT(trigger()) );
}

void ActionButton::updateButtonStatusFromAction()
{
    setText( actionOwner->text() );
    setStatusTip( actionOwner->statusTip() );
    setToolTip( actionOwner->toolTip() );
    setIcon( actionOwner->icon() );
    setEnabled( actionOwner->isEnabled() );
    setCheckable( actionOwner->isCheckable() );
    setChecked( actionOwner->isChecked());

}

Andere Tipps

Ich bin nicht sicher, aber ich verstehe, dass Sie eine Aktion haben (erstellt mit QtDesigner ), und Sie wollen diese Aktion mit einem Menü verknüpfen, eine Schaltfläche in der Symbolleiste, und eine normale Taste sowie .

Mit QtDesigner , ist es einfach, ein QAction als Menüpunkt zu verwenden und als Schaltfläche in der Symbolleiste.

Wenn Sie auch dieses QAction mit einer normalen Taste verwenden möchten, ich denke, man kann es nicht nur mit Qt Designer .

Mein Vorschlag ist, auf dem Formular an, mit QtDesigner einem QToolButton.

Sie Klassenkonstruktor können Sie dann die QToolButton sagen, dass es mit QAction verbunden ist noreferrer "> setDefaultAction () .

ui->toolButton->setDefaultAction(ui->actionHello);

Sie können entsprechend der Geometrie des QToolButton anpassen müssen.

Nun, wenn Sie darauf klicken, wird die Aktion actionHello mit ausgelöst werden.

In Qt Designer Sie können Verbindungen manuell hinzufügen. Ich glaube, Sie könnten ein normales PushButton verwenden mögen, und die clicked() Signal an die Aktion der trigger() Slot der Schaltfläche verbinden.

Lassen Sie uns sagen, dass es ein pushButton_AddFile und ein actionAddFile. Sie können die Verbindung in der Signal/Slot Editor von Qt Designer hinzufügen wie folgt:

Ich glaube, Sie haben einige spezielle Drop-Aktion zu implementieren. Sehen Sie, wenn QtDesigner einige spezifische Mime-Type für seine Komponenten. Alles, was per Drag & Drop ist, muss auf diese Weise umgesetzt werden. Natürlich nicht einfach ist, es richtig zu machen;)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top