⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

make visible 1 layout at a time

  • hello qt community,
    i'm trying to make visible one layout at a time, i want something like the program bellow but with layouts

    QStackedLayout* stackedwidget= new QStackedLayout();
     stackedwidget->addwidget(widget1);// the stackedlayout dispalayed ;
        stackedwidget->addwidget(widget2);// the stackedlayout;

    i used the additem and setcurrentindex but i have a message said that QStackedLayout::addItem: Only widgets can be added

    so i'm wondering if someone had this problem before, i need help please

    thanks again ..

  • Lifetime Qt Champion

    And you are 10.000% sure that it was indeed a
    QWidget you tried to insert?

    in any case, the sample from the docs works without any warnings
    and i could not make it say
    "QStackedLayout::addItem: Only widgets can be added"

            QWidget *firstPageWidget = new QWidget;
            QWidget *secondPageWidget = new QWidget;
            QWidget *thirdPageWidget = new QWidget;
            QStackedLayout *stackedLayout = new QStackedLayout;
            QVBoxLayout *mainLayout = new QVBoxLayout;

    So if you can make it say so, please show the actual code with all definitions.
    and also please mention Qt version , you are using.

    Also, just as a note. There is also QStacketWidget that often is easier to use than
    the raw QStackedLayout.

  • ow sorry i'm using Qt5.

    i think you didn't read the question well or that i wasn't clear enough :p , i'm trying to have one layout at a time and not one widget at a time it's mean that instead of inserting a widget in a stackedwidget i want to insert layouts. and use one layout at a time.

    i know that their is a qstacked widget but i thought that qstacked layout would allow me to stack layouts which is not the case :).

    thanks again

  • Lifetime Qt Champion

    i think its me that didnt read it well :)
    On second read, i see you want layouts.

    It won't accept layouts. (AFAIK)
    However, just use a widget, put the wanted layout on that and use it that way.
    The Widget is transparent so should not matter.

    Ah. yes. stacked layout kinda sounds like layouts in a stack indeed :)

  • i didn't understand what your trying to say so you suggest to put a layout on widget ?

    it won't even compile we can't convert a lyout to a widget, perhaps a widget can be converted to a layout but not the reverse.

  • Moderators

    @benamiar the idea is to create a new QWidget, assign it your Layout, and than add it to the QStacledWidget

  • @j-hilk assign a layout to a widget ? how is that even possible? you can add a widget to a layout but a layout to a widget? i'm lost ?

  • Lifetime Qt Champion

    @benamiar ?
    A layout isn't just somewhere in the air it is on a widget.

  • Moderators

    @benamiar a fanction of every QWidget based widget


    edit: ninja sniped by @jsulm 😆

  • @j-hilk thanks guys it's working

Log in to reply