QWidget not response on ComboBox action
-
Hi, I am new to Qt. I encountered a problem related to QWidget. I setup a QWidget, which has a ComboBox selector. My goal is to use this selector to control some behavior in the main app. I initiated the QWidget in the main ui, but when I change the currentText of the ComboBox, it seems that it does nothing at all. Below is my code:
surfaceproperty.h:
namespace Ui { class SurfaceProperty; } class SurfaceProperty : public QWidget { Q_OBJECT public: explicit SurfaceProperty(QWidget *parent = 0); ~SurfaceProperty(); public slots: void on_selectorSurfaceColormap_currentTextChanged(const QString &arg1); public: Ui::SurfaceProperty *uisp; };
surfaceproperty.cpp:
#include "surfaceproperty.h" #include "ui_surfaceProperty.h" SurfaceProperty::SurfaceProperty(QWidget *parent) : QWidget(parent), uisp(new Ui::SurfaceProperty) { uisp->setupUi(this); } SurfaceProperty::~SurfaceProperty() { delete uisp; } void SurfaceProperty::on_selectorSurfaceColormap_currentTextChanged(const QString &arg1) { qInfo() << arg1; }
Part of main app here:
QWidget *t = new QWidget; uif->uisp->setupUi(t); t->show(); t->setWindowTitle("Surface Properties"); t->setAttribute(Qt::WA_QuitOnClose, false); uif->uisp->selectorSurfaceColormap->setCurrentText("test");
where uif is a SurfaceProperty* I defined here in the main app.
I did not post my ui file here.
Basically, I expect that when this part of the main app is called, a widget window appears, and when the ComboBox's currentText is changed, I can get a qInfo() output at the cmd (I build it under Linux). But I only get a widget window when this part is called. I did not see the qInfo() output at all, meaning the ComboBox part is not functional at all.
Could anyone indicate what is the problem? Did I miss some signal & slot thing?
Thank you very much!
-
Please do not use the auto-connect feature but connect the signals/slots by your own. Maybe you've a type on the member name (selectorSurfaceColormap) so the auto-connect feature can not find the slot.
-
@Christian-Ehrlicher Hi, Christian, thanks for the reply but I am a little confused. Why the auto-connect feature is not encouraged? Is this true for all windows or only qwidget? I have many auto-connects in the main app, they all work very well. I also did not quite get it on this line: "...you've a type on the member name". Could you indicate? Thanks a lot!
-
I said 'Maybe you've a typo...'
The problem with auto-connect is that you can not be sure if it is really connected (e.g. when there is a typo somewhere in the functions signature or the ui element name) and it easily breaks (e.g. when renaming an ui element).