Search code examples
c++qtuser-interfacesignals-slotsdata-distribution-service

Want to add pushbutton to setTimer and setText


I am newbie in GUI design. Here, in sending and receiving messages (float, integer values) using DDS (OpenSplice) I am trying to add a pushButton to my already existing labels(displaying some float values as shown below), so that after a clicking on the pushButton, I should be able to see data in my label. I tried adding a push button with the help of Qt Network sender Example. Now I get the error undefined reference to 'MainWindow::on_pushButton_clicked() in the file moc_mainwindow.cpp while building the project.

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;

Any Help Appreciated.

Note: Only snippets of the code presented


Solution

  • I got it to work this way. Does this work for you?

    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