Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved QTreeView: How to catch header click event?

    General and Desktop
    4
    8
    944
    Loading More Posts
    • 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.
    • sitesv
      sitesv last edited by 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?

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @sitesv last edited by

        @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 Reply Quote 4
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

          @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 has to stay free or it will die.

          1 Reply Last reply Reply Quote 2
          • sitesv
            sitesv last edited by

            @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 Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              @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 has to stay free or it will die.

              sitesv 1 Reply Last reply Reply Quote 0
              • sitesv
                sitesv @Christian Ehrlicher last edited by sitesv

                @Christian-Ehrlicher ,

                1.png

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

                JonB 1 Reply Last reply Reply Quote 0
                • JonB
                  JonB @sitesv last edited by

                  @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 Reply Quote 1
                  • sitesv
                    sitesv last edited by sitesv

                    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 Reply Quote 0
                    • First post
                      Last post