Help please - what am I doing wrong?
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.
Code:
// gridview.h
#ifndef GRIDVIEW_H
#define GRIDVIEW_H
#include <QTableView>
#include <QHeaderView>
#include <QItemSelection>
#include <QItemSelectionModel>
{
Q_OBJECT
public:
~GridView();
private slots:
};
#endif // GRIDVIEW_H
Code:
// gridview.cpp
#include "gridview.h"
GridView
::GridView(QWidget *parent
){
}
GridView::~GridView() { }
{
// my code here never gets called...
}
Any help is appreciated. Thanks in advance.
Jimmy
Re: Help please - what am I doing wrong?
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).
Re: Help please - what am I doing wrong?
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 &)));
Re: Help please - what am I doing wrong?
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?
Re: Help please - what am I doing wrong?
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
Re: Help please - what am I doing wrong?
Quote:
Originally Posted by Jimmy2775
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.
Re: Help please - what am I doing wrong?
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