带有文本的 QToolButton:将最小高度覆盖为最小常规按钮高度
QToolButton with text: Overwrite minimal height to minic regular button height
我正在显示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。到目前为止,一切都很好。
不幸的是,一旦您将 QAction 添加到 QToolButton,Qt 就会自动决定将其缩小到最小尺寸,这不是我想要的 QGridLayout。我使用以下行来纠正水平维度中的行为:
QToolButton* pButton = new QToolButton(0);
pButton->addDefaultAction(pAction);
pButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
pButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
然而,这仍然使我的按钮的高度小于常规(按钮)按钮。我已经尝试过各种其他尺寸政策,但均未成功。
如何最好地解决这个问题?有确定 "normal" 按钮高度的可靠方法吗?
我的一个想法是创建一个常规的虚拟按钮,将其放置在相同的布局中,然后读取其大小。然后可以将此大小应用于我的 QToolButton 并且虚拟按钮将再次被销毁。有没有更优雅/可靠的方式?
我不明白你想达到什么目的。
QPushButton
和 QToolButton
之间的区别在于,QToolButton
已经实现了 PopupMenu(对于 QPushButton 也可以轻松完成)
据我了解,当您使用添加 QActions
到 QToolButton
时,视觉差异只是 QToolButton 右下角的小箭头
这个箭头对我来说只是 QToolButton
和 QPushButton
之间的区别。但也许我遗漏了什么。
根据您的示例 ()
看起来您不想使用该弹出功能。这就是为什么我不明白,为什么要使用 QToolButton 而不是 QPushButtons。
在这个例子中显示:
1) QToolButton
和 QPushButton
高度相同
2) QPushButton
的 PopuMenu
至于我,我不明白为什么要使用QToolButton
并尝试使它看起来像QPushButton
,而使用QPushButton
作为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();
}
我正在显示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。到目前为止,一切都很好。
不幸的是,一旦您将 QAction 添加到 QToolButton,Qt 就会自动决定将其缩小到最小尺寸,这不是我想要的 QGridLayout。我使用以下行来纠正水平维度中的行为:
QToolButton* pButton = new QToolButton(0);
pButton->addDefaultAction(pAction);
pButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
pButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
然而,这仍然使我的按钮的高度小于常规(按钮)按钮。我已经尝试过各种其他尺寸政策,但均未成功。
如何最好地解决这个问题?有确定 "normal" 按钮高度的可靠方法吗?
我的一个想法是创建一个常规的虚拟按钮,将其放置在相同的布局中,然后读取其大小。然后可以将此大小应用于我的 QToolButton 并且虚拟按钮将再次被销毁。有没有更优雅/可靠的方式?
我不明白你想达到什么目的。
QPushButton
和 QToolButton
之间的区别在于,QToolButton
已经实现了 PopupMenu(对于 QPushButton 也可以轻松完成)
据我了解,当您使用添加 QActions
到 QToolButton
这个箭头对我来说只是 QToolButton
和 QPushButton
之间的区别。但也许我遗漏了什么。
根据您的示例 (
在这个例子中显示:
1) QToolButton
和 QPushButton
高度相同
2) QPushButton
至于我,我不明白为什么要使用QToolButton
并尝试使它看起来像QPushButton
,而使用QPushButton
作为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();
}