Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Attempting to change QImage text



  • I have a QImage which I am painting in the constructor like so:

        QImage image(prjcfgISO11783UT_datamaskWidth+2, prjcfgISO11783UT_datamaskHeight+2, QImage::Format_Mono);
        image.fill(1);
        QPainter painter;
    
        painter.begin(&image);
    
        painter.setPen(Qt::black);
        painter.drawText(QRect(0, 0, prjcfgISO11783UT_datamaskWidth+2, prjcfgISO11783UT_datamaskHeight+2), Qt::AlignCenter, tr("Some Text..", "header with description"));
    
        m_image = image;
    

    and I have a translate() method setup like so:

      bool TheObject::event(QEvent *event)
      {
        if (event->type() == QEvent::LanguageChange)
        {
          retranslate();
        }
        return false;
      }
    
      void DataMaskObject::retranslate()
      {
        
      }
    

    how can I go about re-translating the text of m_image?


  • Lifetime Qt Champion

    Hi,

    Put the image setup code in its own method and call it from the constructor and retranslate.


  • Lifetime Qt Champion

    Hi,

    Put the image setup code in its own method and call it from the constructor and retranslate.



  • @SGaist Is there no way to change the text created by painter.drawText inline? The QImage setText() doesn't seem to work like I think it should:

    m_image.setText(tr("header"), tr("New String"));
    

  • Lifetime Qt Champion

    No, you have to redraw it as setText is for metadata.

    What is the issue with the solution I suggested ?


Log in to reply