带有图标 + 文本的 QToolButton:如何将两者居中? [英] QToolButton with icon + text: How to center both?

查看:230
本文介绍了带有图标 + 文本的 QToolButton:如何将两者居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 ,但它似乎具有侵入性,我还不是很清楚:

<块引用>

您可以尝试使用样式表进行水平对齐,但您可能必须实现 QStyle 代理并为 QStyle::CE_ToolButtonLabel 重新实现 drawControl()或者从 QToolButton 派生,覆盖paintEvent() 并为标签以外的所有内容调用样式.

解决方案

正如我在回答您的另一个问题时所建议的那样.https://stackoverflow.com/a/28630318/1917249不要使用QToolButton,只使用QPushButton,并添加弹出菜单如果需要.

那么你不会有不同大小的 QToolButtonQPushButton 小部件.您将拥有居中的图标和文本.

弹出菜单可以很容易地添加到 QPushButton(只有小箭头不会显示)

QPushButton *pushButton = new QPushButton(toolAction->icon(), "PushButton", window);//窗口 - 放置按钮的小部件(以获得正确的 QMenu 位置)QObject::connect(pushButton, &QPushButton::released, [window, pushButton, action](){QMenu 菜单;menu.addAction(action);QPoint pos = window->mapToGlobal(pushButton3->pos());pos += QPoint(0, pushButton->height());menu.exec(pos);});

或者你可以子类化 QPushButton 并在那里添加弹出菜单处理.更好的是尝试将文本与 QToolButton 中的图标居中或具有相同大小的 QPushButtonQToolButton

复杂的例子请看我的回答:https://stackoverflow.com/a/28630318/1917249>

I am using multiple QToolButtons in a custom QGridLayout widget. The buttons are set to display icon + text based on an assigned default QAction. The only issue is that the content (icon + text) is always left-aligned.

The content (icon + text, marked as a red box in the screenshot), should be center in the button (indicated by the blue box).

For most cases this is just fine, given that Qt automatically tries to render that button with the minimal size. However I am stretching the button to fit nicely into my QGridLayout.

QToolButton* pButton = new QToolButton(0);
pButton->addDefaultAction(pAction);
pButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
pButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

QGridLayout *pActionAreaLayout = new QGridLayout;
pActionAreaLayout->addWidget(pSomeOtherWidget, 0, 0, 1, 2);
pActionAreaLayout->addWidget(pButton , 1, 0, 1, 1);

Is there a way to force the content to be centered in the button?

PS: I found the following comment in another forum, which however seems quite invasive and is not really clear to me yet:

You can try doing the horizontal alignment using a stylesheet, but you probably have to implement a QStyle proxy and reimplement drawControl() for QStyle::CE_ToolButtonLabel Or derive from QToolButton, overwrite paintEvent() and call the style for everything other than the label.

解决方案

As I suggest in answer to you another question. https://stackoverflow.com/a/28630318/1917249 Do not use QToolButton, just use QPushButton, and add popup menu if needed.

Then you wont have different sizes of QToolButton and QPushButton widgets. And you will have centered icon and text.

Popupmenu can be easily added to QPushButton ( only small arrow wont be shown )

QPushButton *pushButton = new QPushButton(toolAction->icon(), "PushButton", window);
// window - widget where button is placed ( to get correct QMenu position )
QObject::connect(pushButton, &QPushButton::released, [window, pushButton, action](){
    QMenu menu;
    menu.addAction(action);
    QPoint pos = window->mapToGlobal(pushButton3->pos());
    pos += QPoint(0, pushButton->height());
    menu.exec(pos);
 });

Or you can subclass QPushButton and add Popup menu handling there. Much better then try to center text with icon in QToolButton or have in same size of QPushButton and QToolButton

For complex example please see my answer: https://stackoverflow.com/a/28630318/1917249

这篇关于带有图标 + 文本的 QToolButton:如何将两者居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆