想要将按钮添加到setTimer和setText

时间:2016-08-02 09:43:54

标签: c++ qt user-interface signals-slots data-distribution-service

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

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;

感谢任何帮助。

注意:仅提供代码的片段

1 个答案:

答案 0 :(得分:0)

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

<强> 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