Solved How to get ComboBox->CurrentIndex() in a StackedWidget?
-
Hello,
I am learning Qt and I have this problem;
I would like to get current index number which is stated in a stackedWidget.
I have one ui class and my project structure is just like that;centralWidget -stackedWidget (main stackedWidget) -->Index0 --- -->Index1 ---StackedWidget_2 (another stackedWidget stated in main one) ----->Index0 --------comboBox (the comboBox I would like to read currentIndex) ----->Index1 ----->Index2 -->Index2 ---
What i have tried so far;
int number= ui->stackedWidget->widget(0)->comboBox->currentIndex();
which gives me an error as widget(0) has no membernamed "comboBox".
and this;
QStackedWidget *stackedWidget = new QStackedWidget; stackedWidget->addWidget(new QWidget); // index 0 auto* widget = stackedWidget->widget(0); if (widget) { number=widget->comboBox->currentIndex(); }
Thanks for any suggestions.
-
Hi,
Based on your code:
auto* widget = stackedWidget->widget(0);
widget is an empty widget nothing more.
With this modified version:
QStackedWidget *stackedWidget = new QStackedWidget; stackedWidget->addWidget(new QComboBox); // index 0 auto* comboBox = qobject_cast<QComboBox *>(stackedWidget->widget(0)); number = comboBox->currentIndex();
It should be clearer I think.
-
I would like to get current index number which is stated in a stackedWidget.
What does this, and your code attempts, mean? Have you (somewhere) created some combobox and put it on a page/widget which is to be shown in the stacked widget? If not, there is no combobox anywhere. Are you talking about which page/widget is currently active in the stacked widget, which is not shown anywhere and has nothing to do with a combobox? Or what?
EDIT My reply has crossed with @SGaist's. As usual, he may have done a better job at guessing your intention than I! :)
-
@SGaist thank you for quick respond!
I forgot to mention that I already created these; stackedWidget and comboBox in my ui file. I just wanted to reach it's currentindex. However, I neither get any result nor any error with your implementation. I think, I need to mention also this;
I have one ui class and my project structure is just like that;centralWidget -stackedWidget (main stackedWidget) -->Index0 --- -->Index1 ---StackedWidget_2 (another stackedWidget stated in main one) ----->Index0 --------comboBox (the comboBox I would like to read currentIndex) ----->Index1 ----->Index2 -->Index2 ---
@JonB Thank you for your reply also, to be honest it did immidiately when I saw the code he wrote:)
I did not mention all about these because I thought that I made a mistake in this code line and solution would be simpler than this :/
int number= ui->stackedWidget_2->Index(0)->comboBox->currentIndex();I will edit the main question properly, when the answer is enough to solve the problem. Thanks again.
-
So in fact you have have a QComboBox which is inside a QStackedWidget which is itself inside a QStackedWidget.
So it's going to be:
ui->comboBox->currentIndex();
Yes, it's unrelated but since you are using designer each widget like the QComboBox is directly accessible by its "name" through the
ui
object.If you want to go through all the steps because you constructed the whole stuff, then you'll have to add one step that first extracts the secondary QStackedWidget from the first one and then the combobox from that one.
-
@SGaist wow I did not expect to reach from directly ui form.
It works great now thank you for your replies!