[英]Qt Signals and Slots Confusion
我一直在閱讀有關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)));
一個非常基本的示例是:
class MainClass:public QObject //class must be derived from QObject!
{
Q_OBJECT //this macro must be in the class definition
//so the moc compiler can generate the necessary glue code
public:
void doSomething() {
...
Q_EMIT textChanged(someText);
}
Q_SIGNALS:
void textChanged(const QString &text);
};
class SubClass:public QObject
{
Q_OBJECT
public Q_SLOTS:
void onTextChanged(const QString &text) { //do not inline
//do something
}
};
int main()
{
QApplication a;
MainClass m;
SubClass s;
QObject::connect(&m, SIGNAL(textChanged(QString)),
&s, SLOT(onTextChanged(QString))); //const and & are removed from
//the arguments
return a.exec(); //run the event loop
}
因此,有兩件事很重要:1.信號和插槽必須在派生自QObject的類中聲明2.包含信號和插槽聲明的類必須將Q_OBJECT宏添加到類聲明中
為您簡化操作:始終在頭文件中聲明包含信號或插槽的類(絕不要在.cpp文件中)。
信號和插槽的一個很好的起點是: http : //woboq.com/blog/how-qt-signals-slots-work.html,但官方的Qt文檔也做到了: http : //qt-project.org /doc/qt-4.8/signalsandslots.html
基本上會發生什么:您聲明一些特殊的方法(信號和插槽),在編譯階段Qt會生成額外的CPP文件來處理您的方法(moc),然后將所有內容編譯並鏈接在一起,最后在Qt或其他人發出時一個信號,它將進入相應的插槽。
我嘗試解釋一下。
在main.h中,您應該聲明一個信號:
signals:
void textChanged(const QString& text);
在messagelog.h中,您應該聲明一個插槽:
public slots:
void updateWidgets(const QString& text);
在main.cpp中,您應該發出以下信號:
void TheMethod() {
emit this->textChanged("Your text/value");
}
在messagelog.cpp中,您應該獲得以下值:
// Note: Normalized signal/slot signatures drop the consts and references.
connect(&a, SIGNAL(textChanged(QString)), this, SLOT(updateWidgets(QString)));
void updateWidgets(const QString& text) {
messageLog = new QPlainTextEdit();
messageLog->setFixedHeight(500);
messageLog->setFixedWidth(600);
messageLog->setPlainText(text)
layout->addWidget(messageLog, 2,0);
}
我認為應該可以。
更新:完整的示例: https : //dl.dropboxusercontent.com/u/29647980/test.zip
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.