Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView: How to catch header click event?
QtWS25 Last Chance

QTreeView: How to catch header click event?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sitesvS Offline
    sitesvS Offline
    sitesv
    wrote on last edited by sitesv
    #1

    I was tryed to do something like this:

    MainWindow.cpp:

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        model = new TreeModel();
        ui->tree->setModel(model);
        ui->tree->header()->setSectionsClickable(true);
        connect(ui->tree->header(), SIGNAL(sectionClicked(int)), this, SLOT(header_clicked(int)));
    }
    //---
    void MainWindow::header_clicked(int id){
        QMessageBox msgBox;
        msgBox.setText(QString("%1").arg(id));
        msgBox.exec();
    }
    

    MainWindow.h

    void header_clicked(int id);
    

    But after all there are no messages when I click on header. Something is blocking signal sectionClicked()...
    I was tried to use qt example "Editable Tree Model Example". There are no problems, slot is executing.
    What can influence on header clicking?

    jsulmJ 1 Reply Last reply
    0
    • sitesvS sitesv

      I was tryed to do something like this:

      MainWindow.cpp:

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          model = new TreeModel();
          ui->tree->setModel(model);
          ui->tree->header()->setSectionsClickable(true);
          connect(ui->tree->header(), SIGNAL(sectionClicked(int)), this, SLOT(header_clicked(int)));
      }
      //---
      void MainWindow::header_clicked(int id){
          QMessageBox msgBox;
          msgBox.setText(QString("%1").arg(id));
          msgBox.exec();
      }
      

      MainWindow.h

      void header_clicked(int id);
      

      But after all there are no messages when I click on header. Something is blocking signal sectionClicked()...
      I was tried to use qt example "Editable Tree Model Example". There are no problems, slot is executing.
      What can influence on header clicking?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @sitesv Did you make sure the connect() was successful? It has a boolean return value, check it.
      Also, did you call https://doc.qt.io/qt-5/qheaderview.html#setSectionsClickable ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @jsulm is correct:

        int main(int argc, char **argv)
        {
          QApplication app(argc, argv);
        
          QTreeWidget tw;
          tw.header()->setSectionsClickable(true);
          QObject::connect(tw.header(), &QHeaderView::sectionClicked,
                           qApp, &QCoreApplication::quit);
          tw.show();
          return app.exec();
        }
        

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • sitesvS Offline
          sitesvS Offline
          sitesv
          wrote on last edited by
          #4

          @jsulm, @Christian-Ehrlicher

          This "connect" works fine:

          QObject::connect(header(), &QHeaderView::sectionClicked, qApp, &QCoreApplication::quit);
          

          The trouble in my slot for undefined cause. The app just doesn't see my slot.
          I was tried "private slot:" section and "public slot:"... The same result...

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @sitesv said in QTreeView: How to catch header click event?:

            The app just doesn't see my slot.

            You simply forgot to make the section clickable as @jsulm already told you...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            sitesvS 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @sitesv said in QTreeView: How to catch header click event?:

              The app just doesn't see my slot.

              You simply forgot to make the section clickable as @jsulm already told you...

              sitesvS Offline
              sitesvS Offline
              sitesv
              wrote on last edited by sitesv
              #6

              @Christian-Ehrlicher ,

              1.png

              I see, that moc_mainwindow.cpp doesn't have "header_clicked" slot ((

              JonBJ 1 Reply Last reply
              0
              • sitesvS sitesv

                @Christian-Ehrlicher ,

                1.png

                I see, that moc_mainwindow.cpp doesn't have "header_clicked" slot ((

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @sitesv
                You would eliminate one source of possible error if you changed over your connect()s to https://wiki.qt.io/New_Signal_Slot_Syntax. Which is worth doing now in all cases....

                1 Reply Last reply
                1
                • sitesvS Offline
                  sitesvS Offline
                  sitesv
                  wrote on last edited by sitesv
                  #8

                  Problem solved!
                  The reason was a wrong moc_mainwindo.cpp file in root folder of my project.
                  I just deleted moc files and recompiled the project. Moc files were created in the debug folder. My slot appeared in moc_mainwindow.cpp!
                  it's working!! :)

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved