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]How to dynamic change the type of QWidget shown to the user
QtWS25 Last Chance

[SOLVED]How to dynamic change the type of QWidget shown to the user

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.5k Views
  • 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.
  • T Offline
    T Offline
    thiberi0
    wrote on last edited by
    #1

    I am having a problem to make the QWidget change dynamic as the user change the index in combobox. I have a slot connected to the currentIndexChanged(int) from the combobox. This slot should change the widget shown to the user according to the index of combobox.
    I tried the following code, but didn't work:
    @void Form::setMetodoWidget(int index)
    {
    Form2* form2;// a different widget to show
    if(index == 0 ){
    ui->widget = new QWidget;

    }
    if(index == 1){
        form2 = new Form2;
        ui->widget = form2;
    }
    

    }@

    I tried to add form2 in a vertical layout and then add to the widget, but then he gives this error message:
    Attempting to set QLayout "" on QWidget "widget", which already has a layout

    I would like to know how to do the "Promote to" function of QtDesigner,but in code.
    Thanks.

    thiberi0

    1 Reply Last reply
    0
    • VinorcolaV Offline
      VinorcolaV Offline
      Vinorcola
      wrote on last edited by
      #2

      You must pass throught the layout:
      http://qt-project.org/doc/qt-5/qlayout.html#removeWidget
      and then add the new widget. Be careful to memory leak ;)

      1 Reply Last reply
      0
      • VinorcolaV Offline
        VinorcolaV Offline
        Vinorcola
        wrote on last edited by
        #3

        Hop, even better way:
        http://qt-project.org/doc/qt-5/qstackedlayout.html

        1 Reply Last reply
        0
        • A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          I would use a QStackedWidget. Then add all of the widgets you would like to display to it. Then you can like it to the combo box on change notification and change to the proper widget.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thiberi0
            wrote on last edited by
            #5

            Thanks, I think QStackedLayout is exactly what a I need. Going to try later and if works I will post the code.

            thiberi0

            1 Reply Last reply
            0
            • VinorcolaV Offline
              VinorcolaV Offline
              Vinorcola
              wrote on last edited by
              #6

              Well, there already is a code example in the documentation that uses QComboBox ;) Look at the link I gave you in my second post.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                thiberi0
                wrote on last edited by
                #7

                Yes, and I just found another example at chapter 6 of Qt c++ GUI programming book.The problem it's solved already.Thanks guys.

                thiberi0

                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