QItemSelectionModel signals doesnt work
-
Hi, I'm trying to use QItemSelectionModel to display a list of info(just for some reference). My code works and the window is displayed.
Here's my code:#include <QApplication> #include <QMainWindow> #include <QSplitter> #include <QTreeView> #include <QItemSelectionModel> #include <QStandardItemModel> #include <QObject> #include "mainwindow.hpp" #include <QListView> #include <QHBoxLayout> int main( int argc, char **argv ) { QApplication app( argc, argv ); QTreeView *tree = new QTreeView(); QWidget *w = new QWidget; QHBoxLayout *layout = new QHBoxLayout; layout->addWidget(tree); w->setLayout(layout); QItemSelectionModel mselect; QStandardItemModel model(5, 2); MainWindow *st = new MainWindow(); for( int r=0; r<5; r++ ) for( int c=0; c<2; c++) { QStandardItem *item = new QStandardItem( QString("Row:%0, Column:%1").arg(r).arg(c) ); if( c == 0 ) for( int i=0; i<3; i++ ) { QStandardItem *child = new QStandardItem( QString("Item %0").arg(i) ); child->setEditable(false); item->appendRow(child); } model.setItem(r, c, item); } model.setHorizontalHeaderItem(0, new QStandardItem("C1")); model.setHorizontalHeaderItem(1, new QStandardItem("C2")); mselect.setModel(&model); tree->setModel(&model); //Signals And Slots setup QObject::connect(&mselect, &QItemSelectionModel::selectionChanged, st, &MainWindow::notification); //1 QObject::connect(&mselect, &QItemSelectionModel::currentColumnChanged, st, &MainWindow::notification); //2 QObject::connect(&mselect, &QItemSelectionModel::currentRowChanged, st, &MainWindow::notification); //3 w->show(); return app.exec(); }
The problem starts at the signals and slots connection. I tried every signal, but none of them are emitted when an action is executed.
Is there anything wrong with my code?
Thank you. -
@Pbaodoge said in QItemSelectionModel signals doesnt work:
w->show();
Why do you have this widget AND MainWindow. You never show your MainWindow.
How do you know that MainWindow::notification is never called? Do you have any debug output there? -
I wasn't really wanted to use MainWindow class. If you want the MainWindow class code then here it is:
.hpp#ifndef MAINWINDOW_HPP #define MAINWINDOW_HPP #include <QMainWindow> #include <QDebug> #include <QApplication> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); public slots: void notification(); }; #endif // MAINWINDOW_HPP
.cpp
#include "mainwindow.hpp" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { } void MainWindow::notification() { qDebug() << "Current Item has changed!\n"; }
I tried to use
qDebug()
to output emitted signals. And after many attempts on the Window, it is still not working.