QComboBox doesn't works change event
-
Change item event on combo box doesn't work.
If you look up a YouTube video, it shows selecting a combobox item and pressing a button.
Doesn't signal work when changing combobox item?
If so, why am I not working?
As a test, I made a push button and added a click signal event. But this also doesn't work.void ResizeDlg::on_comboWidthSize_currentIndexChanged(int index) { qDebug() << "test"; .... } void ResizeDlg::on_pushButton_clicked() { qDebug() << "test"; }
-
@Christian-Ehrlicher
Did you mean this?ResizeDlg::ResizeDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::ResizeDlg) { ui->setupUi(this); QStringList widthSizeOpt; widthSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters"; ui->comboWidthSize->addItems(widthSizeOpt); ui->comboWidthSize->setCurrentIndex(2); QStringList heightSizeOpt; heightSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters"; ui->comboHeightSize->addItems(heightSizeOpt); ui->comboHeightSize->setCurrentIndex(2); QObject::connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(on_comboWidthSize_currentIndexChanged(int))); }
But this doesn't work!
@MyNameIsQt
It should. Do you mean nothing happens at runtime when you do the action? (In fact because of how it's named and you have auto-connections enabled, it should get invoked twice). Both of your cases should. Start from these:connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::on_comboWidthSize_currentIndexChanged); connect(ui->pushButton, &QPushButton::clicked, this, &ResizeDlg::on_pushButton_clicked); connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, []() { qDebug() << "QComboBox::currentIndexChanged"; } ); connect(ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "QPushButton::clicked"; } );
-
Change item event on combo box doesn't work.
If you look up a YouTube video, it shows selecting a combobox item and pressing a button.
Doesn't signal work when changing combobox item?
If so, why am I not working?
As a test, I made a push button and added a click signal event. But this also doesn't work.void ResizeDlg::on_comboWidthSize_currentIndexChanged(int index) { qDebug() << "test"; .... } void ResizeDlg::on_pushButton_clicked() { qDebug() << "test"; }
Please do not use the auto-connect stuff but connect the signals and slots by yourself. I would guess the name of your combobox is not
comboWidthSize
-
Please do not use the auto-connect stuff but connect the signals and slots by yourself. I would guess the name of your combobox is not
comboWidthSize
@Christian-Ehrlicher
Did you mean this?ResizeDlg::ResizeDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::ResizeDlg) { ui->setupUi(this); QStringList widthSizeOpt; widthSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters"; ui->comboWidthSize->addItems(widthSizeOpt); ui->comboWidthSize->setCurrentIndex(2); QStringList heightSizeOpt; heightSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters"; ui->comboHeightSize->addItems(heightSizeOpt); ui->comboHeightSize->setCurrentIndex(2); QObject::connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(on_comboWidthSize_currentIndexChanged(int))); }
But this doesn't work!
-
@Christian-Ehrlicher
Did you mean this?ResizeDlg::ResizeDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::ResizeDlg) { ui->setupUi(this); QStringList widthSizeOpt; widthSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters"; ui->comboWidthSize->addItems(widthSizeOpt); ui->comboWidthSize->setCurrentIndex(2); QStringList heightSizeOpt; heightSizeOpt << "Percent" << "Inches" << "Centimeters" << "Millimeters"; ui->comboHeightSize->addItems(heightSizeOpt); ui->comboHeightSize->setCurrentIndex(2); QObject::connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(on_comboWidthSize_currentIndexChanged(int))); }
But this doesn't work!
@MyNameIsQt
It should. Do you mean nothing happens at runtime when you do the action? (In fact because of how it's named and you have auto-connections enabled, it should get invoked twice). Both of your cases should. Start from these:connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::on_comboWidthSize_currentIndexChanged); connect(ui->pushButton, &QPushButton::clicked, this, &ResizeDlg::on_pushButton_clicked); connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, []() { qDebug() << "QComboBox::currentIndexChanged"; } ); connect(ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "QPushButton::clicked"; } );
-
@MyNameIsQt
It should. Do you mean nothing happens at runtime when you do the action? (In fact because of how it's named and you have auto-connections enabled, it should get invoked twice). Both of your cases should. Start from these:connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::on_comboWidthSize_currentIndexChanged); connect(ui->pushButton, &QPushButton::clicked, this, &ResizeDlg::on_pushButton_clicked); connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, []() { qDebug() << "QComboBox::currentIndexChanged"; } ); connect(ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "QPushButton::clicked"; } );
.h file
#include <QComboBox> class ResizeDlg : public QDialog { private slots: void convertPixel(); void convertSize(); ... }
.cpp file
ResizeDlg::ResizeDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::ResizeDlg) { ui->setupUi(this); connect(ui->comboWidth, SIGNAL(currentIndexChanged(int)), this, SLOT(convertPixel())); connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(convertSize())); } void ResizeDlg::convertPixel() { QMessageBox::information(nullptr, "test", "text."); qDebug() << "Hello"; } void ResizeDlg::convertSize() { QMessageBox::information(nullptr, "test", "text."); qDebug() << "Hello"; }
Application Output
qt.core.qobject.connect: QObject::connect: No such slot QDialog::convertPixel() in ..\WpubViewer\resizedlg.cpp:33 qt.core.qobject.connect: QObject::connect: (sender name: 'comboWidth') qt.core.qobject.connect: QObject::connect: (receiver name: 'ResizeDlg') qt.core.qobject.connect: QObject::connect: No such slot QDialog::convertSize() in ..\WpubViewer\resizedlg.cpp:34 qt.core.qobject.connect: QObject::connect: (sender name: 'comboWidthSize') qt.core.qobject.connect: QObject::connect: (receiver name: 'ResizeDlg')
Why does this message occur?
-
-
@MyNameIsQt
It should. Do you mean nothing happens at runtime when you do the action? (In fact because of how it's named and you have auto-connections enabled, it should get invoked twice). Both of your cases should. Start from these:connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::on_comboWidthSize_currentIndexChanged); connect(ui->pushButton, &QPushButton::clicked, this, &ResizeDlg::on_pushButton_clicked); connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, []() { qDebug() << "QComboBox::currentIndexChanged"; } ); connect(ui->pushButton, &QPushButton::clicked, this, []() { qDebug() << "QPushButton::clicked"; } );
@JonB Thanks. It was solved thanks to you.
:)connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::convertPixel); connect(ui->comboWidth, &QComboBox::currentIndexChanged, this, &ResizeDlg::convertSize);
-
@JonB Thanks. It was solved thanks to you.
:)connect(ui->comboWidthSize, &QComboBox::currentIndexChanged, this, &ResizeDlg::convertPixel); connect(ui->comboWidth, &QComboBox::currentIndexChanged, this, &ResizeDlg::convertSize);
@MyNameIsQt
I am glad you have changed over to the Qt New Signal Slot Syntax, which was brought in a decade ago. I recommend you now use this and no longerSIGNAL
/SLOT()
macros going forward. -
.h file
#include <QComboBox> class ResizeDlg : public QDialog { private slots: void convertPixel(); void convertSize(); ... }
.cpp file
ResizeDlg::ResizeDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::ResizeDlg) { ui->setupUi(this); connect(ui->comboWidth, SIGNAL(currentIndexChanged(int)), this, SLOT(convertPixel())); connect(ui->comboWidthSize, SIGNAL(currentIndexChanged(int)), this, SLOT(convertSize())); } void ResizeDlg::convertPixel() { QMessageBox::information(nullptr, "test", "text."); qDebug() << "Hello"; } void ResizeDlg::convertSize() { QMessageBox::information(nullptr, "test", "text."); qDebug() << "Hello"; }
Application Output
qt.core.qobject.connect: QObject::connect: No such slot QDialog::convertPixel() in ..\WpubViewer\resizedlg.cpp:33 qt.core.qobject.connect: QObject::connect: (sender name: 'comboWidth') qt.core.qobject.connect: QObject::connect: (receiver name: 'ResizeDlg') qt.core.qobject.connect: QObject::connect: No such slot QDialog::convertSize() in ..\WpubViewer\resizedlg.cpp:34 qt.core.qobject.connect: QObject::connect: (sender name: 'comboWidthSize') qt.core.qobject.connect: QObject::connect: (receiver name: 'ResizeDlg')
Why does this message occur?
@MyNameIsQt said in QComboBox doesn't works change event:
Why does this message occur?
Because you did not add the Q_OBJECT macro
-
@MyNameIsQt said in QComboBox doesn't works change event:
Why does this message occur?
Because you did not add the Q_OBJECT macro
@Christian-Ehrlicher
Ah, I didn't know that must apply with old-style connect. I nearly mentioned this to user, but it seems it all works with new-style syntax. My understanding is you needQ_OBJECT
if you emit signals, but you can get away without if you only connect/use slots, right? -
@Christian-Ehrlicher
Ah, I didn't know that must apply with old-style connect. I nearly mentioned this to user, but it seems it all works with new-style syntax. My understanding is you needQ_OBJECT
if you emit signals, but you can get away without if you only connect/use slots, right?@JonB said in QComboBox doesn't works change event:
My understanding is you need Q_OBJECT if you emit signals, but you can get away without if you only connect/use slots, right?
No, you need it when you want qobject_cast<> or old style connect or any other QMetaObject feature.
-
@JonB said in QComboBox doesn't works change event:
My understanding is you need Q_OBJECT if you emit signals, but you can get away without if you only connect/use slots, right?
No, you need it when you want qobject_cast<> or old style connect or any other QMetaObject feature.
@Christian-Ehrlicher
I cannot test, but I thought if you havesignals: void someSignal();
you also need
Q_OBJECT
else you get some moc error or something? -
@Christian-Ehrlicher
I cannot test, but I thought if you havesignals: void someSignal();
you also need
Q_OBJECT
else you get some moc error or something?@JonB Maybe a linker error (vtable) - don't know.