Qt World Summit: Register Today!

[Solved] QPushButtons with both text & icons

  • I have a Designer UI Form that has about twenty QPushButtons. Each one of the buttons contains a unique icon as well as some text. The icons are all the same size, but the text is different for each button; as a result, the icons are at different positions in the QPushButton.

    To fix this, I added a number of leading spaces to each text string to get the icons to be in the same position in the QPushButton. That works fine, but now my application is being translated into other languages. Obviously the size of the text string is going to change, and I'll be back to the original problem of having icons be in a different position in each QPushButton. How can I retain the equal spacing?

  • Hi there,
    Maybe put a picture with it, how are the buttons organized? How are the arranged? Where do you want the icon to appear and where the text?
    It might be an idea to use the grid layout for al the buttons and have them all stretched? When the layout changes size, so will all the buttons.

  • There are 3 columns of 8, 3 and 5 buttons respectively. Each button has a width of 254 and a height of 50. By default, if you put an icon and text in a QPushButton, the icon shows up to the left of the text.

    This on an embedded Linux system where the application is running fullscreen, so the screen will never be resized.

  • how about subclassing QPushButton to implement your own drawing ?
    override paintEvent() to draw your icon and text at any position you want.
    text's full width/height can be acquired by using QFontMetrics.
    and icon size could be found by QPushButton's iconSize() as you know.

  • Could you not use some CSS to manage the styling of the buttons? I have done something similar with Qtoolbuttons

Log in to reply