QStandardItemModel itemChanged signal not working
-
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); }
-
Hi,
There's no signal with that signature. It's itemChanged(QStandardItem *)
-
@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); }
-
Do you modify any item somewhere else than in ItemChangeCallback ? Otherwise it will never be called.
-
@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...?
-
@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*))); }
-
You are editing the model before connecting the signal so you won't have anything emitted.
-
@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