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. Prevent flat QToolButton from moving when clicked
Forum Updated to NodeBB v4.3 + New Features

Prevent flat QToolButton from moving when clicked

Scheduled Pinned Locked Moved General and Desktop
12 Posts 8 Posters 7.8k Views 2 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.
  • D Offline
    D Offline
    Deusdies
    wrote on last edited by
    #3

    Thanks - but I need it to be checkable, but not move at the same time. Is this possible?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #4

      So, when you say you want it to be checkable but not move, are you anticipating just a color change, or what?

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Deusdies
        wrote on last edited by
        #5

        Correct, just a color change (which I have accomplished with CSS). I would use a regular button and just make it flat, but I cannot align the button label under the icon and have it centered.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #6

          -Would it be feasible for you to just use different images for your button in the different states? That way, the whole rendering of the original button will be bypassed and your own images will be used instead. You can position those any way you like...-

          Edit: The solution below is much better than this one.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Lykurg
            wrote on last edited by
            #7

            Hi,

            if you will keep the rendering through the current style and avoid the shift use a QProxyStyle for the button where you set QStyle::PM_ButtonShiftHorizontal and QStyle::PM_ButtonShiftVertical in QStyle::pixelMetric() to 0.

            @class MyProxyStyle : public QProxyStyle
            {
            public:
            int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) {
            int ret = 0;
            switch (metric) {
            case QStyle::PM_ButtonShiftHorizontal:
            case QStyle::PM_ButtonShiftVertical:
            ret = 0;
            break;
            default:
            ret = QProxyStyle::pixelMetric(metric, option, widget);
            break;
            }
            return ret;
            }
            };@

            M 1 Reply Last reply
            2
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #8

              [quote author="Lykurg" date="1333011052"]Hi,

              if you will keep the rendering through the current style and avoid the shift use a QProxyStyle for the button where you set QStyle::PM_ButtonShiftHorizontal and QStyle::PM_ButtonShiftVertical in QStyle::pixelMetric() to 0.
              [/quote]

              Hey! That's great information! Thanks for sharing this.

              1 Reply Last reply
              0
              • L Lykurg

                Hi,

                if you will keep the rendering through the current style and avoid the shift use a QProxyStyle for the button where you set QStyle::PM_ButtonShiftHorizontal and QStyle::PM_ButtonShiftVertical in QStyle::pixelMetric() to 0.

                @class MyProxyStyle : public QProxyStyle
                {
                public:
                int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) {
                int ret = 0;
                switch (metric) {
                case QStyle::PM_ButtonShiftHorizontal:
                case QStyle::PM_ButtonShiftVertical:
                ret = 0;
                break;
                default:
                ret = QProxyStyle::pixelMetric(metric, option, widget);
                break;
                }
                return ret;
                }
                };@

                M Offline
                M Offline
                madasionka
                wrote on last edited by
                #9
                This post is deleted!
                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Kirik516
                  wrote on last edited by Kirik516
                  #10

                  Hello, everyone!
                  Team lead from neighbor department told me very interesting alternative idea for solving this problem.
                  You can use such strategy:

                  QPushButton *p = new QPushButton("", yourWidget);
                  QLabel *l = new QLabel("yor button text", p);
                  

                  And you get text without shifting and still clickable button without special code.
                  Only you need is to control QLabel size to be the same as button size.
                  This was tried for Qt5.7.0.

                  Chris KawaC 1 Reply Last reply
                  0
                  • K Kirik516

                    Hello, everyone!
                    Team lead from neighbor department told me very interesting alternative idea for solving this problem.
                    You can use such strategy:

                    QPushButton *p = new QPushButton("", yourWidget);
                    QLabel *l = new QLabel("yor button text", p);
                    

                    And you get text without shifting and still clickable button without special code.
                    Only you need is to control QLabel size to be the same as button size.
                    This was tried for Qt5.7.0.

                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @Kirik516 said in Prevent flat QToolButton from moving when clicked:

                    Only you need is to control QLabel size to be the same as button size.

                    You can use a layout for that:

                    QPushButton *p = new QPushButton(QString(), yourWidget);
                    p->setLayout(new QVBoxLayout());
                    p->layout()->addWidget(new QLabel("your button text"));
                    

                    But honestly adding a sub widget to position text is a bit overkill. In reality what you want is a button with custom drawing, so I would just inherit QToolButton and customize its paintEvent.

                    K 1 Reply Last reply
                    1
                    • Chris KawaC Chris Kawa

                      @Kirik516 said in Prevent flat QToolButton from moving when clicked:

                      Only you need is to control QLabel size to be the same as button size.

                      You can use a layout for that:

                      QPushButton *p = new QPushButton(QString(), yourWidget);
                      p->setLayout(new QVBoxLayout());
                      p->layout()->addWidget(new QLabel("your button text"));
                      

                      But honestly adding a sub widget to position text is a bit overkill. In reality what you want is a button with custom drawing, so I would just inherit QToolButton and customize its paintEvent.

                      K Offline
                      K Offline
                      Kirik516
                      wrote on last edited by Kirik516
                      #12

                      @Chris-Kawa
                      You are right. QLayout controls size very well.
                      But if we about the best method for topic's problem I think @Lykurg gave the most suituble solution.
                      Nevertheless, I think that adding QLabel upon QPushButton is very simple and usefull thing.
                      At least you will make your colleagues laugh, when you tell them this method.

                      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