Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. QItemSelectionModel signals doesnt work
Qt 6.11 is out! See what's new in the release blog

QItemSelectionModel signals doesnt work

Scheduled Pinned Locked Moved Solved Qt 6
5 Posts 3 Posters 1.4k Views 2 Watching
  • 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.
  • PbaodogeP Offline
    PbaodogeP Offline
    Pbaodoge
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #4

      I think you need to specify the SelectionModel for your QTreeView. Note, this must be done after you set the view's model (see the note here).

      1 Reply Last reply
      2
      • PbaodogeP Pbaodoge

        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.

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

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

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

        1 Reply Last reply
        0
        • PbaodogeP Offline
          PbaodogeP Offline
          Pbaodoge
          wrote on last edited by
          #3

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mchinand
            wrote on last edited by mchinand
            #4

            I think you need to specify the SelectionModel for your QTreeView. Note, this must be done after you set the view's model (see the note here).

            1 Reply Last reply
            2
            • PbaodogeP Offline
              PbaodogeP Offline
              Pbaodoge
              wrote on last edited by
              #5

              That works perfectly. Thanks a lot

              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