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. Custom push-button with text and icon

Custom push-button with text and icon

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 3.6k 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.
  • P Offline
    P Offline
    peter-70
    wrote on 31 Jan 2019, 20:21 last edited by peter-70
    #1

    I have a problem with a custom pushbutton I created. My button class inherits from QPushButton that's clear!
    Then I create a BoxLayout, vertical or horizontal, and put into it a text and an icon. Or only text or only icon:

    layout->addWidget(qlabelWithIcon, ...etc)
    layout->addWidget(qlabelWithText, ...etc)
    

    Then I attach the layout to the pushbutton:

    button->setLayout(layout)
    

    It's not good, because the layout is in the z-order over the button, and when I click on it, the receiver of the click-event are the labels with icon or text, and so on...

    What's the solution? I could put a transparent button over the labels (icon and text) and routing the click-event to the underlying button.
    Or I could set the icon and text labels, as a background for the underlying button (but how???)

    What would you say? What is the best practice?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 31 Jan 2019, 20:36 last edited by
      #2

      Hi,

      Why do you need a custom button since QPushButton already supports text and icon ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply 1 Feb 2019, 05:22
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 31 Jan 2019, 20:56 last edited by mrjj
        #3

        Hi
        Adding to to @SGaist , if QPushbutton or QToolButton is not enough icon/text wise,
        and what you need it is a clickable QLabel, then why not just make it respond to mouse?
        https://wiki.qt.io/Clickable_QLabel

        P 1 Reply Last reply 1 Feb 2019, 05:27
        2
        • S SGaist
          31 Jan 2019, 20:36

          Hi,

          Why do you need a custom button since QPushButton already supports text and icon ?

          P Offline
          P Offline
          peter-70
          wrote on 1 Feb 2019, 05:22 last edited by peter-70 2 Jan 2019, 05:23
          #4

          @SGaist I'm using FontAwesome, therefore I have two labels. One for the unicode sign, and the other for the text

          1 Reply Last reply
          0
          • M mrjj
            31 Jan 2019, 20:56

            Hi
            Adding to to @SGaist , if QPushbutton or QToolButton is not enough icon/text wise,
            and what you need it is a clickable QLabel, then why not just make it respond to mouse?
            https://wiki.qt.io/Clickable_QLabel

            P Offline
            P Offline
            peter-70
            wrote on 1 Feb 2019, 05:27 last edited by
            #5

            @mrjj Yes I know about that, but I think this is not a very good solution. The event routing here, is cumbersome (imho)

            M 1 Reply Last reply 1 Feb 2019, 07:15
            0
            • P peter-70
              1 Feb 2019, 05:27

              @mrjj Yes I know about that, but I think this is not a very good solution. The event routing here, is cumbersome (imho)

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 1 Feb 2019, 07:15 last edited by
              #6

              @peter-70
              Hi
              Im not sure what you mean with , "The event routing here, is cumbersome "
              it will just make the Label be able to emit a clicked()
              just like a button does. So it can be connected to a slot and
              both work as a button and a QLabel.

              P 1 Reply Last reply 2 Feb 2019, 12:16
              2
              • M mrjj
                1 Feb 2019, 07:15

                @peter-70
                Hi
                Im not sure what you mean with , "The event routing here, is cumbersome "
                it will just make the Label be able to emit a clicked()
                just like a button does. So it can be connected to a slot and
                both work as a button and a QLabel.

                P Offline
                P Offline
                peter-70
                wrote on 2 Feb 2019, 12:16 last edited by peter-70 2 Feb 2019, 12:21
                #7

                @mrjj
                I must catch mouse events and send these as my custom signals. Furthermore I don't know if QLabel receives all the events, such as like a pushbutton.
                I have solved my problem with the following code (greatly shortened):

                this->layout.addWidget(&this->overlay, 0, 0, this->layout.rowCount(), this->layout.columnCount());
                QObject::connect(&this->overlay, &QPushButton::pressed, [=]() {
                    emit this->pressed();
                });
                
                QObject::connect(&this->overlay, &QPushButton::released, [=]() {
                    emit this->released();
                });
                
                QObject::connect(&this->overlay, &QPushButton::clicked, [=](bool checked = false) {
                    emit this->clicked(checked);
                });
                
                QObject::connect(&this->overlay, &QPushButton::toggled, [=](bool checked) {
                    emit this->toggled(checked);
                });
                

                Just you can see what's the idea to solve it

                edit:
                I could imagine the complete solution here, but the code (I think so), is intellectual property of my employer and because of my secrecy, I do not want to do it. I hope you understand

                M 1 Reply Last reply 2 Feb 2019, 12:47
                0
                • P peter-70
                  2 Feb 2019, 12:16

                  @mrjj
                  I must catch mouse events and send these as my custom signals. Furthermore I don't know if QLabel receives all the events, such as like a pushbutton.
                  I have solved my problem with the following code (greatly shortened):

                  this->layout.addWidget(&this->overlay, 0, 0, this->layout.rowCount(), this->layout.columnCount());
                  QObject::connect(&this->overlay, &QPushButton::pressed, [=]() {
                      emit this->pressed();
                  });
                  
                  QObject::connect(&this->overlay, &QPushButton::released, [=]() {
                      emit this->released();
                  });
                  
                  QObject::connect(&this->overlay, &QPushButton::clicked, [=](bool checked = false) {
                      emit this->clicked(checked);
                  });
                  
                  QObject::connect(&this->overlay, &QPushButton::toggled, [=](bool checked) {
                      emit this->toggled(checked);
                  });
                  

                  Just you can see what's the idea to solve it

                  edit:
                  I could imagine the complete solution here, but the code (I think so), is intellectual property of my employer and because of my secrecy, I do not want to do it. I hope you understand

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 2 Feb 2019, 12:47 last edited by mrjj 2 Feb 2019, 12:49
                  #8

                  @peter-70
                  ah, ok, its just not clicked you need.
                  well if overlay concept works, then should be fine.

                  I understand you cannot post full code. One should not take intellectual property lightly.

                  1 Reply Last reply
                  0

                  7/8

                  2 Feb 2019, 12:16

                  • Login

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