Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding gap between QIcon and text in a QPushButton
Forum Updated to NodeBB v4.3 + New Features

Adding gap between QIcon and text in a QPushButton

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shreya_agrawal
    wrote on 10 Apr 2024, 09:57 last edited by shreya_agrawal 4 Oct 2024, 10:56
    #1

    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?

    J 1 Reply Last reply 10 Apr 2024, 10:30
    0
    • S shreya_agrawal
      10 Apr 2024, 09:57

      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?

      J Online
      J Online
      JonB
      wrote on 10 Apr 2024, 10:30 last edited by
      #2

      @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?

      S 1 Reply Last reply 10 Apr 2024, 10:48
      0
      • J JonB
        10 Apr 2024, 10:30

        @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?

        S Offline
        S Offline
        shreya_agrawal
        wrote on 10 Apr 2024, 10:48 last edited by
        #3

        @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.

        J 1 Reply Last reply 10 Apr 2024, 11:01
        0
        • S shreya_agrawal
          10 Apr 2024, 10:48

          @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.

          J Online
          J Online
          JonB
          wrote on 10 Apr 2024, 11:01 last edited by
          #4

          @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.

          S 1 Reply Last reply 10 Apr 2024, 11:21
          1
          • J JonB
            10 Apr 2024, 11:01

            @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.

            S Offline
            S Offline
            shreya_agrawal
            wrote on 10 Apr 2024, 11:21 last edited by
            #5

            @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 Reply Last reply
            1
            • S shreya_agrawal has marked this topic as solved on 10 Apr 2024, 12:17

            1/5

            10 Apr 2024, 09:57

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved