我试图重现 OP 描述的问题。
不幸的是,OP 没有提供MCVE。所以,我自己做了一个:
// Qt header:
#include <QtWidgets>
class ListWidget: public QListWidget {
public:
ListWidget(QWidget *pQParent = nullptr): QListWidget(pQParent) { }
virtual ~ListWidget() = default;
ListWidget(const ListWidget&) = delete;
ListWidget& operator=(const ListWidget&) = delete;
virtual void currentChanged(
const QModelIndex ¤t, const QModelIndex &previous) override
{
qDebug() << "currentChanged():" << previous << "->" << current;
QListWidget::currentChanged(current, previous);
}
virtual void selectionChanged(
const QItemSelection &selected, const QItemSelection &deselected) override
{
qDebug() << "selectionChanged():" << selected << "->" << deselected;
QListWidget::selectionChanged(selected, deselected);
}
};
void populate(QListWidget &qLst)
{
const int n = 10;
for (int i = 1; i <= n; ++i) {
new QListWidgetItem(QString("item %1").arg(i), &qLst);
}
}
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
ListWidget qLst;
qLst.setSelectionMode(QListWidget::ExtendedSelection);
populate(qLst);
qLst.show();
// runtime loop
return app.exec();
}
在VS2017中用Qt5.13编译:
输出:
Qt Version: 5.13.0
currentChanged(): QModelIndex(-1,-1,0x0,QObject(0x0)) -> QModelIndex(0,0,0x2adc4f38610,QListModel(0x2adc4f3a3a0))
我用 Shift ↓ 选择了前 5 个项目:
currentChanged(): QModelIndex(0,0,0x2adc4f38610,QListModel(0x2adc4f3a3a0)) -> QModelIndex(1,0,0x2adc4f38a00,QListModel(0x2adc4f3a3a0))
selectionChanged(): (QItemSelectionRange(QModelIndex(0,0,0x2adc4f38610,QListModel(0x2adc4f3a3a0)),QModelIndex(0,0,0x2adc4f38610,QListModel(0x2adc4f3a3a0))), QItemSelectionRange(QModelIndex(1,0,0x2adc4f38a00,QListModel(0x2adc4f3a3a0)),QModelIndex(1,0,0x2adc4f38a00,QListModel(0x2adc4f3a3a0)))) -> ()
currentChanged(): QModelIndex(1,0,0x2adc4f38a00,QListModel(0x2adc4f3a3a0)) -> QModelIndex(2,0,0x2adc4f38a70,QListModel(0x2adc4f3a3a0))
selectionChanged(): (QItemSelectionRange(QModelIndex(2,0,0x2adc4f38a70,QListModel(0x2adc4f3a3a0)),QModelIndex(2,0,0x2adc4f38a70,QListModel(0x2adc4f3a3a0)))) -> ()
currentChanged(): QModelIndex(2,0,0x2adc4f38a70,QListModel(0x2adc4f3a3a0)) -> QModelIndex(3,0,0x2adc4f38ae0,QListModel(0x2adc4f3a3a0))
selectionChanged(): (QItemSelectionRange(QModelIndex(3,0,0x2adc4f38ae0,QListModel(0x2adc4f3a3a0)),QModelIndex(3,0,0x2adc4f38ae0,QListModel(0x2adc4f3a3a0)))) -> ()
currentChanged(): QModelIndex(3,0,0x2adc4f38ae0,QListModel(0x2adc4f3a3a0)) -> QModelIndex(4,0,0x2adc4f38f40,QListModel(0x2adc4f3a3a0))
selectionChanged(): (QItemSelectionRange(QModelIndex(4,0,0x2adc4f38f40,QListModel(0x2adc4f3a3a0)),QModelIndex(4,0,0x2adc4f38f40,QListModel(0x2adc4f3a3a0)))) -> ()
然后我使用 Ctrl ↓ 将焦点移动到item 10:
currentChanged(): QModelIndex(4,0,0x2adc4f38f40,QListModel(0x2adc4f3a3a0)) -> QModelIndex(5,0,0x2adc4f3f990,QListModel(0x2adc4f3a3a0))
currentChanged(): QModelIndex(5,0,0x2adc4f3f990,QListModel(0x2adc4f3a3a0)) -> QModelIndex(6,0,0x2adc4f3f6f0,QListModel(0x2adc4f3a3a0))
currentChanged(): QModelIndex(6,0,0x2adc4f3f6f0,QListModel(0x2adc4f3a3a0)) -> QModelIndex(7,0,0x2adc4f3f1b0,QListModel(0x2adc4f3a3a0))
currentChanged(): QModelIndex(7,0,0x2adc4f3f1b0,QListModel(0x2adc4f3a3a0)) -> QModelIndex(8,0,0x2adc4f3f840,QListModel(0x2adc4f3a3a0))
currentChanged(): QModelIndex(8,0,0x2adc4f3f840,QListModel(0x2adc4f3a3a0)) -> QModelIndex(9,0,0x2adc4f3f7d0,QListModel(0x2adc4f3a3a0))
这在我看来是合理的 - 正如 OP 所报告的那样,没有对 selectionChanged() 的不必要调用。
可以肯定的是,我在cygwin中编译了相同的代码并再次尝试:
$ qmake-qt5
Info: creating stash file /cygdrive/d/ds32737/Entwicklung/tests/Qt/QAbstractItemViewCurrentChanged/.qmake.stash
$ make && ./testQAbstractItemViewCurrentChanged
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQAbstractItemViewCurrentChanged.o testQAbstractItemViewCurrentChanged.cc
g++ -o testQAbstractItemViewCurrentChanged.exe testQAbstractItemViewCurrentChanged.o -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
Qt Version: 5.9.4
currentChanged(): QModelIndex(-1,-1,0x0,QObject(0x0)) -> QModelIndex(0,0,0x60014b260,QListModel(0x6000df6b0))
currentChanged(): QModelIndex(0,0,0x60014b260,QListModel(0x6000df6b0)) -> QModelIndex(1,0,0x60014ba60,QListModel(0x6000df6b0))
selectionChanged(): (QItemSelectionRange(QModelIndex(0,0,0x60014b260,QListModel(0x6000df6b0)),QModelIndex(0,0,0x60014b260,QListModel(0x6000df6b0))), QItemSelectionRange(QModelIndex(1,0,0x60014ba60,QListModel(0x6000df6b0)),QModelIndex(1,0,0x60014ba60,QListModel(0x6000df6b0)))) -> ()
currentChanged(): QModelIndex(1,0,0x60014ba60,QListModel(0x6000df6b0)) -> QModelIndex(2,0,0x60014bb20,QListModel(0x6000df6b0))
selectionChanged(): (QItemSelectionRange(QModelIndex(2,0,0x60014bb20,QListModel(0x6000df6b0)),QModelIndex(2,0,0x60014bb20,QListModel(0x6000df6b0)))) -> ()
currentChanged(): QModelIndex(2,0,0x60014bb20,QListModel(0x6000df6b0)) -> QModelIndex(3,0,0x60014bc30,QListModel(0x6000df6b0))
selectionChanged(): (QItemSelectionRange(QModelIndex(3,0,0x60014bc30,QListModel(0x6000df6b0)),QModelIndex(3,0,0x60014bc30,QListModel(0x6000df6b0)))) -> ()
currentChanged(): QModelIndex(3,0,0x60014bc30,QListModel(0x6000df6b0)) -> QModelIndex(4,0,0x60014bd20,QListModel(0x6000df6b0))
selectionChanged(): (QItemSelectionRange(QModelIndex(4,0,0x60014bd20,QListModel(0x6000df6b0)),QModelIndex(4,0,0x60014bd20,QListModel(0x6000df6b0)))) -> ()
currentChanged(): QModelIndex(4,0,0x60014bd20,QListModel(0x6000df6b0)) -> QModelIndex(5,0,0x60014be10,QListModel(0x6000df6b0))
currentChanged(): QModelIndex(5,0,0x60014be10,QListModel(0x6000df6b0)) -> QModelIndex(6,0,0x60014bf00,QListModel(0x6000df6b0))
currentChanged(): QModelIndex(6,0,0x60014bf00,QListModel(0x6000df6b0)) -> QModelIndex(7,0,0x60014bbb0,QListModel(0x6000df6b0))
currentChanged(): QModelIndex(7,0,0x60014bbb0,QListModel(0x6000df6b0)) -> QModelIndex(8,0,0x60014c120,QListModel(0x6000df6b0))
currentChanged(): QModelIndex(8,0,0x60014c120,QListModel(0x6000df6b0)) -> QModelIndex(9,0,0x60014c210,QListModel(0x6000df6b0))
所以,我再次无法复制。报告的事件与我使用 VS2017 得到的事件相当。
请注意,我已经在 cygwin 上安装了 Qt5.9.4——OP 声称拥有的版本。