Solved QTreeView: How to catch header click event?
-
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? -
@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 ? -
@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(); }
-
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... -
@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...
-
-
@sitesv
You would eliminate one source of possible error if you changed over yourconnect()
s to https://wiki.qt.io/New_Signal_Slot_Syntax. Which is worth doing now in all cases.... -
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!! :)