Adding gap between QIcon and text in a QPushButton
-
wrote on 10 Apr 2024, 09:57 last edited by shreya_agrawal 4 Oct 2024, 10:56
I have a QPushButton, in which I am adding a QIcon and some text. How can I add space between the icon and the text without using a spacer or changing the size of the icon. I tried adding padding, but it isn't giving me desired results.
I also considered adding the icon and the text inside two different labels and then adding those two labels in a QHBoxLayout and adding spacing in it and then finally setting this layout as the button layout, but this would be a complex solution. Is there a better way of doing this? -
I have a QPushButton, in which I am adding a QIcon and some text. How can I add space between the icon and the text without using a spacer or changing the size of the icon. I tried adding padding, but it isn't giving me desired results.
I also considered adding the icon and the text inside two different labels and then adding those two labels in a QHBoxLayout and adding spacing in it and then finally setting this layout as the button layout, but this would be a complex solution. Is there a better way of doing this?wrote on 10 Apr 2024, 10:30 last edited by@shreya_agrawal
Padding is inside whatever widget, likely to either change its size or that of its content. I don't know whether it would work, but did you try margin instead, which is outside of widget? -
@shreya_agrawal
Padding is inside whatever widget, likely to either change its size or that of its content. I don't know whether it would work, but did you try margin instead, which is outside of widget?wrote on 10 Apr 2024, 10:48 last edited by@JonB
Thank you for your reply!
Then, we will have to give margin to the icon, since we need space between the icon and the text, but margin can't be directly given to a QIcon. -
@JonB
Thank you for your reply!
Then, we will have to give margin to the icon, since we need space between the icon and the text, but margin can't be directly given to a QIcon.wrote on 10 Apr 2024, 11:01 last edited by@shreya_agrawal
I think you are right, I did not look into it, I thought one could set margin on the icon, but not.https://stackoverflow.com/questions/17345648/how-to-change-qpushbutton-icon-text-spacing-with-stylesheets confirms how difficult it is, but offers a number of possibilities. From doing the paint yourself to putting a space at start of text.
-
@shreya_agrawal
I think you are right, I did not look into it, I thought one could set margin on the icon, but not.https://stackoverflow.com/questions/17345648/how-to-change-qpushbutton-icon-text-spacing-with-stylesheets confirms how difficult it is, but offers a number of possibilities. From doing the paint yourself to putting a space at start of text.
wrote on 10 Apr 2024, 11:21 last edited by@JonB
Yes, both approaches do work. I think I will I will just put blank space at start of the text to avoid unnecessary complexity. -
1/5