madasionka
madasionka

Reputation: 922

Qt: Map a clicked signal to another button

I wanted to have a collapsible widget. I used this code: How to make an expandable/collapsable section widget in QT.

I wanted the title of the QToolButton to be on the far left and the triangle icon to be on the right. I deleted the title, and moved the icon. Then I created a QPushButton and made it look like a QLabel and positioned it where I wanted the title to be. Now, I would like the title to be clickable - to have the same effect as clicking on the toggle icon would have. How do I connect these two signals?

Code for the QToolButton:

QObject::connect(toggleButton, &QToolButton::clicked, [this](const bool checked)
{
    toggleButton->setArrowType(checked ? Qt::ArrowType::DownArrow : Qt::ArrowType::UpArrow);
    toggleAnimation->setDirection(checked ? QAbstractAnimation::Forward : QAbstractAnimation::Backward);
    toggleAnimation->start();
});

Upvotes: 1

Views: 424

Answers (2)

x squared
x squared

Reputation: 3354

You can also write it like this:

QObject::connect(titleLabel, &QPushButten::clicked, toggleButton, &QToolButton::clicked);

Upvotes: 1

madasionka
madasionka

Reputation: 922

I found that this works:

QObject::connect(titleLabel, &QPushButton::clicked, [this]
{
    toggleButton->click();
});

Disclaimer: I have no idea if that is the correct way to do this.

Upvotes: 0

Related Questions