Trolltech Home | Qt4-preview-feedback Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt4-preview-feedback Archive, March 2007
Qt4.3: QTreeWidget::setCurrentItem() selects item when in multi selection mode


Message 1 in thread

For quite some time I've been trying to convince TT that
it makes no sense that QTreeWidget::setCurrentItem() also
sets the item to "selected" in a MultiSelection list.
However, every attempt I've made so far has been rejected.

The first task tracker entry about this was #97290, which
was rejected on 2006-06-20. Then a new task #152590 was created
because the support person I had contact with agreed with me
that this behavior is nonsense in a MultiSelection list.
This task was rejected the very same day it was created (2007-02-28).
I was told to use QItemSelectionModel::setCurrentIndex() to only set
the current item without selecting it. Well, even though this might
work, it still doesn't answer the question why a function that is
called setCurrentItem() must have the nasty side effect of also
selecting the item in a MultiSelection list. I can fully undertand
that this may be the intended behavior in a SingleSelection list,
but in a MultiSelection list it may very well be necessary to
set the focus on an item without changing its selection state.

Please take a look at the attached example. It creates a QTreeWidget
with MultiSelection and adds some QTreeWidgetItems. Items 4 and 5
are set to be "selected", and item 1 is set to be the "current"
item (but _not_ selected). If you run this program, item 1 is
actually selected, which it shouldn't be.

Maybe somebody here can enlighten me *why* it is so very important
that QTreeWidget::setCurrentItem() also selects the item, even if
the list is in MultiSelection mode, where this IMHO makes absolutely
no sense at all. If I want an item to be selected, I call item->setSelected(true)
(which, BTW, doesn't have the side effect of making it the "current"
item ;-).

Regards
Klaus Schmidinger
#include <qapplication.h>
#include <qapplication.h>
#include <qdialog.h>
#include <qlayout.h>
#include <qtreewidget.h>

class cSelectTree : public QTreeWidget {
public:
  cSelectTree(QWidget *parent);
  };

cSelectTree::cSelectTree(QWidget *parent)
:QTreeWidget(parent)
{
  setColumnCount(2);
  QTreeWidgetItem *h = headerItem();
  h->setText(0, "Nr");
  h->setText(1, "Name");
  setSelectionMode(QAbstractItemView::MultiSelection);

  QTreeWidgetItem *currentButton = NULL;
  for (int i = 0; i < 6; ++i) {
      QTreeWidgetItem *b = new QTreeWidgetItem(this);
      b->setText(0, QString::number(i));
      b->setText(1, QString("%1_name").arg(QChar('A' + i)));

      if (i > 3)
         setItemSelected(b, true);
      if (i == 1)
         currentButton = b;
      }
  if (currentButton)
     setCurrentItem(currentButton);
}

class cDialog : public QDialog {
public:
  cDialog(QWidget *parent);
  };

cDialog::cDialog(QWidget *parent)
{
  QVBoxLayout *vbl = new QVBoxLayout(this);
  vbl->addWidget(new cSelectTree(this));
}

int main(int argc, char *argv[])
{
  QApplication myapp(argc, argv);
  cDialog *cd = new cDialog(NULL);
  cd->show();
  return myapp.exec();
}