Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Prevent flat QToolButton from moving when clicked

    General and Desktop
    8
    12
    6609
    Loading More Posts
    • 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
      Deusdies last edited by

      So I'm trying to implement basically the same Settings dialog as in latest versions of Firefox. At the top I've placed a white frame with several QToolButtons placed over it, and styled them with CSS.

      It all looks great but when I click on the button (which is flat btw), it moves a pixel to the right and a pixel to the bottom. Not a big deal but still irritating, so I was wondering if there is any way to compensate for this?

      Thanks

      EDIT: It's not a flat button, it's just a normal QToolButton with "checkable=True"

      1 Reply Last reply Reply Quote 0
      • G
        goetz last edited by

        If a checkable tool buttion is checked, it is visually "pushed in", that's the cause of the move. Try without the style sheet to see the original visual appearance.

        If you do not want the user to see whether a tool button is checked or not, you should consider setting checkable to false. That would prevent the "move" too.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply Reply Quote 0
        • D
          Deusdies last edited by

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

          1 Reply Last reply Reply Quote 0
          • M
            mlong last edited by

            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 Reply Quote 0
            • D
              Deusdies last edited by

              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 Reply Quote 0
              • A
                andre last edited by

                -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 Reply Quote 0
                • L
                  Lykurg last edited by

                  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 Reply Quote 2
                  • A
                    andre last edited by

                    [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 Reply Quote 0
                    • M
                      madasionka @Lykurg last edited by

                      This post is deleted!
                      1 Reply Last reply Reply Quote 0
                      • K
                        Kirik516 last edited by 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 Kawa 1 Reply Last reply Reply Quote 0
                        • Chris Kawa
                          Chris Kawa Moderators @Kirik516 last edited by

                          @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 Reply Quote 1
                          • K
                            Kirik516 @Chris Kawa last edited by Kirik516

                            @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 Reply Quote 0
                            • First post
                              Last post