1

I have a WidgetList with checkboxes and I want to use these as a signal to call the slotTest function.

Unfortunately, the code does not work and I can not find a solution:

connect(ui->listWidget, SIGNAL(item->checkState()), this, SLOT(slotTest()), Qt::QueuedConnection);

here more code:

 QStringList list;
    list << "CH1 100kHz" << "CH2 100kHz" << "CH3 100kHz";
    model->setStringList(list);
    ui->listView->setModel(model);
    ui->listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
    //ui->listView->setSelectionMode(QAbstractItemView::S)
    ui->listWidget->addItems(list);



    QListWidgetItem* item = 0;
    for(int i = 0; i < ui->listWidget->count(); ++i){
        item = ui->listWidget->item(i);
        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
        item->setFlags(item->flags() | Qt::ItemIsEditable);

        item->setCheckState(Qt::Unchecked);
        //connect(ui->listWidget, SIGNAL(itemClicked()), this, SLOT(slotTest()), Qt::QueuedConnection);

        // bool ischeck=ui->listWidget->item(i)->checkState();
        connect(ui->listWidget, SIGNAL(item->checkState()), this, SLOT(slotTest()), Qt::QueuedConnection);

    }
MB1995
  • 29
  • 3
  • Please add a complete [MCVE]. Because on a first glance, that line alone seems ok (except for the `Qt::QueuedConnection` with which I am at least not familiar. – Duck Dodgers Sep 09 '19 at 07:47
  • 1
    You use signal/slot mechanism wrongly. I think you must reread https://doc.qt.io/qt-5/signalsandslots.html once more. – vahancho Sep 09 '19 at 07:50
  • @vahancho i cant see why this dosnt work – MB1995 Sep 09 '19 at 08:12

1 Answers1

1

Fist of all, checkState() is not a signal, so you can't connect to it. Furthermore, the first parameter of the connect() call should be the actual sender of the signal, not its parent.

One alternative would be to connect to one of the signals emitted by the QListWidget when the user interacts with the controls. You should test them out in order to determine which one fits your use case best.

For example:

QObject::connect(ui->listWidget, &QListWidget::itemChanged, this, &MainWindow::slotTest);
void MainWindow::slotTest(QListWidgetItem *item)
{
    qInfo() << item->checkState();
}
  • Thank you, that works. I only have the problem that always all signals are output either as checked or unchecked. So if I have 100 signals the output at the consol will be 100 times checked or unchecked (all the same all checked or unchecked) depending on what I chose last – MB1995 Sep 09 '19 at 09:12
  • That happens because your connect call is inside the for loop. Just move it outside of it, since you only have to do it once. – David Barzi Sep 09 '19 at 09:15