如何在QtDesigner中实现具有动作(QAction)并且可以在设计时连接的按钮?

Max*_*vko 7 qt qt-designer

我希望使用一种方法,当大多数应用程序命令保存在QActions中时,我可以轻松地将操作拖动到菜单,工具栏,按钮或任何其他内容中.所以,我需要实现这样一个按钮.编写一些类很容易,它会保存它,从中获取图标,文本,快捷方式和工具提示,并将clicked()连接到triggered().但我甚至无法将按钮的"动作"属性强加给设计师.似乎只有QVariant可保持类型可能出现在它的属性编辑器中.

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

Jér*_*ôme 8

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

使用QtDesigner,可以很容易地将a QAction用作菜单项和工具栏按钮.

如果你想将它QAction与普通按钮一起使用,我想你不能只用Qt Designer来做.

我的建议是在你的表单上加上QtDesigner a QToolButton.

在您的类构造函数中,您可以告诉QToolButtonQAction使用setDefaultAction()连接到您的类.

ui->toolButton->setDefaultAction(ui->actionHello);
Run Code Online (Sandbox Code Playgroud)

您可能需要相应地调整几何体QToolButton.

现在,如果单击它,则actionHello触发操作.


Yas*_*ash 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
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)