PDA

View Full Version : QListView Event - help needed



wererabit
3rd April 2010, 13:34
Hi,

I am a newbie to QT, and trying to learn it myself. I am currently try to capture signal of a list view (selectionchanged or clicked). I have been trying awhile with these calls now but it does not seem to do anything. I debugged and found out that "err_method_notfound(sender, signal_arg, "connect")"

list = new QListView();
list->setViewMode(QListView::IconMode);
list->setFlow(QListView::TopToBottom);
list->setFixedWidth(100);
list->setWrapping(FALSE);

list->setModel(new ListModel(rscPath));

connect(list, SIGNAL(selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected )), this,
SLOT(onCurrentChanged(const QItemSelection & selected, const QItemSelection & deselected )));


void TransFuncEditorIntensityTexture::onCurrentChanged( const QItemSelection & selected, const QItemSelection & deselected )
{
list->setDisabled(true);
}

Could anyone let me know what have I done wrong? Thanks alot

toutarrive
3rd April 2010, 13:53
The correct signature for signal and slot is (simple example: a and b are objects, T is a type , slot/signal with one parameter only)

QObject::connect( &a, SIGNAL( valueChanged(T) ), &b, SLOT( setValue(T) ) );
It is an error to add selected and deselected in it. Just use the type.

Slot and signal can be provided by Qt or you can add your own signal and slot (i.e you have to provide a declaration and a definition in your class).
Have look at http://doc.trolltech.com/4.6/signalsandslots.html#slots

wererabit
3rd April 2010, 14:02
Thanks for replying!

I have changed my code to

void TransFuncEditorIntensityTexture::createConnections ()
{

connect(list, SIGNAL(selectionChanged ( QItemSelection & , QItemSelection & )), this, SLOT(ListSelectionChanged(QItemSelection &, QItemSelection & )));

}

void TransFuncEditorIntensityTexture::ListSelectionChan ged (QItemSelection &selected, QItemSelection &deselected )
{
list->setDisabled(true);
}
It still doesnt work thought. Is there anything I missed?

Thanks

toutarrive
3rd April 2010, 15:01
The signal selectionChanged implemented by Qt takes no parameter or a QListViewItem * as unique parameter:


void selectionChanged ()
void selectionChanged ( QListViewItem * )

If you want to use a signal/slot wich takes two QItemSelection as arguments, you've to implemented them in your class:


SIGNAL(selectionChanged ( QItemSelection & , QItemSelection & ) )
SLOT(selectionChanged ( QItemSelection & , QItemSelection & ) )


BTW, QItemSelection class manages information about selected items in a model (it is basically a list of selection ranges, see QItemSelectionRange Class Reference), what for do you use this class in your signal?

Lykurg
3rd April 2010, 18:02
Is there anything I missed?
The references are const => SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&))

Lykurg
3rd April 2010, 18:05
The signal selectionChanged implemented by Qt takes no parameter or a QListViewItem * as unique parameter:


void selectionChanged ()
void selectionChanged ( QListViewItem * )

You are talking of itemSelectionChanged, not selectionChanged!

faldzip
3rd April 2010, 22:11
First of all, there is no selectionChanged() signal in QListView - it's reimplemented protected slot - take a look at docs in Qt Assistant (which you should have open constantly to look for such things).
Second of all, keep your eye on console output - there is a warning (for sure) saying that there is no such signal or no such slot.
Third of all, in signal/slot signatures in connect statement use only types, not argument variable names. And only basic type is enough, so when argument is const QItemSelection &selected you can safely type QItemSelection in signal/slot signature in connect.
Fourth of all, do not forget Q_OBJECT macro in your class where your slot is (and of course this class has to inherit QObject or any of its subclasses)
Finally, about selections it is better (in my opinion) to use QItemSelectionModel and it's signals. You can get its instance from view after setting model to this view, just like that:


QItemSelectionModel *sm = list->selectionModel();

It has interesting signals for you -> check them in QtAssistant!

toutarrive
4th April 2010, 09:44
I was refering, by mystake, to Qt3.3 documentation instead of the latest one. Sorry for the confusion

wererabit
4th April 2010, 11:03
Thank so much for all the replies. :) and sadly I lost again at the "Fourth of all".

I am completely new to Qt, and have been trying to get this working for the last two days. Lets me explain what I want to do.

What I have is a class in which I added a QListview and populate it with images

The ListModel I created, in case you need to have a look

//List Model
class ListModel : public QAbstractListModel
{
public:
ListModel(QString srcPath) : QAbstractListModel()
{
size = 72;
QDirIterator it(srcPath);
while (it.hasNext()) {
QString file = it.next();
if (file.endsWith(".jpg"))
{
m_values << file;
}
}
}
int rowCount(QModelIndex const & index) const
{
return m_values.count();
}
QVariant data(QModelIndex const & index, int role) const
{
if (role == Qt::DecorationRole)
{
QPixmap pixmap = QPixmap(m_values.at(index.row()));
pixmap = pixmap.scaled( size, size, Qt::AspectRatioMode::KeepAspectRatio);
return pixmap;
}

if( role != Qt::DisplayRole && role != Qt::EditRole )
return QVariant();

if( index.column() == 0 && index.row() < m_values.count() )
return ""; //Text here
else
return QVariant();
}

private:
QList<QString > m_values;
int size;
};

Then creating the list

QLayout* TransFuncEditorIntensityTexture::createColorLayout () {

const QString rscPath = "../../data/3DTextures/";

list = new QListView();
list->setViewMode(QListView::IconMode);
list->setFlow(QListView::TopToBottom);
list->setFixedWidth(100);
list->setWrapping(FALSE);

list->setModel(new ListModel(rscPath));

QHBoxLayout* hBox = new QHBoxLayout();
hBox->addWidget(list);

if (orientation_ == Qt::Vertical)
return hBox;
else {
QVBoxLayout* vbox = new QVBoxLayout();
vbox->addLayout(hBox, 1);
vbox->addStretch();
return vbox;
}

return hBox;
}


And this is what I got
4468

Now all what I want is when the user click on one of the image, I am notified (any particular method is called) . That is all, really. I guess its probably simpler than I expected but still. I have been trying all kinds of thing and currently. This is what I have


connect(list, SIGNAL(selectionChanged ( QListViewItem* )), this, SLOT(selectionList( QListViewItem* )));

void TransFuncEditorIntensityTexture::selectionList(QLi stViewItem* item)
{
//Just make sure this method is called. but it never happens
list->setDisabled(true);
}

within TransFuncEditorIntensityTexture class definition, I have

public slots:
void selectionList(QListViewItem*);

I have been reading Slot and Signal article, running back and forth on the net. Could you guys please let me know what I am missing? or point me to where I can find the information.

Really appriciate it

Lykurg
4th April 2010, 11:28
First of all, there is no selectionChanged() signal in QListView - it's reimplemented protected slot
missed that...

And only basic type is enough, so when argument is const QItemSelection &selected you can safely type QItemSelection in signal/slot signature in connect.
Crazy, after reading "Signals and Slots" in the docs again there are two options: I am to stupid to read or they don't tell it there. But I works. Learned something more. Thanks.



connect(list, SIGNAL(selectionChanged ( QListViewItem* )), this, SLOT(selectionList( QListViewItem* )));

void TransFuncEditorIntensityTexture::selectionList(QLi stViewItem* item)
{
//Just make sure this method is called. but it never happens
list->setDisabled(true);
}
As faldżip said: use the selection model:
connect(list->selectionModel(), /*...*/);

wererabit
4th April 2010, 11:49
First of all, there is no selectionChanged() signal in QListView - it's reimplemented protected slot

So where could I find the list of signal that are available?

Thanks

toutarrive
4th April 2010, 12:39
So where could I find the list of signal that are available?
Here, look at signals http://doc.qt.nokia.com/4.6/qlistview.html

faldzip
4th April 2010, 12:45
In Qt documentation. If you have Qt installed on your system then you also have Qt Assistant (QTDIR/bin/assistant.exe) which is the help browser. There you can find QListView docs where all of its functions are documented (don't forget it is derived from QAbstractView so check it's methods also). What you are looking for is rather QItemSelectionModel which is special class for handling item selections. You can find documentation also by clicking on class names in my post (the same docs as in QtAssistant).

wererabit
5th April 2010, 02:14
Thank you!