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?
Forum Updated to NodeBB v4.3 + New Features

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.
  • S Offline
    S Offline
    sitesv
    wrote on 26 Apr 2020, 20:34 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?

    J 1 Reply Last reply 27 Apr 2020, 04:55
    0
    • S sitesv
      26 Apr 2020, 20:34

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 27 Apr 2020, 04:55 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
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 27 Apr 2020, 06:41 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
        • S Offline
          S Offline
          sitesv
          wrote on 27 Apr 2020, 09:33 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
          • C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 27 Apr 2020, 09:39 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

            S 1 Reply Last reply 27 Apr 2020, 09:46
            0
            • C Christian Ehrlicher
              27 Apr 2020, 09:39

              @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...

              S Offline
              S Offline
              sitesv
              wrote on 27 Apr 2020, 09:46 last edited by sitesv
              #6

              @Christian-Ehrlicher ,

              1.png

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

              J 1 Reply Last reply 27 Apr 2020, 09:51
              0
              • S sitesv
                27 Apr 2020, 09:46

                @Christian-Ehrlicher ,

                1.png

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

                J Online
                J Online
                JonB
                wrote on 27 Apr 2020, 09:51 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
                • S Offline
                  S Offline
                  sitesv
                  wrote on 27 Apr 2020, 10:00 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

                  1/8

                  26 Apr 2020, 20:34

                  • Login

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