[Solved] Subclass QHeaderView.



  • I need to manipulate use signal from QHeaderView to bind some actiions.
    This is working . Here I Defined the slot in my mainclass.

    filemanager.h
    [code] void sectionClicked( int index);
    [/code]

    filemanager.cpp
    [code]QObject::connect( ui->treeView->header (),SIGNAL( sectionClicked( int ) ), this, SLOT(HDA->sectionClicked(int)));
    .....
    void FileManager:::sectionClicked( int index)
    {
    QMessageBox::about(this,"Header Click Detected!"
    ,"Index:"+QString::number(index));
    }[/code]

    When I try to put the code for sectionClicked in a separate class, it either not compiles
    as soon I am trying to define a pointer to that class. As I understand it should be because Q_OBJECTneeds to be declared in that class.

    Without Q_OBJECT, no problem with compilation - but the function will never execute.

    Is there any workarounds.?


  • Moderators

    QObject::connect( ui->treeView->header (),SIGNAL( sectionClicked( int ) ), this, SLOT(HDA->sectionClicked(int)));

    If HDA is the name of class that has sectionClicked slot then this should be:

    HDA* hda = ... // instance of HDA class
    QObject::connect( ui->treeView->header(), SIGNAL( sectionClicked( int ) ), hda, SLOT(sectionClicked(int)));
    

    If you want to connect a slot then that slot needs to be defined. You can only define slots in QObject derived classes with Q_OBJECT macro used.

    But you can connect to anything callable, not just slots. So if sectionClicked(int) is a member function in class HDA then you can connect it like this too:

    HDA* hda = ... // instance of HDA class
    QObject::connect(ui->treeView->header(), &QHeaderView::sectionClicked, hda, &HDA::sectionClicked);
    


  • Oh, I see. Thank you sir.



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