1

我在工具栏外显示带有图标加文本(Qt::ToolButtonTextBesideIcon )的QToolButton 。每个按钮都有一个与之关联的 QAction,它决定了使用的图标和显示的文本。所有这些按钮都放置在QGridLayout中。到目前为止,一切都很好。

不幸的是,一旦您将 QAction 添加到 QToolButton,Qt 就会自动决定将其缩小到最小尺寸,这不是我想要的 QGridLayout。我使用以下几行来纠正水平维度中的这种行为:

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

但是,这仍然使我的按钮的高度小于常规(按钮)按钮。我已经尝试过各种其他尺寸政策,但均未成功。

如何最好地解决这个问题?是否有可靠的方法来确定“正常”按钮高度?

我的一个想法是创建一个普通的虚拟按钮,将它放在相同的布局中,然后读取它的大小。然后可以将此大小应用于我的 QToolButton 并且虚拟按钮将再次被销毁。有没有更优雅/可靠的方式?

4

1 回答 1

1

我不明白你想要达到什么目的。

QPushButton和之间的区别在于QToolButtonQToolButton已经实现了 PopupMenu(也可以为 QPushButton 轻松完成)据我所知,视觉差异只是 QToolButton 右下角的小箭头,当您使用添加QActionsQToolButton

QToolButton这个箭头对我来说只是和之间的区别QPushButton。但也许我错过了一些东西。

从您的示例(带有图标 + 文本的 QToolButton:如何将两者居中?)看起来您不想使用该弹出功能。这就是为什么我不明白,为什么要使用 QToolButton 而不是 QPushButtons。

在此示例中显示:1)相同的高度QToolButtonQPushButton 2)PopuMenu 为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();
}
于 2015-02-20T13:38:21.313 回答