qt layout Sequencer



  • Need to display multiple layout control by QTimer.
    Currently using layout (hide/show) to achieve this.
    Is there an easier way to achieve this?

    Thanks for all the help given.



  • @houmingc Hi, there is a QStackedLayout that you can assign multiple widgets that in turn contain different layouts. With setCurrentIndex(int) you can set the visible widget.



  • Understand QStackLayout class provides a stack of widgets where only one widget is visible at a time.
    How does QStackedLayout add different layout? And how does QListWidgetItem comes in?

           QStackedLayout* stackLayout = new QStackedLayout;
           stackLayout->addLayout(mainLayout);
           stackLayout->addlayout(layout2);
           stackLayout->addlayout(layout3);
           stackLayout->addlayout(layout4);
           stackLayout->addlayout(layout5);
    

    is there a qt class that provides a stack of layout where one layout is visable at any one time



  • @houmingc said:

    How does QStackedLayout add different layout? And how does QListWidgetItem comes in?

    The method QStackedLayout::addWidget takes a QWidget that in turn can contain other widgets being organized by a QLayout.

    What do you mean by "how does QListWidgetItem comes in"? Do you want to change the currently displayed Widget on the StackedLayout by changing the index of a list? There is setCurrentIndex() to achieve this.

    According to http://doc.qt.io/qt-5/qstackedlayout-members.html there is no method addLayout() for QStackedLayout.



  • Below is my code, i am not able to show secondPageWidget successfully.

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    firstPageWidget = new QWidget();
    secondPageWidget = new QWidget();

         firstPageWidget->setWindowTitle("Remember to save");
         firstPageWidget->setStyleSheet("background-color:black");
    
        stackedLayout = new QStackedLayout;
        stackedLayout->addWidget(firstPageWidget);
        stackedLayout->addWidget(secondPageWidget);
    //----------------------------------------------
        pageComboBox = new QComboBox;
        pageComboBox->addItem(tr("Page 1"));
        pageComboBox->addItem(tr("Page 2"));
        QObject::connect(pageComboBox,SIGNAL(activated(int)),stackedLayout, SLOT(setCurrentIndex(int)));
    //----------------------------------------------
       ml = new WidgetMarqueeLabel();
      //ml->setTextFormat(Qt::RichText);
       ml->setDirection(1);
       ml->setSpeed(1);
      ml->setText(QString("%1").arg(xmlValue[2]));
      ml->setFont(QFont("Arial", 15,30));
     //----------------------------------------------
         video();
    
    // 1st component
         labelOneGif = new QLabel;
        QMovie *movie_one = new QMovie(":/name/animal.gif");
        labelOneGif->setAlignment(Qt::AlignCenter);
        labelOneGif->setMovie(movie_one);
        movie_one->start();
    // 3rd component
        labelTwoGif = new QLabel;
        QMovie *movie_two = new QMovie(":/name/bee.gif");
        labelTwoGif->setMovie(movie_two);
        labelTwoGif->setAlignment(Qt::AlignCenter);
        labelTwoGif->setGeometry(10,10,10,10);
        movie_two->start();
    
        QFont f("Arial",19, QFont::Bold);
        label_clock = new QLabel;
        label_clock->setFont(f);
        label_clock->setStyleSheet("QLabel { color : red; }");
    
       // 2rd component
        label_destination = new QLabel(tr("   11 Woodland North"));
        label_destination->setFont(f);
        label_destination->setAlignment(Qt::AlignCenter);
        label_destination->setStyleSheet("QLabel { color : white; }");
    
        // 4th component
        label_source = new QLabel(tr(" 13 Stevens"));
        label_source->setFont(f);
       label_source->setAlignment(Qt::AlignCenter);
        label_source->setStyleSheet("QLabel { color : white; }");
    
       leftLayout = new QVBoxLayout();
       leftLayout->addWidget(pageComboBox);
        leftLayout->addWidget(labelOneGif);
        leftLayout->addSpacing(3);
       leftLayout->addStretch(0.1);
       leftLayout->addWidget(label_destination);
       leftLayout->addSpacing(3);
        leftLayout->addStretch(0.1);
        leftLayout->addWidget(labelTwoGif);
        leftLayout->addSpacing(3);
        leftLayout->addStretch(0.1);
       leftLayout->addWidget(label_source);
       rightLayout = new QVBoxLayout();
        rightLayout->addWidget(videoWidget);
    
    
        topLayout = new QHBoxLayout();
        topLayout->addLayout(leftLayout);
        topLayout->addLayout(rightLayout);
    
       Layout_page1= new QVBoxLayout();
        Layout_page1->addLayout(topLayout);
        Layout_page1->addWidget(ml);
    
        page2_label1 = new QLabel("NEXT TRAIN");
        page2_label1->setAlignment(Qt::AlignCenter);
        page2_label1->setStyleSheet("QLabel { color : white; }");
        page2_label2 = new QLabel("13 Woodlands North");
       page2_label2->setStyleSheet("QLabel { color : white; }");
        page2_label3 = new QLabel("2ND TRAIN ARRIVAL");
        page2_label3->setStyleSheet("QLabel { color : white; }");
    
       secondPageWidget->showMaximized();
       Layout_page2= new QVBoxLayout();
       Layout_page2->addWidget(page2_label1);
       Layout_page2->addWidget(page2_label2);
       Layout_page2->addWidget(page2_label3);
    
       firstPageWidget->setLayout(Layout_page1);
       firstPageWidget->showMaximized();
    
      // secondPageWidget->setLayout(Layout_page2);
       secondPageWidget->setStyleSheet("background-color:black");
    

    }



  • @houmingc Your code block is a little bit long and since it is referencing a lot of objects not declared I created a small self-contained example.

    But I think the problem is with ComboBox::activated(int) vs. setCurrentIndex(int). From the documentation at http://doc.qt.io/qt-5/qcombobox.html#activated:

    void QComboBox::activated(int index)

    This signal is sent when the user chooses an item in the combobox. The item's index is passed. Note that this signal is sent even when the choice is not changed. If you need to know when the choice actually changes, use signal currentIndexChanged().

    Header File

    #ifndef __mainwindow_h
    #define __mainwindow_h
    
    #include <QApplication>
    #include <QMainWindow>
    #include <QComboBox>
    #include <QVBoxLayout>
    #include <QStackedLayout>
    #include <QLabel>
    
    class MainWindow : public QMainWindow {
      Q_OBJECT
    
      public:
        MainWindow();
    
      private:
        QLabel *firstPageWidget;
        QLabel *secondPageWidget;
        QStackedLayout *stackedLayout;
        QComboBox *pageSelector;
    };
    
    #endif
    

    Implementation

    #include "mainwindow.h"
    
    int main( int argc, char *argv[] ) {
      QApplication app( argc, argv );
    
      MainWindow w;
      w.resize(800,600);
      w.show();
      return app.exec();
    }
    
    MainWindow::MainWindow() {
    
      // Create a ComboBox as the page selector
      pageSelector = new QComboBox;
      pageSelector->addItem("Page 1");
      pageSelector->addItem("Page 2");
    
      // Create the Page Widgets
      // for the sake of simplicity these are just two QLabels
      firstPageWidget = new QLabel("First Page");
      secondPageWidget = new QLabel("Second Page");
    
      // Create the Widget with the stacked Layout
      // and add the two pages
      QWidget *widgetStack = new QWidget;
      stackedLayout = new QStackedLayout;
      stackedLayout->addWidget(firstPageWidget);
      stackedLayout->addWidget(secondPageWidget);
      widgetStack->setLayout(stackedLayout);
    
      // Layout the ComboBox and the Widget
      // containing the stacked Layout
      QVBoxLayout *vbox = new QVBoxLayout;
      vbox->addWidget(pageSelector);
      vbox->addWidget(widgetStack);
    
      QWidget *widgetCentral = new QWidget;
      widgetCentral->setLayout(vbox);
      setCentralWidget(widgetCentral);
    
      // connect signals
      connect(
          pageSelector,
          SIGNAL(currentIndexChanged(int)),
          stackedLayout,
          SLOT(setCurrentIndex(int))
          );
    
    }
    


  • simow, thanks

    I have no problem adding QLabel into StackLayout.
    Problem is adding QLayout(my composite layout-project requirement) into StackLayout

    1. StackLayout->addWidget(new QLabel)
    2. StackLayout->addWidget(new QLayout) // no possible

    Not possible thus use a adapter, add QWidget into StackLayout but it is still no working.Please help :<<

    QWidget* firstPageWidget->setLayout(LayoutComposite1);
    QWidget* secondPageWidget->setLayout(LayoutComposite2);

    StackLayout->addWidget(firstPageWidget);
    StackLayout->addWidget(secondPageWidget);



  • @houmingc, you have to create a QWidget first, assign the layout you want to it and then add it – the QWidget – to the QStackedLayout.

    QWidget *w = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout;
    w->setLayout(layout);
    layout->addWidget( ... );
    
    stackedLayout->addWidget(w);
    


  • it work when layout is wrapped with a QWidget
    w_page1 = new QWidget;
    w_page2= new QWidget;
    w_page1->setLayout(Layout_page1);
    w_page2->setLayout(Layout_page2);

    stackedLayout = new QStackedLayout;
    stackedLayout->addWidget(w_page1);
    stackedLayout->addWidget(w_page2);
    

    How does a timer sequence pages in stacklayout
    connect(timer1,SIGNAL(timeout()),stackedLayout,SLOT(setCurrentIndex(int)));



  • @houmingc I would create a new slot e.g. nextSlide() that gets called on each timer trigger.
    There you increment the index like stackedLayout->setCurrentIndex(stackedLayout->currentIndex()+1).
    With count() you get the number of widgets contained in the layout so you can wrap to 0 if you reach the maximum widget index.



  • Success in using class QStackedLayout to do paging but because i am running on MainWindow.
    I am getting "QLayout:: Attempting to add QLayout "" to MainWindow "Mainwindow, which already has a layout, When i

    ''' QVBoxLayout* Layout_page1 = new QVBoxLayout();
    Layout_page1 -> addWidget(label);
    Should i attempt to use class QStackedWidget? is it more correct ?


  • Lifetime Qt Champion

    @houmingc , it's pretty much the same question I've answered here



  • ok. thanks. :>>
    Have a different scenario. Instead of paging whole layoutPage.
    I need to paging on rowTwoLayout only.
    rowOneLayout and rowThreeWidget is fixed.
    '''
    layoutPage1 = new QVBoxLayout;
    layoutPage1->addLayout(rowOneLayout);
    layoutPage1->addLayout(rowTwoLayout);
    layoutPage1->addWidget(rowThreeWidget);


Log in to reply
 

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