[SOLVED] Get parameter from QStackedWidget



  • Hi Guys,

    I need to get some parameters (public member variables) from a QStackedwidget, which is a member of my first class:

    // class 1 constructor
    stackedWidget = new QStackedWidget;
    stackedWidget ->addWidget(new WidgetPage); // is a class inherited from QWidget

    // class 1 method, here I want to get a parameter of the current selected widget
    QWidget curWidget = pagesWidget->currentWidget();
    QString value = curWidget->findChild<WidgetPage
    >()->comboBox1->currentText();

    The code compiles, but last line give me an "access violation while reading" error.

    What could be the problem with this? Is there a better approach to get members from the widgets placed on my QStackedWidget?

    Best regards,
    Eddi0406

    edit:
    I think I have to cast the currentWidget() return widget to my specific type. Then I should be able to use the member variables of WidgetPage. But how can I know, which is the right destination? I have different widgets in my stackwidget.



  • Yup, What if the findChild results in a empty pointer?? Then you simply call the currenText anyway. Not great programming. When you do a get function with pointers or any conversion. First check if the conversion worked.
    And I do not think what you want is coded here. Shouldn't you need to do a dynamic cast?
    @
    QWidget * curWidget = pagesWidget->currentWidget() // So we have the pointer
    WidgetPage * MyPointer (dynamic_cast<WidgetPage*>(curWidget));
    if (WidgetPage != NULL)
    {
    // So we have a valid pointer to your class
    QString Value(WidgetPage->comboBox1->currentText());
    }
    @
    Damm, Something bugged me after a while. Getting a value straight from you GUI elements is not really a great thing. When using designer the GUI elements are always private and that is the best way to go! Use getters/Setters as interface for your WidgetPage



  • That's the line I was looking for, thank you very much!

    You are absolutely right, of course I have to check whether the pointer is valid or not.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.