如何设置文本下面的QToolButton在QT不低于图标

How to set text below the QToolButton in QT not below the icon

本文关键字:QToolButton QT 不低于 图标 置文本      更新时间:2023-10-16

我使用QToolButton并设置图标。现在我想要文本"below the QToolButton", "Not below the icon"。有没有办法在c++中实现这一点,QT在Linux中?

我发现自己在不久前为嵌入式Linux系统开发应用程序时也处于同样的位置。

我还没有找到一个直接的解决方案(我正在寻找一种用CSS实现它的方法)。

我最后做的是创建一个新的QWidget(使用设计器)。然后将按钮放入其中,并在其下方放置QLabel。

然后添加一个简单的静态函数
static void wdgCustomButton::create(const QString iconPath, const QString text)
{
    // create a new button here, create some modification functions for
    // text, image and optionally QStyleSheets.
    // Call those here (pass the arguments)
    // Then return the button
    // pseudo code, (not tested):
    wdgCustomButton button = new wdgCustomButton( /* could pass a parent */ );
    button->setIcon( iconPath ); // function simply calls the ui->button->setIcon
    button->setText( text );     // function simply calls the ui->label->setText 
    return button;
}

然后使用代码将这些新的QWidgets添加到您的面板(也许有人知道如何在默认工具栏中获得它,但我自己还没有搜索过,因为我从来没有需要过它)。

this->menuButtons[menuBtnsCount] = wdgCustomButton::create( ":/Images/Warning.png", "Delete everything" );            
this->menuButtons[menuBtnsCount]->setGeometry( QRect( /* size and position here */ ) );
this->menuButtons[menuBtnsCount]->show();

我希望这可能会给你一个简单的方法来修复它的想法!

编辑:

抱歉,我忘了添加click事件。点击事件是我制作QWidget的主要原因!我只是使用了连接功能[我相信整个按钮像:connect(this->menuButtons[0],…]