1

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();
});
Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
madasionka
  • 812
  • 2
  • 10
  • 29

2 Answers2

1

You can also write it like this:

QObject::connect(titleLabel, &QPushButten::clicked, toggleButton, &QToolButton::clicked);
x squared
  • 3,173
  • 1
  • 26
  • 41
  • This didn't have the desired effect - the animation filckered, but it didn't expand. But `QObject::connect(titleLabel, &QToolButton::clicked, toggleButton, &QToolButton::click);` worked as it should. So thanks! – madasionka Nov 24 '17 at 09:41
  • Ah of course this works too. My answer requires you to connect the toggleButton’s `clicked()` signal to a slot, I assumed that this was the case. – x squared Nov 24 '17 at 11:19
0

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.

madasionka
  • 812
  • 2
  • 10
  • 29