想要将按钮添加到 setTimer 和 setText

Want to add pushbutton to setTimer and setText

我是GUI设计的新手。在这里,在使用 DDS (OpenSplice) 发送和接收消息(浮点数、整数值)时,我试图将一个按钮添加到我已经存在的标签中(显示一些浮点值,如下所示),以便在单击按钮后,我应该能够在我的标签中看到数据。 我尝试在 Qt Network sender Example 的帮助下添加一个按钮。现在我在构建项目时收到错误 undefined reference to 'MainWindow::on_pushButton_clicked() in the file moc_mainwindow.cpp.

fastsender.cpp

FastSender::FastSender(QLabel *x, QObject *parent) : QObject(parent)
{
wSend = x;
dataTimer = new QTimer(this);
emergency = new QPushButton(tr("Emergency"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(emergency,QDialogButtonBox::ActionRole);


connect(emergency, SIGNAL(clicked()), this, SLOT(startsending()));
connect(dataTimer, SIGNAL(timeout()), this, SLOT(walk()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(buttonBox);
}

void FastSender::startsending()
{
emergency->setEnabled(false);
dataTimer->start(100); // Interval 0 means to refresh as fast as possible
}


int FastSender::walk()
{
msg->value= i+0.1;
snprintf (buf, MAX_MSG_LEN, "Message no. %d", i);

cout << "Writing message: \"" << msg->value << "\"" << endl;
status = talker->write(*msg, userHandle);

QString s=  QString::number(msg->value, 'f',8);
wSend->setText(s);
}

fastsender.h

class FastSender : public QObject
{
Q_OBJECT
public:
explicit FastSender(QObject *parent = 0);
FastSender(QLabel *x, QObject *parent = 0);
~FastSender();
signals:

private:
QTimer* dataTimer;
QLabel *wSend;
DDS::DomainParticipantFactory_var       dpf;

DDS::DomainParticipant_var              parentDP;
DDS::Topic_var                          signalTopic;
DDS::DataReader_var                     parentReader;
DDS::DataWriter_var                     parentWriter;
fw::signalSeq_var                       msgSeq;
char  *                                 signalTypeName;
fw::signalDataWriter_var                talker;
fw::signal                              *msg;
DDS::InstanceHandle_t                   userHandle;
DDS::Publisher_var                      fwPublisher;
int                                     alpa;
QPushButton                             *emergency;
QDialogButtonBox                        *buttonBox;

public slots:

int walk();
int hello();
void startsending();
};

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
fwdds = new FastWanDDS(ui->label);//receiving values are displayed in label
fwdds1 = new FastSender(ui->label_2);//Sending values are displayed in label_2
}

mainwindow.h

Ui::MainWindow *ui;
QTimer* timer;
int counter;
FastWanDDS *fwdds;
FastSender *fwdds1;

感谢任何帮助。

注意:仅提供代码片段

我让它以这种方式工作。这对你有用吗?

mainwindow.cpp

#include "mainwindow.h"
#include "fastsender.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QLabel* label = new QLabel("unchanged");
    FastSender* fs = new FastSender(label);
    setCentralWidget(fs);
}

MainWindow::~MainWindow()
{

}

fastsender.h

#ifndef FASTSENDER_H
#define FASTSENDER_H

#include <QLabel>
#include <QTimer>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QHBoxLayout>

class FastSender : public QWidget
{
    Q_OBJECT
public:
    FastSender(QLabel* label, QWidget* parent = 0)
        : QWidget(parent)
        , _label(label)
        , _timer(new QTimer)
        , _emergency(new QPushButton(tr("Emergency")))
        , _bbox(new QDialogButtonBox)
    {
        _bbox->addButton(_emergency, QDialogButtonBox::ActionRole);

        QHBoxLayout* layout = new QHBoxLayout;
        layout->addWidget(_label);
        layout->addWidget(_bbox);

        setLayout(layout);

        connect(_emergency, SIGNAL(clicked(bool)), this, SLOT(startsending()));
        connect(_timer, SIGNAL(timeout()), this, SLOT(walk()));
    }

public slots:
    void startsending()
    {
        _emergency->setEnabled(false);
        _timer->start(100);
    }

    int walk()
    {
        _label->setText("changed");
        _emergency->setEnabled(true);
    }

private:
    QLabel* _label;
    QTimer* _timer;
    QPushButton* _emergency;
    QDialogButtonBox* _bbox;
};

#endif // FASTSENDER_H