APU
APU

Reputation: 239

QTGUI - How to print a message(QString) on the mainwindow that comes from other Classes?

This question sounds simple, but I've struggled with it.

I have a.h, b.h, c.h, mainwindow.h

and a.cpp, b.cpp, c.cpp, main.cpp, mainwindow.cpp

In a, b, c these C++ Classes I have some custom functions, the common between them is they all use "printf" to print out some messages(since they all origin from C++).

In my UI, I drag a text edit and uses "ui->textedit->append(QString str)" to print out messages.

The code above works fine if written in "mainwindow.cpp", but I want those message in a, b, c Classes can also be print in my text edit component.

How should I do?

Upvotes: 0

Views: 326

Answers (1)

Michael Vincent
Michael Vincent

Reputation: 1660

There are two ways that I would approach this:

  1. Make a public method in mainwindow which has a QString as a parameter. This function will use ui->textedit->append() to write the passed in string to the text edit widget.
  2. Use a signal/slot combination in much the same way as in 1.

In a, b cnd c you will replace the occurances of printf with either the call to the function, or by emitting a signal.

I would probably go with the second option.

Upvotes: 1

Related Questions