0

I've got an std::string object called str;

I've also got an QLineEdit object called line_edit.

I need that str reflects whatever the user writes in line_edit, this I know how to do trough signals and slots.

But I also need that whenever str changes , QLineEdit automatically displays the new contents of str.

How can I do this?

Is it MVC what I need? I've been reading about it but I can't get it yet. Also, examples I've read try to keep mutually updated subobjects of QWidget which makes me think there is some magic happening in there.

How can I achieve that reactivity?

David
  • 1
  • 1
  • Who and how changes `str` variable? – vahancho Jun 14 '18 at 10:23
  • 1
    When the user clicks a button "run", a "virtual processor" starts executing instructions, one at a time, with a bit of delay so the user can see what is happening, as the processor executes instructions, it will write new data to its registers, I wish my QLineEdit objects to reflect the state of those registers, so the user can see how registers change as the execution proceeds, the reason for they to be QLineEdits it's because I want the user to be able to write to those registers on the fly as well. – David Jun 14 '18 at 10:29
  • You may want to look at using a [`QDataWidgetMapper`](http://doc.qt.io/archives/qt-4.8/qdatawidgetmapper.html) to map a [`QStringListModel`](http://doc.qt.io/qt-5/qstringlistmodel.html) containing your register strings, to an equal number of QLineEdits. – acraig5075 Jun 14 '18 at 13:47

1 Answers1

0

First of all it might be easier to use QString instead of std::string.

You can call line_edit->setText(str); to update line_edit from your code.

You can use the signal QLineEdit::textChanged to modify the content of str when writing to the QLineEdit.

There are multiple ways of handling signals, one way would be to use QObject::connect

It might look like this: QObject::connect(line_edit, &QLineEdit::textChanged, this, &YourClassName::setText);

And then you create setText(){ str = line_edit->text}

ALDSMQJF
  • 19
  • 5