QToolButton with text: Overwrite minimal height to minic regular button height

爱⌒轻易说出口 提交于 2019-12-24 18:00:54

问题


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.

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?

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?


回答1:


I do not understand what do you want to achieve.

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

This arrow is for me only difference between QToolButton and QPushButton. But maybe I am missing something.

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.

In this example shows: 1) Same height of QToolButton and QPushButton 2) PopuMenu for 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();
}


来源:https://stackoverflow.com/questions/28515953/qtoolbutton-with-text-overwrite-minimal-height-to-minic-regular-button-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!