Which operating system/Qt version are you using? I've seen strange bugs like this on one OS that will disappear on another (I haven't seen this one, though).
I'm using Qt 4.8.2 under Linux, and I don't think I'm seeing this behaviour. Here's the code I've got:
MyWindow.h:
#include <QMainWindow>
#include <QWidget>
class MyWindow : public QMainWindow
{
Q_OBJECT
public:
MyWindow(QWidget * = 0, Qt::WindowFlags = 0 );
private slots:
void printSelectionChanged();
};
MyWindow.cpp:
#include "MyWindow.h"
#include <QAbstractItemView>
#include <QList>
#include <QString>
#include <QStringList>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <iostream>
using namespace std;
MyWindow::MyWindow(QWidget *parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags)
{
QTreeWidget *treeWidget = new QTreeWidget(this);
treeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
treeWidget->setColumnCount(1);
QList<QTreeWidgetItem *> items;
for (int i = 0; i < 10; ++i)
items.append(new QTreeWidgetItem((QTreeWidget*)0, QStringList(QString("item: %1").arg(i))));
treeWidget->insertTopLevelItems(0, items);
connect(treeWidget, SIGNAL(itemSelectionChanged()), this, SLOT(printSelectionChanged()));
setCentralWidget(treeWidget);
}
void MyWindow::printSelectionChanged()
{
cout << "selection has changed." << endl;
}
Maybe you should post your code (or some simplified version thereof), if you're doing something differently.