Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Get parameter from QStackedWidget
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Get parameter from QStackedWidget

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eddi0406
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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

      Greetz, Jeroen

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eddi0406
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved