c++qttexticonsqtoolbutton

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


I'm using QToolButton and I set the icon. Now I want Text "below the QToolButton", "Not below the icon". Is there any way to achieve this in C++,QT in Linux ?


Solution

  • I've found myself in the same position a while back ago while I was making an application for an embedded Linux system.

    I haven't found a straight forward solution (I was searching for a way to achieve it with CSS).

    What I ended up doing, was creating a new QWidget (using the designer). Then placing the button in it with a QLabel under it.

    Then added a simple static function

    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;
    }
    

    And then add those new QWidgets to your pannel using code (maybe someone knows how to get it in the default toolbar, but I haven't searched for that myself yet since I never needed it).

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

    I hope this might give you an idea to fix it in an easy way!

    Edit: I'm sorry, I forgot to add something about the click event. The click event was mainly why I made a QWidget out of it! I just used the connect function [I belive on the whole button like: connect(this->menuButtons[0], ...]