Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Custom push-button with text and icon



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


  • Lifetime Qt Champion

    Hi,

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


  • Lifetime Qt Champion

    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



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



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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


Log in to reply