0

I want to acces the value of the QSliders that are in my QGridLayout, but I don't find the right command. An example of what I try:

 QSlider* slider=ui->gridLayout->takeAt(1)->widget();

The error message I get is:

cannot convert 'QLayoutItem*' to 'QSlider*' in initialization

Is there a command to ask the QSlider right away and not the widget? Is it possible to cast? I tried it, but this also didn't work. How can I solve this?

Adrien Leravat
  • 2,731
  • 18
  • 32
  • 1
    try with: `QSlider* slider = qobject_cast(ui->gridLayout->takeAt(1)->widget());` – eyllanesc May 20 '18 at 10:50
  • Note that [`QLayout::takeAt`](http://doc.qt.io/qt-5/qlayout.html#takeAt) *removes* the specified item from the layout. You might want [`QLayout::itemAt`](http://doc.qt.io/qt-5/qlayout.html#itemAt) instead. – G.M. May 20 '18 at 11:06
  • thanks, it does the job! – Wouter Devos May 20 '18 at 12:50

1 Answers1

0

You can use QGridLayout::findChild<T> method:

QSlider *slider = ui->gridLayout->findChild<QSlider *>("widget_name"); // Name is optional

Or alternatively

QList<QSlider*> sliders = ui->gridLayout->findChildren<QSlider *>();

As pointed by G.M, QLayout::takeAt is used to safely remove and return a Widget from a QLayout, so not what you are trying to do.

Adrien Leravat
  • 2,731
  • 18
  • 32