QToolButton带有文本:覆盖最小高度以最小化常规按钮的高度 [英] QToolButton with text: Overwrite minimal height to minic regular button height
问题描述
我正在显示 QToolButton ,带有图标加文字( Qt :: ToolButtonTextBesideIcon )工具栏.每个按钮都有一个与之关联的QAction,它确定使用的图标和显示的文本.所有这些按钮都放在 QGridLayout 中.到目前为止,一切都很好.
I am displaying QToolButtons with icon plus text (Qt::ToolButtonTextBesideIcon) outside of a tool bar. Each button has a QAction associated with it which determines the used icon and the displayed text. All those buttons are placed inside a QGridLayout. So far so good.
不幸的是,将QAction添加到QToolButton时,Qt会自动决定将其缩小到最小尺寸,这并不是我想要的QGridLayout.我用以下几行来纠正水平方向的行为:
Unfortunately, it looks like that as soon as you add a QAction to a QToolButton, Qt automatically decides to shrink it down to the minimal size, which is not what I want given my QGridLayout. I used the following lines to correct that behavior in the horizontal dimension:
QToolButton* pButton = new QToolButton(0);
pButton->addDefaultAction(pAction);
pButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
pButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
但是,这仍然使我的按钮比常规(按动)按钮的高度小.我已经尝试过其他各种规模的政策,但没有成功.
However that still leaves my button with a smaller height than a regular (push) button. I already tried various other size policies without success.
如何最好地解决此问题?是否有确定正常"按钮高度的可靠方法?
How to best solve this issue? Is there a reliable way to determine the "normal" button height?
我的一个想法是创建一个常规的虚拟按钮,将其放置在相同的布局中,然后读取其大小.然后可以将这个大小应用于我的QToolButton,并且虚拟按钮将再次被销毁.有没有更优雅/更可靠的方法?
One idea I had was to create a regular dummy button, place it in the same layout and then read its size. This size could then be applied to my QToolButton and the dummy button would be destroyed again. Is there a more elegant / reliable way?
推荐答案
我不明白您要实现什么目标.
I do not understand what do you want to achieve.
QPushButton
和QToolButton
之间的区别在于,QToolButton
实现了PopupMenu(对于QPushButton也可以轻松实现)
据我了解,当您将QActions
添加到QToolButton
Difference between QPushButton
and QToolButton
is that, QToolButton
has implemented PopupMenu ( can be done easily for QPushButton also )
As I understand visual difference is only small arrow in lower right corner of QToolButton, when you use added QActions
to QToolButton
对我来说,此箭头仅是QToolButton
和QPushButton
之间的区别.但是也许我缺少一些东西.
This arrow is for me only difference between QToolButton
and QPushButton
. But maybe I am missing something.
从您的示例中(带有图标+文本的QToolButton:如何将两者居中?) 它看起来像您不想使用该弹出功能.那就是为什么我不明白,为什么要使用QToolButton而不是QPushButtons.
From your examples ( QToolButton with icon + text: How to center both? ) it does not look like you want to use that popup feature. Thats why I do not understand, why to use QToolButton instead of QPushButtons.
在此示例中显示:
1)QToolButton
和QPushButton
的高度相同
2)QPushButton
In this example shows:
1) Same height of QToolButton
and QPushButton
2) PopuMenu for QPushButton
对于我来说,我不明白为什么使用QToolButton
并试图将QPushButton
用作QToolButton
时使它看起来像QPushButton
As for me, I do not understand why to use QToolButton
and try to make it look like QPushButton
when it is simple to use QPushButton
as QToolButton
#include <QtGui>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Prepare layout
QMainWindow *window = new QMainWindow;
QWidget *centralWidget = new QWidget(window);
QGridLayout *grid = new QGridLayout(centralWidget);
QTextEdit *textEdit = new QTextEdit();
window->setCentralWidget(centralWidget);
QAction *toolAction = new QAction(window->style()->standardIcon(QStyle::SP_MediaPlay), "ToolButton", window);
QObject::connect(toolAction, &QAction::triggered, [=]() {
qDebug() << "action";
});
QPushButton *pushButton = new QPushButton(toolAction->icon(), "PushButton1", window);
pushButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QPushButton *pushButton2 = new QPushButton(toolAction->icon(), "PushButton2", window);
pushButton2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QPushButton *pushButton3 = new QPushButton(toolAction->icon(), "PushButton2", window);
pushButton3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
QObject::connect(pushButton3, &QPushButton::released, [window, pushButton3, toolAction](){
QMenu menu;
menu.addAction(toolAction);
QPoint pos = window->mapToGlobal(pushButton3->pos());
pos += QPoint(0, pushButton3->height());
menu.exec(pos);
});
QObject::connect(pushButton, SIGNAL(pressed()), toolAction, SLOT(trigger()));
QObject::connect(pushButton2, SIGNAL(pressed()), toolAction, SLOT(trigger()));
QToolButton *toolButton = new QToolButton(window);
toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolButton->setText("Popup action");
toolButton->addAction(toolAction);
toolButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QToolButton *toolButton2 = new QToolButton(window);
toolButton2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolButton2->setDefaultAction(toolAction);
toolButton2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
toolButton->setMaximumHeight(pushButton->sizeHint().height());
toolButton->setMinimumHeight(pushButton->sizeHint().height());
toolButton2->setMaximumHeight(pushButton->sizeHint().height());
toolButton2->setMinimumHeight(pushButton->sizeHint().height());
grid->addWidget(textEdit ,0,0,1,2);
grid->addWidget(toolButton ,1,0,1,1);
grid->addWidget(pushButton ,1,1,1,1);
grid->addWidget(toolButton2 ,2,0,1,1);
grid->addWidget(pushButton2 ,2,1,1,1);
grid->addWidget(pushButton3 ,3,0,1,2);
window->show();
return a.exec();
}
这篇关于QToolButton带有文本:覆盖最小高度以最小化常规按钮的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!