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. qt layout Sequencer
Forum Updated to NodeBB v4.3 + New Features

qt layout Sequencer

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 3.8k Views 3 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.
  • H houmingc

    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

    simowS Offline
    simowS Offline
    simow
    wrote on last edited by simow
    #4

    @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.

    Let's Try To Negotiate!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      houmingc
      wrote on last edited by houmingc
      #5

      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");
      

      }

      simowS 1 Reply Last reply
      0
      • H houmingc

        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");
        

        }

        simowS Offline
        simowS Offline
        simow
        wrote on last edited by
        #6

        @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))
              );
        
        }
        

        Let's Try To Negotiate!

        1 Reply Last reply
        0
        • H Offline
          H Offline
          houmingc
          wrote on last edited by
          #7

          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);

          simowS 1 Reply Last reply
          0
          • H houmingc

            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);

            simowS Offline
            simowS Offline
            simow
            wrote on last edited by
            #8

            @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);
            

            Let's Try To Negotiate!

            1 Reply Last reply
            0
            • H Offline
              H Offline
              houmingc
              wrote on last edited by houmingc
              #9

              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)));

              simowS 1 Reply Last reply
              0
              • H houmingc

                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)));

                simowS Offline
                simowS Offline
                simow
                wrote on last edited by
                #10

                @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.

                Let's Try To Negotiate!

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  houmingc
                  wrote on last edited by houmingc
                  #11

                  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 ?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      houmingc
                      wrote on last edited by
                      #13

                      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);

                      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