Using function to do stuff in UI?
-
Hi!
I've been trying to for the past few days to create a small GUI-application in Qt and C++ but i'm having trouble calling a function to change stuff in the ui.
I'm having a really hard time wrapping my head around what, how and why the function call needs to be done .
Here's exactly what i want to do:
I have a Combobox in the ui that i want to add items to once at the start of the application and then every time a "Reload" button is pressed.I thought a function to do this would be best and then just call the function whenever i need to update the Combobox. Qt appearantly doesn't think the same.
I understand that i need to pass the data that i want in the combobox to the MainWindow somehow but i'm completely lost. I also don't know what i should type in google to find anything about it.
Please explain how this is done and also why.
//
Anders -
Hi Parre,
I would do it straight forward like this:
@
void init() {
updateComboBox(); // once at start time
}
void reload() {
updateComboBox(); // every time "Reload" was pressed
}
void updateComboBox() {
ui->comboBox->clear(); // clear all contents
// fill with new content
for( int i = 0; i < myComboBoxData.size(); ++i ) {
ui->comboBox->addItem(myComboBoxData[i].displayName);
}
}
@ -
That's what i've done but i get the error message:
error: 'ui' was not declared in this scopeI can 't see why it shouldn't work :S
[quote author="belab" date="1394391703"]Hi Parre,
I would do it straight forward like this:
@
void init() {
updateComboBox(); // once at start time
}
void reload() {
updateComboBox(); // every time "Reload" was pressed
}
void updateComboBox() {
ui->comboBox->clear(); // clear all contents
// fill with new content
for( int i = 0; i < myComboBoxData.size(); ++i ) {
ui->comboBox->addItem(myComboBoxData[i].displayName);
}
}
@[/quote] -
Hi Parre,
First of all, I recommend going through this tutorial: http://doc-snapshot.qt-project.org/qt5-stable/gettingstartedqt.html
Anyway, regarding your case,it depends on how your MainWindow is set up. You can find out by studying mainwindow.h.
belab's solution works if your MainWindow contains a ui pointer. However, your MainWindow probably inherits the object instead of storing its pointer. In this case, just remove "ui->". The names of your combo box variables are set in Qt Designer, under "objectName".