Yes you can do this.
We have a component that has a concept of "Plans", it uses the same view for different access levels, but makes the fields accessible or not depending on the users groups.
So for those uses that can "Run" a plan but not edit it, we turn "off" a bunch of fields. Depending on the field type this may mean setting several field attributes, e.g.
$this->form->setFieldAttribute('name', 'class', 'readonly');
$this->form->setFieldAttribute('name', 'readonly', 'true');
$this->form->setFieldAttribute('description', 'class', 'readonly');
$this->form->setFieldAttribute('description', 'disabled', 'true');
$this->form->setFieldAttribute('description', 'type', 'text');
$this->form->setFieldAttribute('published', 'class', 'readonly');
$this->form->setFieldAttribute('published', 'readonly', 'true');
$this->form->setFieldAttribute('publish_up', 'class', 'readonly');
$this->form->setFieldAttribute('publish_up', 'readonly', 'true');
$this->form->setFieldAttribute('publish_up', 'format', '%Y-%m-%d %H:%M:%S');
$this->form->setFieldAttribute('publish_up', 'filter', 'user_utc');
$this->form->setFieldAttribute('publish_down', 'class', 'readonly');
$this->form->setFieldAttribute('publish_down', 'readonly', 'true');
$this->form->setFieldAttribute('publish_down', 'format', '%Y-%m-%d %H:%M:%S');
$this->form->setFieldAttribute('publish_down', 'filter', 'user_utc');
So, depending on what your myReadOnlyCode
field is you can do it by setting one or more of the attributes as shown above, e.g. if it's just a standard text input:
$this->form->setFieldAttribute('myReadOnlyCode', 'class', 'readonly');
$this->form->setFieldAttribute('myReadOnlyCode', 'readonly', 'true');