nicktook
nicktook

Reputation: 154

Mapping a Qt base class signal to a slot in a derived class

I am having a problem with Qt signals and slots. I am just learning Qt but I have lots of C++ experience. I have derived a class from QTreeView and I want to handle the columnResized signal. The slot is never being called and I am seeing this in the 'Application Output':

QObject::connect: No such signal TRecListingView::columnResized(int,int,int) in ../ec5/reclistingwidget.cpp:142

The class declaration looks like this:

class TRecListingView : public QTreeView
{
    Q_OBJECT
public:
    TRecListingView(QWidget *parent, TTopicPtr topic);
    ~TRecListingView();

private slots:
    void onColumnResized(int index, int oldsize, int newsize);

private:
    TRecListingModel *Model = 0;
};

In the constructor I am doing this:

connect(this,SIGNAL(columnResized(int,int,int)),
        this,SLOT(onColumnResized(int,int,int)));

I had this working earlier before I implemented the derived class. Then I was mapping the signal to a slot in the parent widget.

I have tried running qmake and rebuilding the project. I also tried this:

QTreeView *tv = this;
connect(tv,SIGNAL(columnResized(int,int,int)),
        this,SLOT(onColumnResized(int,int,int)));

Upvotes: 0

Views: 1308

Answers (2)

Jablonski
Jablonski

Reputation: 18524

Because it is not a signal:

From documentation:

void QTreeView::columnResized ( int column, int oldSize, int newSize ) [protected slot]

Try reimplement it:

#include <QTreeView>
#include <QHeaderView>
#include <QTimer>
#include <QDebug>


class TRecListingView : public QTreeView
{
    Q_OBJECT
public:
    TRecListingView(QWidget *parent=0):
        QTreeView(parent)

    {
        QTimer::singleShot(0, this, SLOT(fixHeader()));
    }


public slots:

    void fixHeader()
    {
        QHeaderView *hv = new QHeaderView(Qt::Horizontal, this);
        hv->setHighlightSections(true);
        this->setHeader(hv);
        hv->show();
    }
protected slots:
    void columnResized(int a, int b, int col)
    {
        qDebug() << "This is called";
    }

public slots:

};

Simple usage:

TRecListingView trec;

QStringList stringList;
stringList << "#hello" << "#quit" << "#bye";
QStringListModel *mdl = new QStringListModel(stringList);
trec.setModel(mdl);
trec.show();

Now it works properly and when you resize header, you'll see many This is called strings.

Upvotes: 0

Rafal Mielniczuk
Rafal Mielniczuk

Reputation: 1342

columnResized is not a signal, but slot, so you cannot connect to it.

Instead you can connect to the QHeaderView::sectionResized

connect(this->horizontalHeader(),SIGNAL(sectionResized(int,int,int)),
        this,                    SLOT(onColumnResized(int,int,int)));

Upvotes: 1

Related Questions