Daniel Viaño
Daniel Viaño

Reputation: 564

How to connect callback with QPlainTextEdit change?

I am listening to a topic and want to display and update the received value every time it changes.

This function creates the logging part of the GUI

  QGroupBox *Window::startLoggingGroup()
  {
      QGroupBox *groupBox = new QGroupBox(tr("Logging"));

      log_value = new QPlainTextEdit;
      log_value->setReadOnly(true);
      log_value->setPlaceholderText("Value will appear here \n");

      QHBoxLayout *hbox = new QHBoxLayout;
        hbox->addWidget(log_carrot);

      groupBox->setLayout(hbox);
      return groupBox;

  }

This is the code executed on every value changed.

void EFISWindow::callback_value(const geometry_msgs::PoseStamped& msg){
    QString qst = QString::number(msg.pose.position.z);
    log_value->setPlainText(qst);

}

It works at first, I can see the GUI and some values, but after several messages like the ones I show now it crashes:

QObject::connect: Cannot queue arguments of type 'QTextBlock' (Make sure 'QTextBlock' is registered using qRegisterMetaType().) QObject: Cannot create children for a parent that is in a different thread. (Parent is QTextDocument(0x227e580), parent's thread is QThread(0x1f9db50), current thread is QThread(0x7f4ae40011d0)

How can I solve this threading issue? Maybe using a signal-slot design? I don't really understand why this is not working.

Upvotes: 0

Views: 291

Answers (1)

zapprox
zapprox

Reputation: 61

You should not access a GUI element from another thread.

Maybe using a signal-slot design?

Yes, Your worker object should have a signal that you emit at some point and you should connect that signal to update the "log_value" value.

Upvotes: 3

Related Questions