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. QStandardItemModel itemChanged signal not working
Forum Updated to NodeBB v4.3 + New Features

QStandardItemModel itemChanged signal not working

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 7.1k 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.
  • J Offline
    J Offline
    justiliang
    wrote on last edited by
    #1

    I created a QTableView which uses a QStandardItemModel populated with QStandardItem. I want it so that when I edit the values in one of the QStandardItem cells, a signal is emitted that will call the ItemChangeCallBack(QStandardItem&) function to change the color of the cell background.

    I have a TestGUI class that has the following member function which creates the data table. I tried debugging and it seems like the CallBack does not even get called.

    void TestGUI::CreateDataTables() {
    	QTableView* tableView = new QTableView(mainWindow);
    	QStandardItemModel* model = new QStandardItemModel(tableView);
    	for (uint8_t row = 0; row < 5; ++row) {
    		model->setItem(row, 0, new QStandardItem("test"));
    	}
    	tableView->setModel(model);
    	_mainLayout->addWidget(tableView);
    	QObject::connect(model, SIGNAL(itemChanged(QStandardItem&)), this, SLOT(ItemChangeCallback(QStandardItem&)));
    
     }
    

    Here is the ItemChangeCallback:

    void TestGUI::ItemChangeCallback(QStandardItem& item) {
    
    	item.setBackground(Qt::GlobalColor::red);
    
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      There's no signal with that signature. It's itemChanged(QStandardItem *)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        There's no signal with that signature. It's itemChanged(QStandardItem *)

        J Offline
        J Offline
        justiliang
        wrote on last edited by
        #3

        @SGaist Hmm, I actually tried that as well and had the same problem.

        With the new code I have:

        void TestGUI::CreateDataTables() {
        	QTableView* tableView = new QTableView(mainWindow);
        	QStandardItemModel* model = new QStandardItemModel(tableView);
        	for (uint8_t row = 0; row < 5; ++row) {
        		model->setItem(row, 0, new QStandardItem("test"));
        	}
        	tableView->setModel(model);
        	_mainLayout->addWidget(tableView);
        	QObject::connect(model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(ItemChangeCallback(QStandardItem*)));
        
         }
        

        Here is the ItemChangeCallback:

        void TestGUI::ItemChangeCallback(QStandardItem* item) {
        
        	item->setBackground(Qt::GlobalColor::red);
        
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you modify any item somewhere else than in ItemChangeCallback ? Otherwise it will never be called.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 2 Replies Last reply
          1
          • SGaistS SGaist

            Do you modify any item somewhere else than in ItemChangeCallback ? Otherwise it will never be called.

            J Offline
            J Offline
            justiliang
            wrote on last edited by justiliang
            #5

            @SGaist Hmmm, don't think so, the item and model are only instantiated in the CreateDataTables() function so they can't be accessed without signals and slots. I think...

            Maybe I have to connect them in the constructor...?

            1 Reply Last reply
            0
            • SGaistS SGaist

              Do you modify any item somewhere else than in ItemChangeCallback ? Otherwise it will never be called.

              J Offline
              J Offline
              justiliang
              wrote on last edited by justiliang
              #6

              @SGaist Hmm, although I do edit the model, I didn't give you all my code as I thought some of it would be irrelevant but here I also give the headers names:

              void TestGUI::CreateDataTables() {
              	QTableView* tableView = new QTableView(mainWindow);
              	QStandardItemModel* model = new QStandardItemModel(tableView);
              	QStringList headerList;
                  for (uint8_t row = 0; row < 5; ++row) {
                      model->setItem(row, 0, new QStandardItem("test"));
                      headerList.append(QString("Some Header Names"));
                      model->setVerticalHeaderLabels(headerList);
                      model->setHorizontalHeaderLabels(QStringList("Values"));
              	}
              	tableView->setModel(model);
              	_mainLayout->addWidget(tableView);
              	QObject::connect(model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(ItemChangeCallback(QStandardItem*)));
              
               }
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You are editing the model before connecting the signal so you won't have anything emitted.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  alex_malyu
                  wrote on last edited by
                  #8

                  @justiliang said:

                  QObject::connect(model

                  Also I highly recommend always check result connect returns

                  bool ok = QObject::connect(model .....
                  Q_ASSERT( ok );

                  This wil save you a lot of time

                  1 Reply Last reply
                  1

                  • Login

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