Lykurg
24th March 2009, 16:04
Hi,
I have a QListView and connect the signal from its selection model ItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) to my slot. In that slot I want to revert the change. But I fail. How to revert an selection?
Example:
#include <QtGui>
#include "test.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Test w;
w.show();
return app.exec();
}
#ifndef TEST_H
#define TEST_H
#include <QtGui>
class Test : public QWidget
{
Q_OBJECT
public:
Test(QWidget *parent = 0);
QListView *v;
bool canceled;
public slots:
void changed( const QModelIndex & current, const QModelIndex & previous );
};
#endif
#include "test.h"
#include <QtGui>
Test::Test(QWidget *parent)
: QWidget(parent)
{
QLayout *l = new QHBoxLayout(this);
v = new QListView(this);
v->setSelectionMode(QAbstractItemView::SingleSelectio n);
QStringListModel *model = new QStringListModel();
QStringList list;
list << "a" << "b" << "c" << "d" << "e" << "f";
model->setStringList(list);
v->setModel(model);
l->addWidget(v);
setLayout(l);
v->setCurrentIndex(model->index(0,0));
connect(v->selectionModel(), SIGNAL(currentChanged(const QModelIndex& , const QModelIndex&)), this, SLOT(changed(const QModelIndex& , const QModelIndex&)));
canceled = false;
}
void Test::changed( const QModelIndex & current, const QModelIndex & previous )
{
qWarning() << current << previous;
if (canceled)
{
canceled = false;
return;
}
QMessageBox msgBox;
msgBox.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
int ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Discard:
break;
case QMessageBox::Cancel:
canceled = true;
v->selectionModel()->setCurrentIndex(previous, QItemSelectionModel::ClearAndSelect); // no "back change"
//v->setCurrentIndex(previous); // all lines between current and previous are selected
return;
break;
}
}
Thanks for any help.
I have a QListView and connect the signal from its selection model ItemSelectionModel::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) to my slot. In that slot I want to revert the change. But I fail. How to revert an selection?
Example:
#include <QtGui>
#include "test.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
Test w;
w.show();
return app.exec();
}
#ifndef TEST_H
#define TEST_H
#include <QtGui>
class Test : public QWidget
{
Q_OBJECT
public:
Test(QWidget *parent = 0);
QListView *v;
bool canceled;
public slots:
void changed( const QModelIndex & current, const QModelIndex & previous );
};
#endif
#include "test.h"
#include <QtGui>
Test::Test(QWidget *parent)
: QWidget(parent)
{
QLayout *l = new QHBoxLayout(this);
v = new QListView(this);
v->setSelectionMode(QAbstractItemView::SingleSelectio n);
QStringListModel *model = new QStringListModel();
QStringList list;
list << "a" << "b" << "c" << "d" << "e" << "f";
model->setStringList(list);
v->setModel(model);
l->addWidget(v);
setLayout(l);
v->setCurrentIndex(model->index(0,0));
connect(v->selectionModel(), SIGNAL(currentChanged(const QModelIndex& , const QModelIndex&)), this, SLOT(changed(const QModelIndex& , const QModelIndex&)));
canceled = false;
}
void Test::changed( const QModelIndex & current, const QModelIndex & previous )
{
qWarning() << current << previous;
if (canceled)
{
canceled = false;
return;
}
QMessageBox msgBox;
msgBox.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
int ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Discard:
break;
case QMessageBox::Cancel:
canceled = true;
v->selectionModel()->setCurrentIndex(previous, QItemSelectionModel::ClearAndSelect); // no "back change"
//v->setCurrentIndex(previous); // all lines between current and previous are selected
return;
break;
}
}
Thanks for any help.