【发布时间】:2016-08-02 09:43:54
【问题描述】:
我是 GUI 设计的新手。在这里,在使用 DDS(OpenSplice)发送和接收消息(浮点数、整数值)时,我试图向我已经存在的标签添加一个按钮(显示一些浮点值,如下所示),以便在单击按钮后,我应该能够在我的标签中看到数据。 我尝试在 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;
感谢任何帮助。
注意:仅呈现代码的 sn-ps
【问题讨论】:
-
我不明白你的问题是什么。 QLabel 中的文字没有显示吗?注意:
repaint()在 MainWindow 构造函数返回后调用一次。这很可能在调用walk()之前。因此,我猜是更新问题。只需尝试调整/移动您的应用程序并查看重绘是否绘制了新值。 (如果有,可以使用wSend->update()更新walk()中的Label) -
@BernhardHeinrich 运行可执行文件时正在显示文本。现在我希望窗口打开并等待我单击按钮来设置文本。毕竟我想添加一个按钮
-
如果要添加更多详细信息,请告诉我
-
@AkhilChandraMaganti 如果我的回答对您有用,请奖励我。否则,如果您还有其他问题,请告诉我。 stackoverflow.com/help/privileges/set-bounties
-
嗨 Akhil,对不起,我仍然不明白这个问题....您想将“紧急”按钮添加到 MainLayout 吗? - 要添加到 MainWindow,您应该用真正的 mainLayout 替换行
QVBoxLayout *mainLayout = new QVBoxLayout;。 (使用 FastSender 的构造函数参数parent和this,将parent转换为 QMainWindow* 和我们的QMainWindow::centralWidget()->layout())。或者,稍微简单一点:将紧急按钮添加到 ui。
标签: c++ qt user-interface signals-slots data-distribution-service