Qt QPushbutton Icon above Text

前端 未结 3 469
借酒劲吻你
借酒劲吻你 2021-01-03 19:16

When I create a QPushButton with an Icon it by default displays the text to the right of the icon. Is there a way to have the text display underneath the icon?

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 19:59

    If you're able to, the easiest thing to do is use a QToolButton instead:

    QToolButton* button = new QToolButton(this);
    button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    button->setIcon(myIcon);
    button->setText("Sample text");
    

    If that's not an option you could consider creating your own button widget, possibly derived from QPushButton or QAbstractButton. In this case you'll probably (I haven't tried it myself) want to focus most of your efforts on reimplementing paintEvent().

    [Edit: read the comments for alternatives which are probably way simpler than this]

提交回复
热议问题