我如何实现可以包含操作(QACTION)的按钮,并且可以在qtdesigner中的DesignTime中与一个按钮进行连接?
-
30-09-2019 - |
题
当大多数应用程序命令在Qactions中保存时,我想使用一种方法,以便我可以轻松地将操作拖动到菜单,工具栏或按钮或其他任何内容中。因此,我需要实现这样的按钮。很容易编写一些类,可以容纳它,从中拿出图标,文本,快捷方式和工具提示,然后将clighted()连接到触发()。但是我什至无法将我的按钮的“动作”属性强加给设计师。似乎只有Qvariant可持续类型可能会出现在其属性编辑器中。
但!巨魔以某种方式做到了这一点,因此应该可以完成任务。那么,有什么建议吗?
解决方案
只是为任何寻求类似解决方案的人添加此处
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());
}
其他提示
我不确定,但我知道您有一个行动(与 qtdesigner)您想将此动作与菜单,工具栏按钮和普通按钮相关联。
和 qtdesigner, ,很容易使用 QAction
作为菜单项目和工具栏按钮。
如果您想使用此 QAction
使用普通按钮,我想您不能只使用 QT设计师.
我的建议是添加您的表格 qtdesigner 一种 QToolButton
.
在您的课堂构造函数中,您可以告诉 QToolButton
它与您的联系 QAction
和 setDefaultAction().
ui->toolButton->setDefaultAction(ui->actionHello);
您可能必须相应地调整 QToolButton
.
现在,如果您单击它,则动作 actionHello
与触发。
我认为您必须实施一些特定的掉落。查看QTDESIGNER是否具有其组件的一些特定的MIME类型。任何拖放的东西都必须以这种方式实现。当然,正确正确并不直接;)
不隶属于 StackOverflow