Get QComboBox reference to connect()
-
Hello,
I have a dialog with a QCombBox and QListWidget.
In another class I have an array who will fill a QListWidget when combobox changes.
In this same class, I try create a connect to a slot.MyDialog dlg; connect(dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&)));This code above don't work. Even if I put combobox and slot public.
But, if I put this code inside the dialog class, works fine.connect(mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&))); //into dialog classHow I can reference the combobox outside dialog class to connect in a slot?
My bests regards.
-
Hello,
I have a dialog with a QCombBox and QListWidget.
In another class I have an array who will fill a QListWidget when combobox changes.
In this same class, I try create a connect to a slot.MyDialog dlg; connect(dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&)));This code above don't work. Even if I put combobox and slot public.
But, if I put this code inside the dialog class, works fine.connect(mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&))); //into dialog classHow I can reference the combobox outside dialog class to connect in a slot?
My bests regards.
-
@koahnig No error, only the slot don't execute if I put the connect() outside dialog class.
@Helson
So you have a runtime problem.
The this pointer is different between both cases.Try the reference syntax of connect as given in the small example
Look out for
´´´
Counter a, b;
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
´´´ -
@Helson
So you have a runtime problem.
The this pointer is different between both cases.Try the reference syntax of connect as given in the small example
Look out for
´´´
Counter a, b;
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
´´´@koahnig
But in this way:
dlg.mpCourseComboBox
How I can pass the reference?connect(&dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&)));This give a compilation error:
error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'QComboBox **' to 'const QObject *' -
@koahnig
But in this way:
dlg.mpCourseComboBox
How I can pass the reference?connect(&dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(listLoadValues(const int&)));This give a compilation error:
error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'QComboBox **' to 'const QObject *'The references are meant for the 2nd and 4th parameter of the connect statement.
The 1st is already a pointer. The reference there yields a pointer to a pointer and that is wrong.
Please check out the documentation as linked above. There is also a wiki entry. Unfortunately, I do not remember where to find this. -
The references are meant for the 2nd and 4th parameter of the connect statement.
The 1st is already a pointer. The reference there yields a pointer to a pointer and that is wrong.
Please check out the documentation as linked above. There is also a wiki entry. Unfortunately, I do not remember where to find this. -
connect(&dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), &dlg, SLOT(&dlg.listLoadValues(const int&)));
This complile but the slot never execute.
You are mixing things now. Please checkout the reference syntax of connect as given in the small example
Create a small project and use the sections from the example. This shall give you the general understanding of using signals and slots in Qt. Possibly you can check out also the real example.
If your problem is small enough you can also post here the relevant code of your classes.
-
You are mixing things now. Please checkout the reference syntax of connect as given in the small example
Create a small project and use the sections from the example. This shall give you the general understanding of using signals and slots in Qt. Possibly you can check out also the real example.
If your problem is small enough you can also post here the relevant code of your classes.
File1.cpp Dlg::Dlg(QWidget *parent, const char *name):QDialog(parent) , mpGridLayout(NULL) , mpCourseComboBox(NULL) , mpTurmaListView(NULL) { setObjectName(name); setWindowTitle(tr("Envio de Material")); resize(356, 134); mpGridLayout = new QGridLayout(); setLayout(mpGridLayout); mpCourseComboBox = new QComboBox(); mpTurmaListView = new QListWidget(); mpGridLayout->addWidget(mpCourseComboBox, 5, 0, 1, 1); mpGridLayout->addWidget(mpTurmaListView, 22, 0, 1, 1); connect(mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(loadValuesListView(const int&))); //Its works fine }File1.cpp void Class1::do() { QList<QString> myArray; Dlg dlg; fill(myArray); QObject::connect(dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), &dlg, SLOT(loadValuesListView(const int&))); //Compile but the slot never execute... dlg.mpCourseComboBox(otherArray); /This fill combobox with one value if(QDialog::Accepted == dlg.exec()) { } }I'm new on QT, I need finish this project about 1 week... Sorry for my bad english and thank for you attention.
-
File1.cpp Dlg::Dlg(QWidget *parent, const char *name):QDialog(parent) , mpGridLayout(NULL) , mpCourseComboBox(NULL) , mpTurmaListView(NULL) { setObjectName(name); setWindowTitle(tr("Envio de Material")); resize(356, 134); mpGridLayout = new QGridLayout(); setLayout(mpGridLayout); mpCourseComboBox = new QComboBox(); mpTurmaListView = new QListWidget(); mpGridLayout->addWidget(mpCourseComboBox, 5, 0, 1, 1); mpGridLayout->addWidget(mpTurmaListView, 22, 0, 1, 1); connect(mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), this, SLOT(loadValuesListView(const int&))); //Its works fine }File1.cpp void Class1::do() { QList<QString> myArray; Dlg dlg; fill(myArray); QObject::connect(dlg.mpCourseComboBox, SIGNAL(currentIndexChanged(const int&)), &dlg, SLOT(loadValuesListView(const int&))); //Compile but the slot never execute... dlg.mpCourseComboBox(otherArray); /This fill combobox with one value if(QDialog::Accepted == dlg.exec()) { } }I'm new on QT, I need finish this project about 1 week... Sorry for my bad english and thank for you attention.
In principle your code looks reasonable. However, without the include files an essential key part is missing.
It is important everything is executed within an event loop. Also classes with signals and slots shall inherit from QObject and the Q_OBJECT has to be in those declarations.
QObject provides you also the dumpObjectInfo and dumpObjectTree member functions. Those will give you some important information on connections for your instances to the application output.What strikes me a bit strange is that you have not reported some output that a signal or a slot was not found. That should have happened already at the beginning. Also the connects are providing a return value which can be checked and shall tell you also if a connection was successful.
What debugger are you using?
-
In principle your code looks reasonable. However, without the include files an essential key part is missing.
It is important everything is executed within an event loop. Also classes with signals and slots shall inherit from QObject and the Q_OBJECT has to be in those declarations.
QObject provides you also the dumpObjectInfo and dumpObjectTree member functions. Those will give you some important information on connections for your instances to the application output.What strikes me a bit strange is that you have not reported some output that a signal or a slot was not found. That should have happened already at the beginning. Also the connects are providing a return value which can be checked and shall tell you also if a connection was successful.
What debugger are you using?
-
It's only a logic error!
I put the connect after change the add values on the dropdown!
I solve this changing the code order.Thank you so much!