Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved How to insert an image on QTextEdit

    Tools
    5
    11
    6723
    Loading More Posts
    • 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
      jacky2580 last edited by

      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);
      
            }
           }
      aha_1980 1 Reply Last reply Reply Quote 0
      • aha_1980
        aha_1980 Lifetime Qt Champion @jacky2580 last edited by

        @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 Reply Quote 3
        • J
          jacky2580 @aha_1980 last edited by

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

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @jacky2580 last edited by

            @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 Reply Quote 1
            • J
              jacky2580 @aha_1980 last edited by

              @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 Reply Quote 1
              • Pablo J. Rogina
                Pablo J. Rogina last edited by

                @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 Reply Quote 2
                • A
                  Alex_Tsikhun @jacky2580 last edited by

                  @jacky2580 , maybe you know how change size image?

                  mrjj 1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion @Alex_Tsikhun last edited by

                    @Alex_Tsikhun

                    try with

                    <img src=":/GFX/Icons/clear_today.svg" width="48" height="48"/>
                    
                    A 1 Reply Last reply Reply Quote 0
                    • A
                      Alex_Tsikhun @mrjj last edited by

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

                      mrjj 1 Reply Last reply Reply Quote 0
                      • mrjj
                        mrjj Lifetime Qt Champion @Alex_Tsikhun last edited by mrjj

                        @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 Reply Quote 0
                        • A
                          Alex_Tsikhun @mrjj last edited by

                          @mrjj , tnx

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post