Add item to combobox when it is "touched"
-
I would like to add a list of items (as example a list of files in a folder) to a combobox
dynamically when the user clicks on this combobox.I tried this - by the way how would this be in the new syntax?
connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));
And I tried:
void MyApp::on_combo_Measurements_highlighted(int index) { QStringList files = measurementsFolder.entryList(QStringList() << "*.*", QDir::Files); ui->combo_Measurements->clear(); if(! files.isEmpty()) { foreach (QString fileName, files) { QString filePath = QDir::toNativeSeparators(measurementsFolder.filePath(fileName)); ui->combo_Measurements->addItem(fileName, filePath); } } }
The later is partially "working" but I cant select any entry then, because the actualisation takes permanently place. Therefore I am looking for the better (correct) way of doing this.
-
@ademmler said in Add item to combobox when it is "touched":
connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));
What is this signal?
IIRCaboutToShow()
is a signal fromQMenu
. There is nothing like that inQComboBox
.he later is partially "working" but I cant select any entry then, because the actualisation takes permanently place
Then don't choose the
highlighted
signal as trigger for your code.
Put it in some extra function and call this function inmousePressEvent
, for example.Or work with
QComboBox
ability to display model data. Create a model which holds your data and let theQComboBox
view display it. -
@Pl45m4 said in Add item to combobox when it is "touched":
Hi, thx for your feedback.
@ademmler said in Add item to combobox when it is "touched":
connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList()));
What is this signal?
IIRCaboutToShow()
is a signal fromQMenu
. There is nothing like that inQComboBox
.This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.
he later is partially "working" but I cant select any entry then, because the actualisation takes permanently place
Then don't choose the
highlighted
signal as trigger for your code.
Put it in some extra function and call this function inmousePressEvent
, for example.Or work with
QComboBox
ability to display model data. Create a model which holds your data and let theQComboBox
view display it.Ok, thx for the hint. I actually try this and it seems to work:
ui->combo_Measurements->installEventFilter(this); .... bool MyApp::eventFilter(QObject *f_object, QEvent *f_event){ if(f_object == ui->combo_Measurements){ if(f_event->type() == QEvent::MouseButtonPress){ slotGetMeasurementsList(); } return false; } return false; }
-
@ademmler said in Add item to combobox when it is "touched":
This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.
Do not ever use old-style signal/slot syntax. It was replaced over a decade ago.
connect(ui->combo_Measurements, SIGNAL(aboutToShow()), this, SLOT(slotGetMeasurementsList())); # replace by connect(ui->combo_Measurements, &QMenu::aboutToShow, this, &ThisClass::slotGetMeasurementsList);
If you did this you would see, and the compiler would complain, that
QMenu::aboutToShow
does not go withui->combo_Measurements
, which is presumably aQComboBox
. -
@ademmler said in Add item to combobox when it is "touched":
This was simply a try - because with menus for example it works and the compiler/Qt did not complained about this.
Haha, you can't just throw signals and classes together at random. It won't work like this ;-)
I think you know that :)It doesn't complain, because the string based (with
SIGNAL
andSLOT
) connection does not perform a check (signal + slot compatible, signal/slot valid, matching args, etc...) while compiling.
Like @JonB said, do yourself a favor and stop using this type of connection ;-)
With function pointer based connections, you can even connect to functions which are not explicitely declaredQ_SLOT
... and you see right away whether your connection will work or not.Btw:
QObject::connect(....)
has a return value, that you can check or output withqDebug()
for example. -
thx for teaching me this - its very helpful to see old and new syntax side by side.
However I try to use eventFilter instead. As you can see from my post above.
Basically it works, but after a while (some mouse clicks in the UI) the event filter seems to be "vanished" or better to say does not work any longer.
Any idea what's causing this or what I do wrong here?