如何在QtDesigner中实现一个带有动作(QAction)并能与设计时连接的按钮?

7
我希望使用一种方法,将大部分应用程序命令存储在QActions中,以便我可以轻松地将它们拖放到菜单、工具栏、按钮或其他位置。所以,我需要实现这样一个按钮。
编写一个类来实现它很容易,该类将保存图标、文本、快捷键和工具提示,并将clicked()连接到triggered()。但是,我甚至无法将我的按钮的“action”属性强制添加到设计器中。似乎只有可保持的QVariant类型才能出现在属性编辑器中。
但是,恶意用户已经找到了解决办法,所以这个任务应该是可以完成的。那么,有什么建议吗?
4个回答

9

我不确定,但是我了解您拥有一个使用QtDesigner创建的操作,并且您想将此操作与菜单、工具栏按钮和普通按钮关联起来。

使用QtDesigner,可以将QAction用作菜单项和工具栏按钮。

如果您想将此QAction也用于普通按钮,则我猜您不能仅使用Qt Designer

我的建议是在表单中添加一个QToolButton

在类构造函数中,您可以通过setDefaultAction()告诉QToolButton连接到您的QAction

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

您可能需要相应调整 QToolButton 的几何形状。

现在,如果您单击它,将触发操作 actionHello


当然,我可以将一个操作添加到QWidget并连接适当的信号。但这不是我所需要的。我想找到一种实现一个类的方法,该类将在设计时接受QActions。 - Maxim Popravko
至少,我找到了一个有点愚蠢的解决方法:将 QToolBar 固定在表单底部,并使用自定义样式,将工具栏按钮固定到右侧并将文本放置在图标后面。8\ - Maxim Popravko

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

}

-1
Qt Designer 中,您可以手动添加连接。我认为您可能想使用普通的 PushButton 并将按钮的 clicked() 信号连接到操作的 trigger() 槽上。
假设有一个 pushButton_AddFile 和一个 actionAddFile。您可以在 Qt DesignerSignal/Slot Editor 中添加连接,如下所示:

enter image description here


-2

我认为你需要实现一些特定的拖放操作。 看看QtDesigner是否有其组件的特定Mime-Type。 任何拖放操作都必须以这种方式实现。当然,要做到正确并不是直接的。


这很简单,但如果只是这么简单就好了。在QtDesiger插件机制中,我没有找到非QVariant类型聚合的机制。 - Maxim Popravko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接