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. Return custom widget from QStackedWidget
Forum Updated to NodeBB v4.3 + New Features

Return custom widget from QStackedWidget

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 534 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.
  • A Offline
    A Offline
    AaronKelsey
    wrote on last edited by
    #1

    I am trying to access a widget that inherits QWidget which is added to my QStackedWidget.

    I have for example a class "PageTwo" that inherits QWidget. This "PageTwo" is added to my QStackedWidget. I call get function in a unrelated class that return the QStackedWidget object, which I then use roughly "Object->getStackedWidget()->widget(PageTwo)" but it complains that it cannot convert from "QWidget *" to "PageTwo".

    Am I doing this wrong? I need to be able to access the stackedwidget from all pages so that I can call a function on each page just before going to it.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      From the description it looks like are trying to assign base class pointer to derived class pointer. Hence hitting the issue

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        You can access "Pages" with
        http://doc.qt.io/qt-5/qstackedwidget.html#widget
        it returns a base QWidget pointer so you have to cast it to your type.

        PageTwo *p2=qobject_cast<PageTwo *>( StackedWidget ->widget(0) );
        if(p2) { // always check if cast worked. else it be null
        use the custom page.
        }

        A 1 Reply Last reply
        4
        • mrjjM mrjj

          Hi
          You can access "Pages" with
          http://doc.qt.io/qt-5/qstackedwidget.html#widget
          it returns a base QWidget pointer so you have to cast it to your type.

          PageTwo *p2=qobject_cast<PageTwo *>( StackedWidget ->widget(0) );
          if(p2) { // always check if cast worked. else it be null
          use the custom page.
          }

          A Offline
          A Offline
          AaronKelsey
          wrote on last edited by
          #4

          @mrjj Thank you very much!

          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