PDA

View Full Version : Help please - what am I doing wrong?



Jimmy2775
3rd March 2006, 23:48
Hi there - I have created a class that extends QTableView. When the user changes which items are selected in the view, I would expect that the class would send a selectionChanged signal. However, I cannot seem to connect that signal to a slot. I am pretty sure that my syntax is correct, but I must be doing something wrong since it's not working.


// gridview.h
#ifndef GRIDVIEW_H
#define GRIDVIEW_H

#include <QTableView>
#include <QHeaderView>
#include <QItemSelection>
#include <QItemSelectionModel>

class GridView : public QTableView
{
Q_OBJECT

public:
GridView(QWidget *parent = 0);
~GridView();
private slots:
void testSlot(const QItemSelection &selected, const QItemSelection &deselected);
};

#endif // GRIDVIEW_H



// gridview.cpp
#include "gridview.h"

GridView::GridView(QWidget *parent)
: QTableView(parent)
{
connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)), this, SLOT(testSlot(const QItemSelection &selected, const QItemSelection &deselected)));
}

GridView::~GridView() { }

void GridView::testSlot(const QItemSelection &selected, const QItemSelection &deselected)
{
// my code here never gets called...
}


Any help is appreciated. Thanks in advance.

Jimmy

wysota
4th March 2006, 00:18
I'm not sure, but you're probably getting some info on the console (remember about CONFIG+= console debug) that Qt (or QMetaType) can't serialize QItemSelection or something like that. If so, then you have to register QItemSelection with Qt Metadata system using Q_DECLARE_METATYPE(QItemSelection).

newellm
4th March 2006, 05:29
The problem is that you are listing the variable names in the connect statement.

connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)), this, SLOT(testSlot(const QItemSelection &selected, const QItemSelection &deselected)));


should be

connect(selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(testSlot(const QItemSelection &, const QItemSelection &)));

Jimmy2775
6th March 2006, 17:18
I eliminated the variable names, but the problem remains. Following wysota's advice I was able to trap this error message:

Object::connect: Cannot connect (null)::selectionChanged(const QItemSelection, const QItemSelection) to GridView::testSlot(const QItemSelection, const QItemSelection)

This leads me to believe that perhaps the QItemSelectionModel has not been initialized (since it's referred to as 'null'). Is there some kind of initialization I need to do for the selection model before I can access it in the grid's constructor?

Jimmy2775
6th March 2006, 17:33
Yes that was the problem. I created a new QItemSelectionModel and passed it in to the view's setSelectionModel() method. Now it works as expected.

I had assumed that creating a new QTableView object would implicitly create the selection model object as well, but I guess I assumed wrong.

Thanks for your help anyway.

Jimmy

Chicken Blood Machine
6th March 2006, 19:27
Yes that was the problem. I created a new QItemSelectionModel and passed it in to the view's setSelectionModel() method. Now it works as expected.

I had assumed that creating a new QTableView object would implicitly create the selection model object as well, but I guess I assumed wrong.

Thanks for your help anyway.

Jimmy

Once you set a model for the view with setModel(), a new QItemSelectionModel will be created. You don't have to create one.

Jimmy2775
6th March 2006, 22:06
So then the problem was that I was trying to connect the selection model's signal in the constructor of the QTableView, before the view's model was set. Makes sense.

Thanks