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. Problem:StackedWidget Page switching
Forum Updated to NodeBB v4.3 + New Features

Problem:StackedWidget Page switching

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.4k Views 2 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.
  • Q Offline
    Q Offline
    Qt_HuangZiHan
    wrote on last edited by Qt_HuangZiHan
    #1

    I use Stackedwidget. There are some buttons to switch pages under the main interface. You can switch pages by clicking the button. But other pages are embedded in the middle of the interface by other windows. How do you do this?
    0_1558432103920_捕获.PNG

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QTime>
    #include <QTimer>
    #include "SecurityMonitoring.h"
    #include "SerialPort_ControlLED.h"
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
        enum enum_widget
            {
                E_SerialPort_ControlLED=0,
                E_SecurityMonitoring,
                E_RealTime_Clock= 2,
            };
    
    public:
        explicit Widget(QWidget *parent = nullptr);
        ~Widget();
    
    private slots:
        void on_ToolButton_RealTime_Clock_clicked();
    
        void on_ToolButton_SerialPort_ControlLED_clicked();
    
        void on_ToolButton_SecurityMonitoring_clicked();
    
        void slotShowCurrentDataTime();
    
        void initDataTime();
    
        void initWidget();
    
    private:
        Ui::Widget *ui;
        QTimer *m_timer;
    
        SecurityMonitoring *new_SecurityMonitoring;
        SerialPort_ControlLED *new_SerialPort_ControlLED;
    
        void initForm();
    };
    
    #endif // WIDGET_H
    
    

    widget.cpp

    #include "widget.h"
    #include "ui_widget.h"
    #include <QTime>
    #include <QTimer>
    #include <QDebug>
    #include "SecurityMonitoring.h"
    #include "SerialPort_ControlLED.h"
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
        this->initForm();
        this->initDataTime();
        this->slotShowCurrentDataTime();
        this->initWidget();
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::initForm()
    {
        //设置窗体标题栏隐藏
        this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint |
                             Qt::WindowMinMaxButtonsHint);
        this->setWindowTitle(tr("子涵智家"));
        this->setWindowIcon(QIcon(":/image/home.ico"));
    
        ui->widget_data->setObjectName("widget_data");
        ui->widget_data->setStyleSheet("#widget_data{border:2px groove gray;border-radius:12px;padding:2px 4px;}");
    
        ui->ToolButton_RealTime_Clock->setAutoRaise(true);
        ui->ToolButton_RealTime_Clock->setIcon(QPixmap(":/image/RealTime_Clock.jpg"));
        ui->ToolButton_RealTime_Clock->setIconSize(QPixmap(":/image/RealTime_Clock.jpg").size());
        ui->ToolButton_RealTime_Clock->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->ToolButton_SerialPort_ControlLED->setAutoRaise(true);
        ui->ToolButton_SerialPort_ControlLED->setIcon(QPixmap(":/image/LightingControl.jpg"));
        ui->ToolButton_SerialPort_ControlLED->setIconSize(QPixmap(":/image/LightingControl.jpg").size());
        ui->ToolButton_SerialPort_ControlLED->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->ToolButton_SecurityMonitoring->setAutoRaise(true);
        ui->ToolButton_SecurityMonitoring->setIcon(QPixmap(":/image/SecurityMonitoring.jpg"));
        ui->ToolButton_SecurityMonitoring->setIconSize(QPixmap(":/image/SecurityMonitoring.jpg").size());
        ui->ToolButton_SecurityMonitoring->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->stackedWidget->setCurrentIndex(E_RealTime_Clock);
        qDebug()<<"E_HOME_WIDGET is = "<<E_RealTime_Clock;
    
    }
    
    void Widget::initDataTime()
    {
        m_timer = new QTimer(this);
        connect(m_timer,SIGNAL(timeout()),this,SLOT(slotShowCurrentDataTime()));
        m_timer->start(1000);//定时1s钟。
    }
    
    //显示当期的日期和时间
    void Widget::slotShowCurrentDataTime()
    {
        ui->label_hour->setText(QTime::currentTime().toString("hh"));
        ui->label_min->setText(QTime::currentTime().toString("mm"));
        ui->label_sec->setText(QTime::currentTime().toString("ss"));
        ui->label_data->setText(QDate::currentDate().toString("yyyy年MM月dd日"));
        ui->label_week->setText(QDate::currentDate().toString("dddd"));
        ui->label_hour->setStyleSheet("color:#f0c051");
        ui->label_min->setStyleSheet("color:#f0c051");
        ui->label_sec->setStyleSheet("color:#f0c051");
        ui->label_week->setStyleSheet("color:#f0c051");
        ui->label_data->setStyleSheet("color:#f0c051");
    }
    
    void Widget::initWidget()
    {
        new_SecurityMonitoring = new SecurityMonitoring;
        new_SerialPort_ControlLED=new SerialPort_ControlLED;
        ui->stackedWidget->addWidget(new_SecurityMonitoring);
        ui->stackedWidget->addWidget(new_SerialPort_ControlLED);
    }
    
    
    void Widget::on_ToolButton_RealTime_Clock_clicked()
    {
        ui->stackedWidget->setCurrentIndex(E_RealTime_Clock);
        qDebug()<<"当前索引页 实时时钟:="<<ui->stackedWidget->currentIndex();
    }
    
    void Widget::on_ToolButton_SerialPort_ControlLED_clicked()
    {
        ui->stackedWidget->setCurrentIndex(E_SerialPort_ControlLED);
        qDebug()<<"当前索引页 串口控制:="<<ui->stackedWidget->currentIndex();
    }
    
    void Widget::on_ToolButton_SecurityMonitoring_clicked()
    {
        ui->stackedWidget->setCurrentIndex(E_SecurityMonitoring);
        qDebug()<<"当前索引页 安防监控:="<<ui->stackedWidget->currentIndex();
    }
    
    
    jsulmJ 1 Reply Last reply
    0
    • Q Qt_HuangZiHan

      I use Stackedwidget. There are some buttons to switch pages under the main interface. You can switch pages by clicking the button. But other pages are embedded in the middle of the interface by other windows. How do you do this?
      0_1558432103920_捕获.PNG

      widget.h

      #ifndef WIDGET_H
      #define WIDGET_H
      
      #include <QWidget>
      #include <QTime>
      #include <QTimer>
      #include "SecurityMonitoring.h"
      #include "SerialPort_ControlLED.h"
      
      namespace Ui {
      class Widget;
      }
      
      class Widget : public QWidget
      {
          Q_OBJECT
      
          enum enum_widget
              {
                  E_SerialPort_ControlLED=0,
                  E_SecurityMonitoring,
                  E_RealTime_Clock= 2,
              };
      
      public:
          explicit Widget(QWidget *parent = nullptr);
          ~Widget();
      
      private slots:
          void on_ToolButton_RealTime_Clock_clicked();
      
          void on_ToolButton_SerialPort_ControlLED_clicked();
      
          void on_ToolButton_SecurityMonitoring_clicked();
      
          void slotShowCurrentDataTime();
      
          void initDataTime();
      
          void initWidget();
      
      private:
          Ui::Widget *ui;
          QTimer *m_timer;
      
          SecurityMonitoring *new_SecurityMonitoring;
          SerialPort_ControlLED *new_SerialPort_ControlLED;
      
          void initForm();
      };
      
      #endif // WIDGET_H
      
      

      widget.cpp

      #include "widget.h"
      #include "ui_widget.h"
      #include <QTime>
      #include <QTimer>
      #include <QDebug>
      #include "SecurityMonitoring.h"
      #include "SerialPort_ControlLED.h"
      
      Widget::Widget(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::Widget)
      {
          ui->setupUi(this);
          this->initForm();
          this->initDataTime();
          this->slotShowCurrentDataTime();
          this->initWidget();
      }
      
      Widget::~Widget()
      {
          delete ui;
      }
      
      void Widget::initForm()
      {
          //设置窗体标题栏隐藏
          this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint |
                               Qt::WindowMinMaxButtonsHint);
          this->setWindowTitle(tr("子涵智家"));
          this->setWindowIcon(QIcon(":/image/home.ico"));
      
          ui->widget_data->setObjectName("widget_data");
          ui->widget_data->setStyleSheet("#widget_data{border:2px groove gray;border-radius:12px;padding:2px 4px;}");
      
          ui->ToolButton_RealTime_Clock->setAutoRaise(true);
          ui->ToolButton_RealTime_Clock->setIcon(QPixmap(":/image/RealTime_Clock.jpg"));
          ui->ToolButton_RealTime_Clock->setIconSize(QPixmap(":/image/RealTime_Clock.jpg").size());
          ui->ToolButton_RealTime_Clock->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
      
          ui->ToolButton_SerialPort_ControlLED->setAutoRaise(true);
          ui->ToolButton_SerialPort_ControlLED->setIcon(QPixmap(":/image/LightingControl.jpg"));
          ui->ToolButton_SerialPort_ControlLED->setIconSize(QPixmap(":/image/LightingControl.jpg").size());
          ui->ToolButton_SerialPort_ControlLED->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
      
          ui->ToolButton_SecurityMonitoring->setAutoRaise(true);
          ui->ToolButton_SecurityMonitoring->setIcon(QPixmap(":/image/SecurityMonitoring.jpg"));
          ui->ToolButton_SecurityMonitoring->setIconSize(QPixmap(":/image/SecurityMonitoring.jpg").size());
          ui->ToolButton_SecurityMonitoring->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
      
          ui->stackedWidget->setCurrentIndex(E_RealTime_Clock);
          qDebug()<<"E_HOME_WIDGET is = "<<E_RealTime_Clock;
      
      }
      
      void Widget::initDataTime()
      {
          m_timer = new QTimer(this);
          connect(m_timer,SIGNAL(timeout()),this,SLOT(slotShowCurrentDataTime()));
          m_timer->start(1000);//定时1s钟。
      }
      
      //显示当期的日期和时间
      void Widget::slotShowCurrentDataTime()
      {
          ui->label_hour->setText(QTime::currentTime().toString("hh"));
          ui->label_min->setText(QTime::currentTime().toString("mm"));
          ui->label_sec->setText(QTime::currentTime().toString("ss"));
          ui->label_data->setText(QDate::currentDate().toString("yyyy年MM月dd日"));
          ui->label_week->setText(QDate::currentDate().toString("dddd"));
          ui->label_hour->setStyleSheet("color:#f0c051");
          ui->label_min->setStyleSheet("color:#f0c051");
          ui->label_sec->setStyleSheet("color:#f0c051");
          ui->label_week->setStyleSheet("color:#f0c051");
          ui->label_data->setStyleSheet("color:#f0c051");
      }
      
      void Widget::initWidget()
      {
          new_SecurityMonitoring = new SecurityMonitoring;
          new_SerialPort_ControlLED=new SerialPort_ControlLED;
          ui->stackedWidget->addWidget(new_SecurityMonitoring);
          ui->stackedWidget->addWidget(new_SerialPort_ControlLED);
      }
      
      
      void Widget::on_ToolButton_RealTime_Clock_clicked()
      {
          ui->stackedWidget->setCurrentIndex(E_RealTime_Clock);
          qDebug()<<"当前索引页 实时时钟:="<<ui->stackedWidget->currentIndex();
      }
      
      void Widget::on_ToolButton_SerialPort_ControlLED_clicked()
      {
          ui->stackedWidget->setCurrentIndex(E_SerialPort_ControlLED);
          qDebug()<<"当前索引页 串口控制:="<<ui->stackedWidget->currentIndex();
      }
      
      void Widget::on_ToolButton_SecurityMonitoring_clicked()
      {
          ui->stackedWidget->setCurrentIndex(E_SecurityMonitoring);
          qDebug()<<"当前索引页 安防监控:="<<ui->stackedWidget->currentIndex();
      }
      
      
      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Qt_HuangZiHan said in Problem:StackedWidget Page switching:

      But other pages are embedded in the middle of the interface by other windows. How do you do this?

      By using layouts?
      But I'm not sure what you mean. Can you explain better what you want to do and what the problem is?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Qt_HuangZiHan said in Problem:StackedWidget Page switching:

        But other pages are embedded in the middle of the interface by other windows. How do you do this?

        By using layouts?
        But I'm not sure what you mean. Can you explain better what you want to do and what the problem is?

        Q Offline
        Q Offline
        Qt_HuangZiHan
        wrote on last edited by
        #3

        @jsulm In the middle of this page is a Stacked Widget. I created three index pages, but I also made three UI interfaces. Now I want to embed these three UI interfaces into the index page of Stacked Widget.

        jsulmJ 1 Reply Last reply
        0
        • Q Qt_HuangZiHan

          @jsulm In the middle of this page is a Stacked Widget. I created three index pages, but I also made three UI interfaces. Now I want to embed these three UI interfaces into the index page of Stacked Widget.

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @Qt_HuangZiHan There is an example right there in the documentation (https://doc.qt.io/qt-5/qstackedwidget.html):

          // Replace QWidget with your own widget
          QWidget *firstPageWidget = new QWidget;
          QWidget *secondPageWidget = new QWidget;
          QWidget *thirdPageWidget = new QWidget;
          
          QStackedWidget *stackedWidget = new QStackedWidget;
          stackedWidget->addWidget(firstPageWidget);
          stackedWidget->addWidget(secondPageWidget);
          stackedWidget->addWidget(thirdPageWidget);
          

          There is also https://doc.qt.io/qt-5/qstackedwidget.html#insertWidget
          You also can use https://doc.qt.io/qt-5/qstackedwidget.html#widget to get the widget on the given tab and use it as parent for you widget which you add on top of it...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Qt_HuangZiHan There is an example right there in the documentation (https://doc.qt.io/qt-5/qstackedwidget.html):

            // Replace QWidget with your own widget
            QWidget *firstPageWidget = new QWidget;
            QWidget *secondPageWidget = new QWidget;
            QWidget *thirdPageWidget = new QWidget;
            
            QStackedWidget *stackedWidget = new QStackedWidget;
            stackedWidget->addWidget(firstPageWidget);
            stackedWidget->addWidget(secondPageWidget);
            stackedWidget->addWidget(thirdPageWidget);
            

            There is also https://doc.qt.io/qt-5/qstackedwidget.html#insertWidget
            You also can use https://doc.qt.io/qt-5/qstackedwidget.html#widget to get the widget on the given tab and use it as parent for you widget which you add on top of it...

            Q Offline
            Q Offline
            Qt_HuangZiHan
            wrote on last edited by
            #5

            @jsulm Is that the case?

                SecurityMonitoring *new_SecurityMonitoring;
                SerialPort_ControlLED *new_SerialPort_ControlLED;
            
            void Widget::initWidget()
            {
                new_SecurityMonitoring = new SecurityMonitoring;
                new_SerialPort_ControlLED=new SerialPort_ControlLED;
                ui->stackedWidget->addWidget(new_SecurityMonitoring);
                ui->stackedWidget->addWidget(new_SerialPort_ControlLED);
            }
            
            jsulmJ 1 Reply Last reply
            0
            • Q Qt_HuangZiHan

              @jsulm Is that the case?

                  SecurityMonitoring *new_SecurityMonitoring;
                  SerialPort_ControlLED *new_SerialPort_ControlLED;
              
              void Widget::initWidget()
              {
                  new_SecurityMonitoring = new SecurityMonitoring;
                  new_SerialPort_ControlLED=new SerialPort_ControlLED;
                  ui->stackedWidget->addWidget(new_SecurityMonitoring);
                  ui->stackedWidget->addWidget(new_SerialPort_ControlLED);
              }
              
              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Qt_HuangZiHan said in Problem:StackedWidget Page switching:

              Is that the case?

              What is the case?

              Your code looks fine, Does it work?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                Hi
                I read your issues as you want to do
                WidgetPtr->ui->stackedWidget->setCurrentIndex()
                but UI is private ?
                so from main widget, you want to let buttons switch paged for
                a other widgets stackedWidget ? ( the class Widget : public QWidget )
                Is that correct ?

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Qt_HuangZiHan said in Problem:StackedWidget Page switching:

                  Is that the case?

                  What is the case?

                  Your code looks fine, Does it work?

                  Q Offline
                  Q Offline
                  Qt_HuangZiHan
                  wrote on last edited by
                  #8

                  @jsulm I tried my code, but it didn't work.

                  jsulmJ 1 Reply Last reply
                  0
                  • Q Qt_HuangZiHan

                    @jsulm I tried my code, but it didn't work.

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Qt_HuangZiHan If you say "didn't work" you should say what exactly did not work (or in what way it misbehaves).

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved