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

My clock won't move.



  • HomePage.h

    #ifndef HOMEPAGE_H
    #define HOMEPAGE_H
    
    #include <QWidget>
    #include <QTime>
    #include <QTimer>
    
    namespace Ui {
    class HomePage;
    }
    
    class HomePage : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit HomePage(QWidget *parent = 0);
        ~HomePage();
    
    private:
        Ui::HomePage *ui;
    
        //声明私有的指针变量
        QTimer *new_timer;
    
        //声明私有的函数
        void InitializeForm();
        void InitializeForm_Time();
        void ShowCurrentTime();
        void Counter();
    };
    
    #endif // HOMEPAGE_H
    
    

    HomePage.cpp

    #include "HomePage.h"
    #include "ui_HomePage.h"
    #include <QTime>
    #include <QTimer>
    
    HomePage::HomePage(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::HomePage)
    {
        ui->setupUi(this);
        this->InitializeForm();
        this->Counter();
        this->InitializeForm_Time();
        this->ShowCurrentTime();
    
    }
    
    HomePage::~HomePage()
    {
        delete ui;
    }
    
    //初始化首页界面
    void HomePage::InitializeForm()
    {
        //去掉系统自带
        this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint |
                             Qt::WindowMinMaxButtonsHint);
        this->setWindowTitle(tr("子涵智家"));
        this->setWindowIcon(QIcon(":/Images/Icon/TitleBarIcon.ico"));
    }
    
    /******************************时钟模块***************************************/
    //初始化首页界面的时钟模块
    void HomePage::InitializeForm_Time()
    {
        //为时钟模块设置文本
        ui->HomePage_Hour->setText("08");
        ui->HomePage_Minute->setText("00");
        ui->HomePage_Colon->setText(":");
        ui->HomePage_Second->setText("00");
        ui->HomePage_Date->setText("2016年12月12日");
        ui->HomePage_Week->setText("星期一");
    
        //为时钟模块设置颜色
        ui->HomePage_Hour->setStyleSheet("color:#f2f2f2");
        ui->HomePage_Minute->setStyleSheet("color:#f2f2f2");
        ui->HomePage_Second->setStyleSheet("color:#f2f2f2");
        ui->HomePage_Colon->setStyleSheet("color:#f2f2f2");
        ui->HomePage_Date->setStyleSheet("color:#f2f2f2");
        ui->HomePage_Week->setStyleSheet("color:#f2f2f2");
    }
    
    //时钟模块中的计数器
    void HomePage::Counter()
    {
        new_timer = new QTimer(this);
        connect(new_timer,SIGNAL(timeout()),this,SLOT(ShowCurrentTime()));
        new_timer->start(1000);//定时1s钟。
    }
    
    //显示当前时间
    void HomePage::ShowCurrentTime()
    {
        ui->HomePage_Hour->setText(QTime::currentTime().toString("hh"));
        ui->HomePage_Minute->setText(QTime::currentTime().toString("mm"));
        ui->HomePage_Second->setText(QTime::currentTime().toString("ss"));
        ui->HomePage_Date->setText(QDate::currentDate().toString("yyyy年MM月dd日"));
        ui->HomePage_Week->setText(QDate::currentDate().toString("dddd"));
    }
    
    
    

    But the clock didn't move, it just showed the current clock.Compilation runs with the following results:

    QObject::connect: No such slot HomePage::ShowCurrentTime() in ..\ZiHan_SmartHome\HomePage.cpp:58
    QObject::connect:  (receiver name: 'HomePage')
    

  • Lifetime Qt Champion

    Hi @Qt_HuangZiHan,

    you need to change your header like this:

    private:
        Ui::HomePage *ui;
    
        QTimer *new_timer;
    
        void InitializeForm();
        void InitializeForm_Time();
        void Counter();
    
    private slots:
        void ShowCurrentTime();
    };
    

    Don't forget to run Build > Run Qmake afterwards, then build your project.

    Regards



  • @aha_1980 I take other people's source code to study, why others are not wrong?Its code is as follows:
    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"
    #include "kitchen.h"
    #include "curtain_control.h"
    
    namespace Ui {
    class Widget;
    }
    
    class Widget : public QWidget
    {
        Q_OBJECT
    
        enum enum_widget
        {
            enum_widget_home=1,             //首页
            enum_bedroom=6,                 //卧室
            enum_kitchen = 5,               //厨房
            enum_parlour=4,                 //客厅
            enum_toolButton_control = 0,    //控制主页
            enum_paly_music=3,              //音乐播放界面
            enum_curtain_control=2,         //窗帘界面
    
            //E_MODEL_WIDGET=5,             //模式控制
    
    
            //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();
        void count();
    
    
        settingDialog *new_settingDialog;           //设置界面
        Parlour *new_parlourWidget;                 //客厅界面
        bedroom *new_bedRoomWidget;                 //卧室界面
        kitchen *new_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
    
    

    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();
        this->ShowCurrentDataTime_SlotsAndStyle();
    }
    
    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);
    }
    
    void Widget::InitializeWidget()
    {
        ui->widget_middle->setCurrentIndex(enum_widget_home);
        //qDebug()<<"E_HOME_WIDGET is = "<<enum_homepage;
        //要按下家居控制按钮后才进入中间部分样式。
    
        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();
        new_kitchenWidget = new kitchen();
        //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->widget_middle->addWidget(new_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();
         qDebug()<<"当前部件数量="<<ui->widget_middle->count();
    }
    
    void Widget::on_widget_bedroom_clicked()
    {
          this->setCurrentWidget(enum_bedroom);
          qDebug()<<"当前索引页 卧室:="<<ui->widget_middle->currentIndex();
          qDebug()<<"当前部件数量="<<ui->widget_middle->count();
    }
    
    void Widget::on_widget_kitchen_clicked()
    {
          ui->widget_middle->setCurrentIndex(enum_kitchen);
          qDebug()<<"当前索引页 窗帘控制:="<<ui->widget_middle->currentIndex();
          qDebug()<<"当前部件数量="<<ui->widget_middle->count();
    }
    
    void Widget::on_widget_safety_clicked()
    {
    
    }
    
    void Widget::on_widget_parlor_clicked()
    {
         this->setCurrentWidget(enum_parlour);
         qDebug()<<"当前索引页 客厅:="<<ui->widget_middle->currentIndex();
         qDebug()<<"当前部件数量="<<ui->widget_middle->count();
    }
    
    void Widget::on_toolButton_control_clicked()
    {
         this->setCurrentWidget(enum_toolButton_control);
         qDebug()<<"当前索引页 家居控制:="<<ui->widget_middle->currentIndex();
         qDebug()<<"当前部件数量="<<ui->widget_middle->count();
    }
    
    void Widget::on_toolButton_music_clicked()
    {
         ui->widget_middle->setCurrentIndex(enum_paly_music);
         qDebug()<<"当前索引页 音乐播放:="<<ui->widget_middle->currentIndex();
         qDebug()<<"当前部件数量="<<ui->widget_middle->count();
    }
    
    void Widget::on_toolButton_statistics_clicked()
    {
    
    }
    
    void Widget::on_toolButton_curtain_clicked()
    {
         ui->widget_middle->setCurrentIndex(enum_curtain_control);
         qDebug()<<"当前索引页 窗帘控制:="<<ui->widget_middle->currentIndex();
         qDebug()<<"当前部件数量="<<ui->widget_middle->count();
    }
    
    void Widget::on_toolButton_module_clicked()
    {
    
    }
    
    

  • Lifetime Qt Champion

    @Qt_HuangZiHan said in My clock won't move.:

    private slots:

    That one is important!


Log in to reply