2016-08-02 64 views
-1

我是GUI設計中的新手。在這裏,在使用DDS(OpenSplice)發送和接收消息(浮點數,整數值)我試圖添加一個pushButton到我已經存在的標籤(顯示一些浮點值,如下所示),以便在點擊pushButton之後,我應該能夠看到我標籤中的數據。 我試着在Qt Network sender Example的幫助下添加一個按鈕。現在,在構建項目時,我在文件moc_mainwindow.cpp中收到錯誤未定義的對'MainWindow :: on_pushButton_clicked()的引用。想要添加按鈕setTimer和setText

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; 

任何幫助表示讚賞。

注意:只有代碼的片段呈現

+0

我不明白什麼是你的問題。 QLabel中的文本是否不顯示?注意:在MainWindow構造函數返回後調用'repaint()'一次。這很可能在'walk()'被調用之前。因此我猜想更新問題。只是嘗試調整大小/移動你的應用程序,看看重繪是否繪製新值。 (如果是這樣,你可以使用'wSend-> update()'來更新'walk()'中的Label。) –

+0

@BernhardHeinrich當我運行可執行文件時,文本正在顯示。現在我想打開窗口並等待按下按鈕來設置文本。我想添加一個按鈕畢竟 –

+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