Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Stackwidget interface switching problem



  • Hello there,
    My program runs without problems, but when I click the tool button, some page numbers are incorrectly printed, some pages are displayed upside down, and some are not displayed.

    widget.cpp

    #include "widget.h"
    #include "ui_widget.h"
    #include <QTime>
    #include <Qtimer>
    #include <QFont>
    #include <QPropertyAnimation>
    #include <QDebug>
    
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
        this->InitializeForm();
        this->InitializeWidget();
        this->CounterTime();
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    //界面样式初始化
    void Widget::InitializeForm()
    {
        this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint |
                             Qt::WindowMinMaxButtonsHint);
        this->setWindowTitle(tr("子涵智家"));
        this->setWindowIcon(QIcon(":/images/icon/TitleBarIcon.ico"));
    
        ui->widget_time->setObjectName("widget_time");
        ui->widget_time->setStyleSheet("#widget_time{border:2px groove gray;border-radius:12px;padding:2px 4px;}");
    
        //设置首页顶部的样式
        ui->widget_setting->setText(tr("设置"));
        ui->widget_setting->setIcon(QPixmap(":/images/widget_top/setting_pressed.png"));
        ui->widget_setting->setIconSize(QPixmap(":/images/widget_top/setting_pressed.png").size());
        ui->widget_setting->setAutoRaise(true);//鼠标经过是自动凸起
        ui->widget_setting->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);//文本在图标的下面
    
        ui->widget_home->setText(tr("首页"));
        ui->widget_home->setIcon(QPixmap(":/images/widget_top/home_normal.png"));
        ui->widget_home->setIconSize(QPixmap(":/images/widget_top/home_normal.png").size());
        ui->widget_home->setAutoRaise(true);
        ui->widget_home->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->widget_title->setStyleSheet("border-image: url(:/images/widget_top/widget_title.jpg);");
    
        //设置首页中部的样式
    
    
    
        //设置首页底部的样式
        ui->toolButton_control->setText(tr("家居控制"));
        ui->toolButton_control->setIcon(QPixmap(":/images/widget_bottom/toolButton_control.jpg"));
         ui->toolButton_control->setIconSize(QPixmap(":/images/widget_bottom/toolButton_control.jpg").size());
        ui->toolButton_control->setAutoRaise(true);//鼠标经过是自动凸起
        ui->toolButton_control->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);//文本在图标的下面
    
        ui->toolButton_curtain->setText(tr("窗帘控制"));
        ui->toolButton_curtain->setIcon(QPixmap(":/images/widget_bottom/toolButton_curtain.jpg"));
        ui->toolButton_curtain->setIconSize(QPixmap(":/images/widget_bottom/toolButton_curtain.jpg").size());
        ui->toolButton_curtain->setAutoRaise(true);
        ui->toolButton_curtain->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->toolButton_module->setText(tr("模式选择"));
        ui->toolButton_module->setIcon(QPixmap(":/images/widget_bottom/toolButton_module.jpg"));
         ui->toolButton_module->setIconSize(QPixmap(":/images/widget_bottom/toolButton_module.jpg").size());
        ui->toolButton_module->setAutoRaise(true);//鼠标经过是自动凸起
        ui->toolButton_module->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);//文本在图标的下面
    
        ui->toolButton_music->setText(tr("音乐播放"));
        ui->toolButton_music->setIcon(QPixmap(":/images/widget_bottom/toolButton_music.png"));
        ui->toolButton_music->setIconSize(QPixmap(":/images/widget_bottom/toolButton_music.png").size());
        ui->toolButton_music->setAutoRaise(true);
        ui->toolButton_music->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->toolButton_statistics->setText(tr("数据统计"));
        ui->toolButton_statistics->setIcon(QPixmap(":/images/widget_bottom/toolButton_statistics.jpg"));
        ui->toolButton_statistics->setIconSize(QPixmap(":/images/widget_bottom/toolButton_statistics.jpg").size());
        ui->toolButton_statistics->setAutoRaise(true);
        ui->toolButton_statistics->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        //设置首页中部选择房间的工具按钮的样式
        ui->widget_bedroom->setText(tr("卧室"));
        ui->widget_bedroom->setIcon(QPixmap(":/images/room_selection/widget_bedroom.jpg"));
        ui->widget_bedroom->setIconSize(QPixmap(":/images/room_selection/widget_bedroom.jpg").size());
        ui->widget_bedroom->setAutoRaise(true);
        ui->widget_bedroom->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->widget_kitchen->setText(tr("厨房"));
        ui->widget_kitchen->setIcon(QPixmap(":/images/room_selection/widget_kitchen.jpg"));
        ui->widget_kitchen->setIconSize(QPixmap(":/images/room_selection/widget_kitchen.jpg").size());
        ui->widget_kitchen->setAutoRaise(true);
        ui->widget_kitchen->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->widget_parlor->setText(tr("客厅"));
        ui->widget_parlor->setIcon(QPixmap(":/images/room_selection/widget_parlor.jpg"));
        ui->widget_parlor->setIconSize(QPixmap(":/images/room_selection/widget_parlor.jpg").size());
        ui->widget_parlor->setAutoRaise(true);
        ui->widget_parlor->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->widget_safety->setText(tr("安防"));
        ui->widget_safety->setIcon(QPixmap(":/images/room_selection/widget_safety.jpg"));
        ui->widget_safety->setIconSize(QPixmap(":/images/room_selection/widget_safety.jpg").size());
        ui->widget_safety->setAutoRaise(true);
        ui->widget_safety->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    
        ui->widget_middle->setCurrentIndex(enum_widget_home);
        //qDebug()<<"E_HOME_WIDGET is = "<<enum_homepage;
        //要按下家居控制按钮后才进入中间部分样式。
    }
    
    void Widget::InitializeWidget()
    {
        new_settingDialog = new settingDialog;
        //m_statisticWidget = new StatisticWidget();
        new_bedRoomWidget = new bedroom();
        new_curtainWidget = new curtain_control();
        new_PlayMusicWidget = new play_music();
        //m_modelWidget = new ModelChooseWidget();
        new_parlourWidget = new Parlour();
        //m_kitchenWidget = new KitchenWidget();
        //ui->stackedWidget->addWidget(m_bedRoomWidget);
        ui->widget_middle->addWidget(new_curtainWidget);
        ui->widget_middle->addWidget(new_PlayMusicWidget);
        //ui->stackedWidget->addWidget(m_modelWidget);
        ui->widget_middle->addWidget(new_parlourWidget);
        //ui->stackedWidget->addWidget(m_kitchenWidget);
        //ui->stackedWidget->addWidget(m_statisticWidget);
    }
    
    void Widget::CounterTime()
    {
        new_timer = new QTimer(this);
        connect(new_timer,SIGNAL(timeout()),this,SLOT(ShowCurrentDataTime_SlotsAndStyle()));
        new_timer->start(1000);//定时1s钟。
    }
    
    /*void Widget::startAnimation()
    {
        QPropertyAnimation *animation = new QPropertyAnimation(ui->widget_middle->currentWidget(), "geometry");
        animation->setDuration(800);
        animation->setStartValue(QRect(0, 0,100, 30));
        animation->setEndValue(QRect(0, 0, 800, 360));
        animation->start();
    }*/
    
    void Widget::ShowCurrentDataTime_SlotsAndStyle()
    {
        ui->widget_hour->setText(QTime::currentTime().toString("hh"));
        ui->widget_min->setText(QTime::currentTime().toString("mm"));
        ui->widget_sec->setText(QTime::currentTime().toString("ss"));
        ui->widget_data->setText(QDate::currentDate().toString("yyyy年MM月dd日"));
        ui->widget_week->setText(QDate::currentDate().toString("dddd"));
        ui->widget_colon->setText(":");
        ui->widget_hour->setStyleSheet("color:#f0c051");
        ui->widget_sec->setStyleSheet("color:#f0c051");
        ui->widget_min->setStyleSheet("color:#f0c051");
        ui->widget_week->setStyleSheet("color:#f0c051");
        ui->widget_data->setStyleSheet("color:#f0c051");
        ui->widget_colon->setStyleSheet("color:#f0c051");
    
        QFont font("SimHei", 50, 50, false);
        ui->widget_colon->setFont(font);
    
    }
    
    void Widget::setCurrentWidget(enum_widget enum_widget_name)
    {
        //startAnimation();
        ui->widget_middle->setCurrentIndex(enum_widget_name);
    }
    
    void Widget::on_widget_setting_clicked()
    {
        new_settingDialog->show();
    }
    
    void Widget::on_widget_home_clicked()
    {
         this->setCurrentWidget(enum_widget_home);
         qDebug()<<"当前索引页 主页:="<<ui->widget_middle->currentIndex();
    }
    
    void Widget::on_widget_bedroom_clicked()
    {
          this->setCurrentWidget(enum_bedroom);
          qDebug()<<"当前索引页 卧室:="<<ui->widget_middle->currentIndex();
    }
    
    void Widget::on_widget_kitchen_clicked()
    {
    
    }
    
    void Widget::on_widget_safety_clicked()
    {
    
    }
    
    void Widget::on_widget_parlor_clicked()
    {
         this->setCurrentWidget(enum_parlour);
         qDebug()<<"当前索引页 客厅:="<<ui->widget_middle->currentIndex();
    }
    
    void Widget::on_toolButton_control_clicked()
    {
         this->setCurrentWidget(enum_toolButton_control);
         qDebug()<<"当前索引页 家居控制:="<<ui->widget_middle->currentIndex();
    }
    
    void Widget::on_toolButton_music_clicked()
    {
         ui->widget_middle->setCurrentIndex(enum_paly_music);
         qDebug()<<"当前索引页 音乐播放:="<<ui->widget_middle->currentIndex();
    }
    
    void Widget::on_toolButton_statistics_clicked()
    {
    
    }
    
    void Widget::on_toolButton_curtain_clicked()
    {
         ui->widget_middle->setCurrentIndex(enum_curtain_control);
         qDebug()<<"当前索引页 窗帘控制:="<<ui->widget_middle->currentIndex();
    }
    
    void Widget::on_toolButton_module_clicked()
    {
    
    }
    
    

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QTime>
    #include <QTimer>
    #include "curtain_control.h"
    #include "settingdialog.h"
    #include "play_music.h"
    #include "parlour.h"
    #include "bedroom.h"
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
        enum enum_widget
        {
            enum_toolButton_control = 0,    //控制主页
            enum_widget_home=1,             //首页
            enum_bedroom=2,                 //卧室
            enum_curtain_control=3,         //窗帘界面
            enum_paly_music=4,              //音乐播放界面
            //E_MODEL_WIDGET=5,             //模式控制
            enum_parlour=6,       //客厅
            //E_KITCHEN_WIDGET = 7,         //厨房
            //E_TEMPTURE = 8,               //统计
            //E_NIGHT_WIDGET,               //夜间模式
            //E_SECURITY_WIDGET,            //安防管理界面
        };
    
    public:
        explicit Widget(QWidget *parent = 0);
        ~Widget();
    
    private:
        Ui::Widget *ui;
    
        QTimer *new_timer;
    
        void  InitializeForm();
        void  CounterTime();
        void  InitializeWidget();
        void  setCurrentWidget(enum_widget enum_widget_name);
        //void  startAnimation();
    
    
    
        settingDialog *new_settingDialog;           //设置界面
        Parlour *new_parlourWidget;                 //客厅界面
        bedroom *new_bedRoomWidget;                 //卧室界面
        //KitchenWidget *m_kitchenWidget;                 //厨房界面
        //StatisticWidget *m_statisticWidget;             //统计界面
        curtain_control *new_curtainWidget;                 //窗帘界面
        //ModelChooseWidget *m_modelWidget;               //模式选择控制界面
         play_music *new_PlayMusicWidget;                 //音乐播放界面
    
    private slots:
        void ShowCurrentDataTime_SlotsAndStyle();
        void on_widget_setting_clicked();
        void on_widget_home_clicked();
        void on_widget_bedroom_clicked();
        void on_widget_kitchen_clicked();
        void on_widget_safety_clicked();
        void on_widget_parlor_clicked();
        void on_toolButton_control_clicked();
        void on_toolButton_music_clicked();
        void on_toolButton_statistics_clicked();
        void on_toolButton_curtain_clicked();
        void on_toolButton_module_clicked();
    };
    
    #endif // WIDGET_H
    
    

    Unable to display page
    0_1556165636713_问题1.PNG
    Interchanging pages
    1_1556165712355_问题4.PNG 0_1556165712354_问题3.PNG
    The index number is set to 6 but the printout is 0.
    0_1556165796756_问题2.PNG


  • Qt Champions 2019

    @Qt_HuangZiHan said in Stackwidget interface switching problem:

    enum_paly_music=4, //音乐播放界面
    //E_MODEL_WIDGET=5, //模式控制
    enum_parlour=6,

    Are you sure you actually have widget #6? You commented out enum value #5.
    You can check how many widgets you actually have printing https://doc.qt.io/qt-5/qstackedwidget.html#count-prop



  • @jsulm I am annotating part 5, part 6 is not commented out, but part 6 prints the index number of part 0

       enum_toolButton_control = 0,
    //E_MODEL_WIDGET=5,             //模式控制
       enum_parlour=6, 
    

    The index number shown here should be 6, but it prints 0.
    0_1556169171316_439d1dce-2240-400f-90a3-c3e7269453ee-图片.png


  • Qt Champions 2019

    @Qt_HuangZiHan I didn't say you commented out 6, I said you commented out 5.
    Did you try to print out https://doc.qt.io/qt-5/qstackedwidget.html#count-prop as I suggested?



  • @jsulm This is the result of the display, showing that the current number of parts is 5, but I enumerated six in the header file.

    当前索引页 家居控制:= 0
    当前部件数量= 5
    当前索引页 卧室:= 2
    当前部件数量= 5
    当前索引页 家居控制:= 0
    当前部件数量= 5
    当前索引页 客厅:= 0
    当前部件数量= 5
    当前索引页 音乐播放:= 4
    当前部件数量= 5
    当前索引页 窗帘控制:= 3
    当前部件数量= 5
    

  • Qt Champions 2019

    @Qt_HuangZiHan If you have 5 widgets then you can't set current index to 6. If you have 5 widgets then the highest valid index is 4.



  • @jsulm said in Stackwidget interface switching problem:

    If you have 5 widgets then you can't set current index to 6. If you have 5 widgets then the highest valid index is 4.

    The problem of page switching has been successfully solved. What you said makes sense. What I also found is that the order of enumeration should be the same as the order of the slot functions in the source file.


Log in to reply