Solved QObject::connect: No such signal QTableWidget::itemChanged(QTableWidgetItem *item)
-
Could you edit my code ?
I would like to use QTableWidget::itemChanged .QMetaObject::connectSlotsByName: No matching signal for on_table_itemChanged(QTableWidgetItem*) QObject::connect: No such signal QTableWidget::itemChanged(QTableWidgetItem *item) in ../test/mainwindow.cpp:29 QObject::connect: (receiver name: 'MainWindow')
// mainewindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QtDebug> #include <QTableWidget> #include <QVBoxLayout> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QVBoxLayout *layout = new QVBoxLayout; QTableWidget* table = new QTableWidget(this); table->setRowCount(10); table->setColumnCount(5); for(int row = 0; row < 10; row++) { QTableWidgetItem *Item = new QTableWidgetItem(tr("%1").arg(row*2)); table->setItem(row, 0, Item); } layout->addWidget(table); MainWindow::centralWidget()->setLayout(layout); connect(table, SIGNAL(itemChanged(QTableWidgetItem *item)), SLOT(on_table_itemChanged(QTableWidgetItem *item))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_table_itemChanged(QTableWidgetItem *item) { qDebug() << item->text() << endl ; }
// mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QTableWidget> #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_table_itemChanged(QTableWidgetItem *item); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
-
Hi,
Change the name of your slot to something like
onTableItemChanged
. The on_widget_signalName notation is used for the connectsSlotsByName feature. Since you create the widget in the constructor, it's not yet available whenconnectsSlotsByName
is called throughui->setupUi(this)
. -
hi @masayoshi
first, you should change the name of your slot. with a name like
on_table_itemChanged
you'll run into issues with the automatic connect by name functionality of Qt.
I'll also suggest using the new Qt5 Syntax, it will report back compile time errors that are usually more useful.
//void on_table_itemChanged(QTableWidgetItem *item); void onTableItemChanged(QTableWidgetItem *item); ... connect(table, &QTableWidget::itemChanged, this, &MainWindow::onTableItemChanged);
-
@J.Hilk said in QObject::connect: No such signal QTableWidget::itemChanged(QTableWidgetItem *item):
hi @masayoshi
first, you should change the name of your slot. with a name like
on_table_itemChanged
you'll run into issues with the automatic connect by name functionality of Qt.
I'll also suggest using the new Qt5 Syntax, it will report back compile time errors that are usually more useful.
//void on_table_itemChanged(QTableWidgetItem *item); void onTableItemChanged(QTableWidgetItem *item); ... connect(table, &QTableWidget::itemChanged, this, &MainWindow::onTableItemChanged);
Thank you for reply. I got the following error.
// /usr/bin/ld: error: undefined symbol: MainWindow::onTableItemChanged(QTableWidgetItem*) >>> referenced by moc_mainwindow.cpp:74 >>> moc_mainwindow.o:(MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)) clang++: error: linker command failed with exit code 1 (use -v to see invocation) *** [test] Error code 1
// mainwindow.cpp
connect(table, &QTableWidget::itemChanged, this, &MainWindow::onTableItemChanged);
void MainWindow::onTableitemChanged(QTableWidgetItem *item)
{
qDebug() << item->text() << endl ;
}// mainwindow.h void onTableItemChanged(QTableWidgetItem *item);
-
@masayoshi
you should clean your build folder and rerun qmake.
Also did you by any chance forget to
#include <QTableWidgetItem>
? -
@masayoshi said in QObject::connect: No such signal QTableWidget::itemChanged(QTableWidgetItem *item):
void MainWindow::onTableitemChanged(QTableWidgetItem *item)
it should be Item not item:
onTableItemChanged
-
@jsulm said in QObject::connect: No such signal QTableWidget::itemChanged(QTableWidgetItem *item):
@masayoshi said in QObject::connect: No such signal QTableWidget::itemChanged(QTableWidgetItem *item):
void MainWindow::onTableitemChanged(QTableWidgetItem *item)
it should be Item not item:
onTableItemChanged
Thank you! It works.