7

ほとんどのアプリケーションコマンドがQActionsに保持されている場合は、アプローチを使用して、アクションをメニュー、ツールバー、ボタンなどに簡単にドラッグできるようにします。だから、私はそのようなボタンを実装する必要があります。クラスを作成するのは簡単です。クラスを保持し、アイコン、テキスト、ショートカット、ツールチップを取得して、clicked()をtriggered()に接続します。しかし、ボタンの「アクション」プロパティをデザイナーに強制することさえできません。QVariantの保持可能なタイプのみがプロパティエディタに表示される可能性があるようです。

しかし!トロルはどういうわけかそれをしたので、タスクは達成可能であるはずです。それで、何か提案はありますか?

4

4 に答える 4

9

よくわかりませんが、アクション(QtDesignerで作成)があり、このアクションをメニュー、ツールバーボタン、および通常のボタンにも関連付けたいと考えています。

QtDesignerを使用すると、をメニュー項目およびツールバーボタンとして簡単に使用できます。QAction

これを通常のボタンでも使いたいのなら、 QtDesignerQActionだけではできないと思います。

私の提案は、QtDesigneraを使用してフォームに追加することQToolButtonです。

クラスコンストラクターでは、 setDefaultAction()を使用しQToolButtonて接続されていることを通知できます。QAction

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

それに応じて、のジオメトリを調整する必要がある場合がありQToolButtonます。

これで、クリックすると、アクションactionHelloがトリガーされます。

于 2010-11-11T11:14:29.853 に答える
5

同様の解決策を探している人のためにここに追加するだけです

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

ヘッダ

#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

クラス

#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());

}
于 2014-08-24T15:29:53.647 に答える
-1

Qt Designerでは、手動で接続を追加できます。ノーマルPushButtonを使用して、ボタンのclicked()信号をアクションのtrigger()スロットに接続することをお勧めします。

pushButton_AddFileとがあるとしましょうactionAddFile。次のようSignal/Slot Editorに、QtDesignerのに接続を追加できます。

ここに画像の説明を入力してください

于 2018-05-03T06:22:25.783 に答える
-2

特定のドロップアクションを実装する必要があると思います。QtDesignerのコンポーネントに特定のMimeタイプがあるかどうかを確認してください。ドラッグアンドドロップであるものはすべて、この方法で実装する必要があります。もちろん、それを正しく理解するのは簡単ではありません;)

于 2010-11-17T18:53:59.570 に答える