【问题标题】:Qtimer does not call function every 5 secondsQtimer 不是每 5 秒调用一次函数
【发布时间】:2023-03-26 18:25:01
【问题描述】:

我使用 Qt creator 创建了一个表单。该表单有三个选项卡,每个选项卡有 30 个字段。所以我希望表单上的数据每 5 秒刷新一次。我 sed QTimer 类来实现这个功能。这是我的代码:

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <ctime>
#include <iostream>
#include <QPixmap>
#include <stdio.h>
#include <QProcess>
#include <QString>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QLabel>
#include <QTimer>
#include <ctime>

using namespace std;

void load_Tab1Values(Ui::MainWindow* ui)
{
    // append values
}

void load_Tab2Values(Ui::MainWindow* ui)
{
    //append values
}

void MainWindow::onTabChanged(int tabIndex)
{
    cout<<"the tab index is:"<<tabIndex<<endl;
    if (tabIndex == 0)
    {
        // Create the first tab elements

        cout<<"tab 0"<<endl;
    }
    else if (tabIndex == 1)
    {
        // ...

        cout<<"tab 1"<<endl;
    }
}

void MainWindow::refresh_values()
{
    cout<<"values refreshed"<<endl;
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    time_t now = time(0);
    char* dt = ctime(&now);

    ui->setupUi(this);
    this->setWindowTitle("First Qt Project");

    load_Tab1Values(ui);
    load_Tab2Values(ui);
    connect( ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)) ); 

    QTimer *timer= new QTimer(this);
    timer->connect(timer,SIGNAL(timeout()),this,SLOT(refresh_values()));
    timer->start(5000);
}

MainWindow::~MainWindow()
{
    delete ui;
}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui
{
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void ontabchanged(int tabIndex);    

    void refresh_values();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

根据代码,函数 refresh_values 应每 5 秒调用一次,但不会调用该函数。是否应该在 Main.cpp 中添加它,以便每 5 秒调用一次。你能告诉我我在这里缺少什么吗?谢谢。

【问题讨论】:

  • 试试qDebug() &lt;&lt; connect(timer,SIGNAL(timeout()),this,SLOT(refresh_values())); 并包含QDebug。你得到了什么,是真是假?
  • @Chernobyl 所以我添加了这些语句 cout
  • @Chernobyl 没有变化..该函数仍未被调用。因为每当我单击选项卡时我在表单上有三个选项卡,就会调用此函数。我不确定为什么单击选项卡时会调用此函数。即使这样,当 Qtimer 正确使用时,该函数也应该每 5 秒调用一次。还有,定时器应该在 main.cpp 而不是 mainwindow.cpp 中?
  • 有点奇怪,这不是你的完整代码,对吧?发布完整代码和 QTimer 在 mainWindow.cpp 上工作。你不应该在 main.cpp 中这样做
  • @Chernobyl 我已经编辑了这两个文件,这是我拥有的代码。我在头文件和 mainwindow.cpp 中添加了 onTabChanged 插槽,我在 QTimer 之前调用了两个函数,将值加载到选项卡中的字段中。这会导致问题吗?

标签: c++ qt qt4 qt-creator


【解决方案1】:

我找到了解决这个问题的方法:

默认情况下,创建的任何插槽都在 MainWindow.h 文件中的“私有插槽”部分下,如下所示:

private slots:
//default slots created from the ui(when the user right clicks on a button and then connects to a     slot)

所以在我的例子中,因为函数 refresh_values 是一个用户定义的函数,我手动为这个函数创建了一个 SLOT,它应该在公共插槽部分。所以以这种方式包含它解决了这个问题。

public slots:

    void refresh_values();

【讨论】:

  • 我相信这只是 C++ 课程的标准。任何不受公共或保护的东西都是私有的......不仅仅是插槽。
  • @kiss-o-matic 是的,它是一个 c++ 标准。在这种情况下,我指的是插槽,只是为了针对添加插槽时遇到的问题提出解决方案。手动创建插槽时,用户可以在已经可用的私有插槽部分中定义它们。
猜你喜欢
  • 2018-05-17
  • 1970-01-01
  • 2011-11-06
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2019-04-21
相关资源
最近更新 更多