7

当大多数应用程序命令都包含在 QActions 中时,我想使用一种方法,以便我可以轻松地将操作拖到菜单、工具栏、按钮或任何东西中。所以,我需要实现这样一个按钮。编写一些类很容易,它将保存它,从中获取图标、文本、快捷方式和工具提示,并将 clicked() 连接到 trigger()。但我什至不能将按钮的“动作”属性强加给设计器。似乎只有 QVariant 可持有类型可能出现在它的属性编辑器中。

但!巨魔以某种方式做到了,所以任务应该是可以完成的。那么,有什么建议吗?

4

4 回答 4

9

我不确定,但我知道您有一个操作(使用QtDesigner创建)并且您希望将此操作与菜单、工具栏按钮和普通按钮相关联。

使用QtDesigner,很容易将 aQAction用作菜单项和工具栏按钮。

如果您也想将其QAction与普通按钮一起使用,我想您不能仅使用Qt Designer来做到这一点。

我的建议是在您的表单上添加QtDesigner a 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。您可以在 Qt Designer 中添加连接,Signal/Slot Editor如下所示

在此处输入图像描述

于 2018-05-03T06:22:25.783 回答
-2

我认为您必须实施一些特定的放置动作。看看 QtDesigner 的组件是否有一些特定的 Mime-Type。任何拖放操作都必须以这种方式实现。当然,要做到这一点并不简单;)

于 2010-11-17T18:53:59.570 回答