【发布时间】:2014-01-17 21:31:53
【问题描述】:
我一直在阅读有关 Qt 信号和插槽的信息,我正在尝试让它工作,但直到现在都没有成功。我希望有人能指出我正确的方向。
我有两个文件,homeCommand.cpp 和 messagelogcommand.cpp。我在messagelogcommand.cpp 中有一个QPlainTextEdit 对象,我想从homeCommand.cpp 更新它。
如何使用信号和插槽做到这一点?正在调用我的信号,因为我的 QDebug 每秒打印一次,但是小部件没有更新。
这就是我想要做的:
在 MessageLogCommand.h 中
class MessageLogCommand : public QWidget
{
Q_OBJECT
public:
explicit MessageLogCommand(QWidget *parent = 0);
QLabel *homeLabel;
QPlainTextEdit *messageLog;
public Q_SLOTS:
void updateWidgets(const QString &text);
};
homeCommand.h
class homeCommand : public QWidget
{
Q_OBJECT
Q_SIGNALS:
void textChanged(const QString &text);
public:
explicit homeCommand(QWidget *parent = 0);
public slots:
void run(void);
void getHealthStatusPacket(void);
homeCommand.cpp
homeCommand::homeCommand(QWidget *parent) : QWidget(parent)
{
...
//Timer
QTimer *timer = new QTimer(this);
timer->setSingleShot(false);
connect(timer, SIGNAL(timeout()), this, SLOT(run()));
timer->start(1000);
setLayout(layout);
}
void homeCommand::run(void)
{
getHealthStatusPacket();
}
void homeCommand::getHealthStatusPacket(void)
{
...
Q_EMIT textChanged("ZOMG");
}
在 MessageLogCommand.cpp 中
MessageLogCommand::MessageLogCommand(QWidget *parent) : QWidget(parent)
{
QGridLayout *layout = new QGridLayout;
QWidget::setFixedHeight(600);
//Sub-system Label
homeLabel = new QLabel("GSS Message Log");
QFont subsystemFont = homeLabel->font();
subsystemFont.setPointSize(12);
subsystemFont.setBold(true);
homeLabel->setFont(subsystemFont);
layout->addWidget(homeLabel, 0, 0);
//Event Log
messageLog = new QPlainTextEdit();
messageLog->setFixedHeight(500);
messageLog->setFixedWidth(600);
layout->addWidget(messageLog, 2,0);
setLayout(layout);
}
void MessageLogCommand::updateWidgets(const QString &text)
{
qDebug() << "Here";
messageLog->appendPlainText(text);
}
在 main.cpp 中
MessageLogCommand s;
homeCommand m;
QObject::connect(&m, SIGNAL(textChanged(QString)), &s, SLOT(updateWidgets(QString)));
【问题讨论】:
-
我认为任何人都可以提供帮助,您需要在
code块中提供您拥有的代码 -
向我们展示更多。也许没有必要使用信号&槽
标签: c++ qt signals-slots