Qt: Connecting protected QListWidget::itemChanged signal to a slot
Asked Answered
S

1

2

I used below syntax in Qt5 according to new connect syntax to avoid type mismatches of slot and signals for a a QListWidget with checkable items.

connect(item, &QListWidget::itemChanged,this , &mainWindow::checkItemChanged);

I want to run my slot in case any of list item changed its state. In order to this this I used itemChanged signal due to this answer, but it is protected and compile time error raise as below:

error: ‘void QListWidget::itemChanged(QListWidgetItem*)’ is protected

How can I handles this? Should I subclass my own QListWidget or there are some other solutions to this?

Swat answered 2/1, 2018 at 11:3 Comment(3)
What you propose is the only option I can think of.Vexation
Are you sure you're using Qt5? QListWidget::itemChanged should be public in Qt5 but may have been protected in Qt4.Ashlieashlin
In both Qt4 and Qt5 this signal declared under Q_SIGNALS macro which resolves to be public in Qt5 and protected for Qt4. You might use Qt4 connection style instead.Resistencia
F
2

You can use the more appropriate syntax according to Qt version:

#if QT_VERSION >= 0x050000
    connect(item, &QListWidget::itemChanged, this , &MainWindow::checkItemChanged);
#else
    connect(item, SIGNAL(checkItemChanged), this , SLOT(checkItemChanged));
#endif

(or the 'old string-based' for all versions).

Folkways answered 2/1, 2018 at 13:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.