How to extend method expanded in QTreeView



  • I want overwrite QTreeView
    @class SelfDifineTree : public QTreeView{
    public:
    SelfDifineTree();
    virtual ~SelfDifineTree();
    Q_SIGNALS:
    void expanded(const QModelIndex &index);
    @
    but got a error
    @D:\bak\mytreewindow\mytreewindow-build-desktop......\QtSDK\Desktop\Qt\4.7.3\mingw\include\QtGui\qtreeview.h:151: candidates are: void QTreeView::expanded(const QModelIndex&)@

    can anyone help me on this problem?thx a lot



  • The signal "expanded is already declared at class QTreeView":http://doc.qt.nokia.com/latest/qtreeview.html#expanded which is inherited by your class SelfDifineTree.



  • Thx for your reply, it is true that expanded is already defined, and this is also my purpose, I want do overwrite this method
    @
    void SelfDifineTree::expanded(const QModelIndex &index){
    //to do somthing
    this->QTreeView::expanded(&index);
    }
    @
    is this possible?
    [quote author="leon.anavi" date="1314767211"]The signal "expanded is already declared at class QTreeView":http://doc.qt.nokia.com/latest/qtreeview.html#expanded which is inherited by your class SelfDifineTree.[/quote]



  • [quote author="shen yang" date="1314767996"]Thx for your reply, it is true that expanded is already defined, and this is also my purpose, I want do overwrite this method
    [/quote]

    No, you can't because it is not declared as virtual and it is a signal.



  • expanded() is a signal, and is as leon.anavi rightly points out, not virtual. Instead of subclassing and reimplenting the method, why not simply create a new slot in your subclass, and connect to the signal instead?



  • Thx anyway, now I'm wondering what make this method expanded can not been overwrite, I can only see
    @

    define Q_SIGNALS protected

    @
    for this, I write a small test, when I call testp(), it call the testprotected() in childclass, really puzzled.
    parentclass.h
    @
    class ParentClass{
    public:
    ParentClass();
    virtual ~ParentClass();
    protected:
    void testprotected();
    };

    @
    childclass.h
    @
    #include "parentclass.h"

    class ChildClass:public ParentClass{
    public:
    ChildClass();
    void testp();
    virtual ~ChildClass();

    protected:
    void testprotected();
    };

    @
    parentclass.cpp
    @
    #include "parentclass.h"
    #include <QDebug>

    ParentClass:: ParentClass (){

        qDebug()<<"Build Parent";
    }
    

    ParentClass:: ~ParentClass(){
    qDebug()<<"delete parent";
    }

    void ParentClass:: testprotected(){
    qDebug()<<"testprotected parent";
    }

    @
    childclass.cpp
    @
    #include "childclass.h"
    #include <QDebug>

    ChildClass::ChildClass(){
    }
    ChildClass::~ChildClass(){
    }

    void ChildClass:: testprotected(){
    qDebug()<<"testprotected child";
    }

    void ChildClass:: testp(){
    testprotected();
    }
    @
    And even change to this, this small test can also,run.
    @

    define Q_SIGNALS protected

    class ParentClass{
    public:
    ParentClass();
    virtual ~ParentClass();
    Q_SIGNALS:
    void testprotected();
    };
    @

    [quote author="leon.anavi" date="1314768472"]
    [quote author="shen yang" date="1314767996"]Thx for your reply, it is true that expanded is already defined, and this is also my purpose, I want do overwrite this method
    [/quote]

    No, you can't because it is not declared as virtual and it is a signal.
    [/quote]



  • Got it, thx.
    [quote author="Andre" date="1314770348"]expanded() is a signal, and is as leon.anavi rightly points out, not virtual. Instead of subclassing and reimplenting the method, why not simply create a new slot in your subclass, and connect to the signal instead?[/quote]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.