Problem:StackedWidget Page switching



  • 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();
    }
    
    

  • Qt Champions 2018

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



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


  • Qt Champions 2018

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



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

  • Qt Champions 2018

    @Qt_HuangZiHan said in Problem:StackedWidget Page switching:

    Is that the case?

    What is the case?

    Your code looks fine, Does it work?


  • Lifetime Qt Champion

    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 ?



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


  • Qt Champions 2018

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


Log in to reply