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. Show only Icon for push button without text[solved]
Forum Updated to NodeBB v4.3 + New Features

Show only Icon for push button without text[solved]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 13.0k Views 1 Watching
  • 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.
  • V Offline
    V Offline
    vivek.narvekar
    wrote on 20 Feb 2012, 07:25 last edited by
    #1

    Hello,

    When I set icon to a push button and also set text using setText, It shows text as well as icon.

    Is there a way, by which, I can show only the icon (when it is set), otherwise text, when icon is not set? this was happening automatically in Qt3..... I am trying to port Qt3 code to Qt 4......

    Thanks for any help

    Regards
    Vivek

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 20 Feb 2012, 08:27 last edited by
      #2

      I think (but please be sure to check) that that is the behaviour of [[doc:QToolButton]]. At least, it provides a greater level of control over this than QPushButton.

      1 Reply Last reply
      1
      • V Offline
        V Offline
        vivek.narvekar
        wrote on 20 Feb 2012, 10:00 last edited by
        #3

        Thanks a lot Andre for responding.

        I was hoping that there would be some style setting or something in pushbutton, that would solve this. Now the problem is that we have used QPushButton extensively, hence it wont be a straight forward task to replace QpushButton with QtoolButton.

        anyways, thanks a lot for your valuable suggestion.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 20 Feb 2012, 11:00 last edited by
          #4

          In that case, perhaps a [[doc:QProxyStyle]] can be of use to you?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mabrouk
            wrote on 20 Feb 2012, 11:18 last edited by
            #5

            a work around for this is just set the icon size to the button size and don't set a text for the button and you'll get the result you want

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vivek.narvekar
              wrote on 20 Feb 2012, 11:31 last edited by
              #6

              Thanks Andre and Mabrouk

              Andre - I am looking into QProxyStyle, and see if it can be of use, in this case

              Mabrouk - eliminating text setting is a difficult task here, because of the code architecture.

              Edit: removed @ characters, as the forum software interprets those as tags surrounding code sections; Andre

              1 Reply Last reply
              0
              • V Offline
                V Offline
                vivek.narvekar
                wrote on 22 May 2012, 06:51 last edited by
                #7

                Hello Andre and Mabrouk,

                I managed to solve this problem by using QStyleOptionButton, as follows

                @if(!icon ().isNull ()){
                QStyleOptionButton option;
                initStyleOption(&option);
                option.text = QString(""); // set the empty string so that only icon is drawn and not the text, when we specify both icon and text.
                if(m_mouseOverButton == true){
                option.icon = icon().pixmap ( iconSize(), QIcon::Active);
                }else{
                option.icon = icon().pixmap ( iconSize(), QIcon::Normal);
                }
                QPainter painter(this);
                style()->drawControl(QStyle::CE_PushButton, &option, &painter, this);
                //event->ignore ();
                }else{
                QPushButton::paintEvent (event);
                }@

                Thus when the icon is present, set the option text to empty string. This way, the original string is preserved.

                Thanks a lot for all the help...

                1 Reply Last reply
                0

                • Login

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