Qt: Connecting protected QListWidget::itemChanged

2019-02-25 17:50发布

问题:

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?

回答1:

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).