繁体   English   中英

Qt信号和插槽混乱

[英]Qt Signals and Slots Confusion

我一直在阅读有关Qt信号和插槽的信息,我正在尝试使其正常工作,但直到现在仍未成功。 我希望有人能指出我正确的方向。
我有两个文件, homeCommand.cppmessagelogcommand.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM