Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to insert an image on QTextEdit

How to insert an image on QTextEdit

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
11 Posts 5 Posters 9.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jacky2580
    wrote on 2 Jun 2018, 06:48 last edited by
    #1

    how can I do to display images. Here is my code but it does not open an image with QTextEdit.
    thank you```

     QString text2 = ui->textEdit->toPlainText();
        ui->textEdit->setText(text2);
        QString file = QFileDialog::getOpenFileName(this);
        if (!file.isEmpty())
         {   QFile sFile(file);
        if (sFile.open(QFile::ReadOnly |QFile::Text))
        {
           QTextStream in (&sFile);
           QString text1;
          text2 +=  "\n"+ text1 + in.readAll() ;
           sFile.close();
           ui->textEdit->setPlainText(text2);
    
          }
         }
    A 1 Reply Last reply 2 Jun 2018, 10:26
    0
    • J jacky2580
      2 Jun 2018, 06:48

      how can I do to display images. Here is my code but it does not open an image with QTextEdit.
      thank you```

       QString text2 = ui->textEdit->toPlainText();
          ui->textEdit->setText(text2);
          QString file = QFileDialog::getOpenFileName(this);
          if (!file.isEmpty())
           {   QFile sFile(file);
          if (sFile.open(QFile::ReadOnly |QFile::Text))
          {
             QTextStream in (&sFile);
             QString text1;
            text2 +=  "\n"+ text1 + in.readAll() ;
             sFile.close();
             ui->textEdit->setPlainText(text2);
      
            }
           }
      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 2 Jun 2018, 10:26 last edited by
      #2

      @jacky2580

      Using Google I found this link: https://stackoverflow.com/questions/3254652/several-ways-of-placing-an-image-in-a-qtextedit which provides several solutions.

      Qt has to stay free or it will die.

      J 2 Replies Last reply 2 Jun 2018, 21:44
      3
      • A aha_1980
        2 Jun 2018, 10:26

        @jacky2580

        Using Google I found this link: https://stackoverflow.com/questions/3254652/several-ways-of-placing-an-image-in-a-qtextedit which provides several solutions.

        J Offline
        J Offline
        jacky2580
        wrote on 2 Jun 2018, 21:44 last edited by
        #3

        @aha_1980
        Thanks you .
        I try this solution but it doesn't work. I don't need an QTextDocument.

        M 1 Reply Last reply 2 Jun 2018, 21:54
        0
        • J jacky2580
          2 Jun 2018, 21:44

          @aha_1980
          Thanks you .
          I try this solution but it doesn't work. I don't need an QTextDocument.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 2 Jun 2018, 21:54 last edited by
          #4

          @jacky2580
          Hi,
          its not possible to show images using just plain text.
          If you don't want to use QTextDocument , you will need to construct some html
          to show image instead.
          it will not accept some image as text and show it.

          1 Reply Last reply
          1
          • A aha_1980
            2 Jun 2018, 10:26

            @jacky2580

            Using Google I found this link: https://stackoverflow.com/questions/3254652/several-ways-of-placing-an-image-in-a-qtextedit which provides several solutions.

            J Offline
            J Offline
            jacky2580
            wrote on 2 Jun 2018, 22:06 last edited by
            #5

            @aha_1980
            I resolved this problem.Thank you so much.

            //your code here
            

            QString cheminImage = QFileDialog::getOpenFileName(this, "Choisir une image", QString(), "Images (.)");

                QString texteFinal = ui->textEdit->toHtml() + "<img src = \""+ cheminImage +"\" alt = \"\"/>";
            
                ui->textEdit->setHtml(texteFinal);
            
            A 1 Reply Last reply 5 Jun 2021, 16:31
            1
            • P Offline
              P Offline
              Pablo J. Rogina
              wrote on 3 Jun 2018, 00:11 last edited by
              #6

              @jacky2580 if your issue is solved,please don't forget to mark your post as such. Thanks

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2
              • J jacky2580
                2 Jun 2018, 22:06

                @aha_1980
                I resolved this problem.Thank you so much.

                //your code here
                

                QString cheminImage = QFileDialog::getOpenFileName(this, "Choisir une image", QString(), "Images (.)");

                    QString texteFinal = ui->textEdit->toHtml() + "<img src = \""+ cheminImage +"\" alt = \"\"/>";
                
                    ui->textEdit->setHtml(texteFinal);
                
                A Offline
                A Offline
                Alex_Tsikhun
                wrote on 5 Jun 2021, 16:31 last edited by
                #7

                @jacky2580 , maybe you know how change size image?

                M 1 Reply Last reply 5 Jun 2021, 16:41
                0
                • A Alex_Tsikhun
                  5 Jun 2021, 16:31

                  @jacky2580 , maybe you know how change size image?

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 5 Jun 2021, 16:41 last edited by
                  #8

                  @Alex_Tsikhun

                  try with

                  <img src=":/GFX/Icons/clear_today.svg" width="48" height="48"/>
                  
                  A 1 Reply Last reply 6 Jun 2021, 08:38
                  0
                  • M mrjj
                    5 Jun 2021, 16:41

                    @Alex_Tsikhun

                    try with

                    <img src=":/GFX/Icons/clear_today.svg" width="48" height="48"/>
                    
                    A Offline
                    A Offline
                    Alex_Tsikhun
                    wrote on 6 Jun 2021, 08:38 last edited by
                    #9

                    @mrjj , i can set the size via QComboBox for example?

                    M 1 Reply Last reply 6 Jun 2021, 08:50
                    0
                    • A Alex_Tsikhun
                      6 Jun 2021, 08:38

                      @mrjj , i can set the size via QComboBox for example?

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 6 Jun 2021, 08:50 last edited by mrjj 6 Jun 2021, 08:50
                      #10

                      @Alex_Tsikhun
                      Hi
                      Sure you could let user choose some predefined sized from a combobox and then use
                      these values to make the HTML.

                      However, if you plan on letting user alter size after it has been inserted then
                      you might want to look into
                      https://doc.qt.io/qt-5/qtextimageformat.html
                      which lets you control size after it has been inserted.

                      A 1 Reply Last reply 6 Jun 2021, 19:26
                      0
                      • M mrjj
                        6 Jun 2021, 08:50

                        @Alex_Tsikhun
                        Hi
                        Sure you could let user choose some predefined sized from a combobox and then use
                        these values to make the HTML.

                        However, if you plan on letting user alter size after it has been inserted then
                        you might want to look into
                        https://doc.qt.io/qt-5/qtextimageformat.html
                        which lets you control size after it has been inserted.

                        A Offline
                        A Offline
                        Alex_Tsikhun
                        wrote on 6 Jun 2021, 19:26 last edited by
                        #11

                        @mrjj , tnx

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved