Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Are text descriptions posible?
Forum Updated to NodeBB v4.3 + New Features

Are text descriptions posible?

Scheduled Pinned Locked Moved General and Desktop
15 Posts 2 Posters 5.5k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mariusmssj
    wrote on last edited by
    #1

    Hey everyone I got a question,

    I read in a part of code from a text file(.txt) into a text browser, "so it looks like this":http://i.imgur.com/dLWob.jpg

    to do so I use this code:
    @QFile Tfile("./code_output/translate/translate.txt");
    Tfile.open(QIODevice::ReadOnly);
    QTextStream Tstream(&Tfile);
    QString translate = Tstream.readAll();
    Tfile.close();
    ui->textBrowser->setPlainText(translate);@

    What I want to do is to be able have a description appear when I hover the mouse cursor over a line of text in the textbrowser. Maybe it's possible to attach a description to a string when it's read in?

    What I wanted to ask is it possible to do so? And if so how would I do it?

    Thank you =]

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RaubTieR
      wrote on last edited by
      #2

      May be you should display your text more like HTML document, and then include descriptions in HTML way as tags. AFAIK that is unable to add descriptions to separate strings as you do it for the whole text browser.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mariusmssj
        wrote on last edited by
        #3

        [quote author="RaubTieR" date="1334409250"]May be you should display your text more like HTML document, and then include descriptions in HTML way as tags. AFAIK that is unable to add descriptions to separate strings as you do it for the whole text browser.[/quote]

        so I would need to use toHtml () and setHtml () functions? or would I modify:
        @ QFile Tfile("./code_output/translate/translate.txt");
        Tfile.open(QIODevice::ReadOnly);
        QTextStream Tstream(&Tfile);
        QString translate = Tstream.readAll();
        Tfile.close();
        ui->textBrowser->setPlainText(translate);
        @
        to read the string from the txt file as HTML?

        [edit]
        Ok i found out how it would go in the HTML code:
        @<A HREF="http://www.yourdomain.com/" TITLE="Your text description">Your Text</A>@

        But i still got no idea how to set up the normal text I got now into a HTML text

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RaubTieR
          wrote on last edited by
          #4

          What about the captions you want to provide for the text, where do you get them from? If you somehow parse the text, so this is where you can form the new string which will use HTML. During the parsing you may use come predictable substrings like those:
          @QString p = "<p style=' margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;'>"; // begin HTML line
          QString _p = "</p>"; // end line
          QString span_template = "<span title='%1'>";
          QString _span = "</span>";@

          Now you can form your strings this way (i show parsed and generated substrings just as constant strings here) :

          @QString html = p + span_template.arg("Line 1") + "Whatever on line1 " + span_template.arg("Vector3") + "vec3(1,0,0)" + _span + _span + _p; //first line
          html += p + span_template.arg("Line 2") + "Whatever on line2" + _span + _p;@

          When finished parsing, just append your html to "empty" browser's HTML:
          @ui->textBrowser->setHtml(ui->textBrowser->toHtml()+html);@

          May be it looks a little bit difficult, but through this way you can even colorize text like in Qt Creator.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mariusmssj
            wrote on last edited by
            #5

            It worked like a charm thank you very much :D

            But I ran into a problem, I am reading each line separately and using the html+= causes code duplication
            "I will show now here:":http://i.imgur.com/MrNZm.png

            I got multiple text files, 1 for each line of code that I display, and then I read from all of them and put them into the text browser.

            I need them to be separate as they are dynamic and always changing(When you change a value in the program it writes to the text file and then reads from it to the text browser). As you can see below.

            @if(GLFrame->RenderThread.getTranslate()==1)
            {
            QFile Tfile("./code_output/translate/translate.txt");
            Tfile.open(QIODevice::ReadOnly);
            QTextStream Tstream(&Tfile);
            QString translate = Tstream.readAll();
            Tfile.close();
            ui->textBrowser->setPlainText(translate);
            }

            if(GLFrame->RenderThread.getRotation()==1)
            {
                QFile Rfile&#40;"./code_output/rotation/rotation.txt"&#41;;
                Rfile.open(QIODevice::ReadOnly&#41;;
                QTextStream Rstream(&Rfile);
                QString rotation = Rstream.readAll();
                Rfile.close();
                ui->textBrowser->append(rotation);
            }
            
            if(GLFrame->RenderThread.getScale()==1)
            {
                QFile Scfile&#40;"./code_output/scale/scale.txt"&#41;;
                Scfile.open(QIODevice::ReadOnly&#41;;
                QTextStream Scstream(&Scfile);
                QString scale = Scstream.readAll();
                Scfile.close();
                ui->textBrowser->append(scale);
            }@
            

            I tried creating 2 versions of the HTML variables but that didn't work. Any suggestions?

            And thanks for helping me, I really appreciate it.

            1 Reply Last reply
            0
            • R Offline
              R Offline
              RaubTieR
              wrote on last edited by
              #6

              I have an idea.
              @QString StartLine = "<p style=' margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;'>"; // begin HTML line
              QString EndLine = "</p>"; // end line
              QMap<QString, QString> Replacement = loadRepls("replacements.txt"); // implement that as you wish
              QString InStr = readSource("source.c"); // again wherever you get this from
              QString OutStr; // storing HTML here

              QTextStream InTxt(InStr); // read string as stream
              while(!InTxt.atEnd())
              {
              OutStr += StartLine; // new line started in HTML
              QString line = InTxt.readLine();
              QTextStream ln(line);
              while(!ln.atEnd()) // parse the source line
              {
              QString word;
              ln>>word; // this also will read signs and everything
              if(Replacement.contains(word))
              OutStr += Replacement[word];
              else OutStr += word; // just pass if symbol is not to be replaced
              }

              OutStr += EndLine;
              }
              ui->textBrowser->append(OutStr);@

              In the code above i show the following algorithm:

              Prepare output string

              Obtain the replacement table

              Obtain the source text

              Interpret source text as stream#1

              While stream#1 is not parsed

              Add line-start HTML to output

              Take one line from stream#1

              Interpret the line as stream#2

              While stream#2 is not parsed

              Take one word from stream#2

              If the word present in replacement table add replacement to output

              Else add word itself to the output

              Add line-end HTML to the output

              Pass output string to the text browser.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mariusmssj
                wrote on last edited by
                #7

                Wow, this kinda complicates things a bit then. If you got some free time I would like to have a look at my tool and what it does, to get a better understand of what I am trying to achieve =]

                [url=http://mariusmssj.brinkster.net/release.rar]Executable .rar[/url]
                [url=http://mariusmssj.brinkster.net/release.zip]Executable .zip[/url]

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  RaubTieR
                  wrote on last edited by
                  #8

                  I tried the exe, nice app :)
                  But i don't see any reason that makes imposible the use of the algorithm I provided. At first I suggest to use the OutStr buffer for storing HTML code until it is ready, and pass it to the textBrowser in the very last step. This is how you can hightlight everything and then apply replacements to already hightlighted text. I dont think that any highlighting HTML tags could conflict with keywords for GL.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mariusmssj
                    wrote on last edited by
                    #9

                    thank you =] i am going to catch some sleep and try to implement your algorithm first thing in the morning, thank you for all of your help !

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mariusmssj
                      wrote on last edited by
                      #10

                      RaubTieR using your great examples I created something much simpler, now I instead write the code in HTML to the text file and then read from it. And it works like a charm but I got an issue.

                      To make things clear this is how I crate the code
                      @void MainWindow::CreateCode()
                      {
                      QString p = "<p style=' margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;'>"; // begin HTML line
                      QString _p = "</p>"; // end line
                      QString span_template1 = "<span title=";
                      QString span_template2 = ">";
                      QString _span = "</span>";

                      QFile ScaleFile&#40;"./code_output/scale/scale.txt"&#41;;
                      ScaleFile.open(QIODevice::WriteOnly | QIODevice::Truncate&#41;;
                      QTextStream Sout(&ScaleFile&#41;;
                      
                      Sout << p + span_template1 + "'glScale does this'" + span_template2;
                      Sout << "glScalef(" << GLFrame->RenderThread.getxScal();
                      Sout << "f, " << GLFrame->RenderThread.getyScal();
                      Sout << "f, " << GLFrame->RenderThread.getzScal() << "f);";
                      Sout << _span + _p;
                      ScaleFile.close();
                      

                      }@

                      So it creates a HTML line in the the .txt file and then I read from it and it works really well, the reading code:
                      @if(GLFrame->RenderThread.getScale()==1)
                      {
                      QFile Scfile("./code_output/scale/scale.txt");
                      Scfile.open(QIODevice::ReadOnly);
                      QTextStream Scstream(&Scfile);
                      QString scale = Scstream.readAll();
                      Scfile.close();
                      if(scaleChanged) //if the text file has been updated
                      {
                      ui->textBrowser->setTextBackgroundColor(Qt::green);
                      ui->textBrowser->append(scale);
                      ui->textBrowser->setTextBackgroundColor(Qt::white);
                      scaleChanged = false;
                      }
                      else
                      {
                      ui->textBrowser->append(scale);
                      }
                      }@

                      This means that the code:
                      @ if(scaleChanged) //if the text file has been updated
                      {
                      ui->textBrowser->setTextBackgroundColor(Qt::green);
                      ui->textBrowser->append(scale);
                      ui->textBrowser->setTextBackgroundColor(Qt::white);
                      scaleChanged = false;
                      }@

                      Doesn't work any more how would I highlight the line???

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        RaubTieR
                        wrote on last edited by
                        #11

                        By the way, why dont you use the arg() method of QString here:
                        @Sout << p + span_template1 + "'glScale does this'" + span_template2;@

                        Also this syntax may actually be used for more sophisticated approach. For example now you generate the scale.txt file based on current parameters set by user, then you actually read it, highlight and display. But you could have persistent file as a template: you never generate it, you only have to read it and add your parameters where required.
                        ... oh god... why this forum behaves so wierd with symbols that are required in code. I spent lots of time trying to post the code here, but when it came to % symbols, it began to show me things D:
                        Any way here is the simplest project where you can see my implementation "LINK":http://ngageclan.ucoz.ru/test002.rar - I've put it on site where I am admin :)
                        The code is very simple, but remember to copy "scale.txt" file to your EXE directory, right next to compiled app, because it searches for file there.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mariusmssj
                          wrote on last edited by
                          #12

                          Wow I mean just wow that code worked LIKE A DREAM!!!!!!!!!

                          This way I could shorten my code by at least 300 lines. Where did you acquire this kind of knowledge(as in i would love to read books/tutorials to learn more)?

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            RaubTieR
                            wrote on last edited by
                            #13

                            My sources are google and this site's doc section :D and ofcourse my experience. I started Qt (not C++) less than year ago and I was amazed by it. Helping you I discovered few things for myself, since I could only theoretize about such manipulations with QTextBrowser object. Any way i have a good understanding of C++ and OOP, and HTML a bit, which helps.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mariusmssj
                              wrote on last edited by
                              #14

                              Really appreciate your help =] and i envy your knowledge.
                              Also last question if yo don't mind. My other txt files I am still loading in the old way because they are static and never change, but since they have multiple lines of code
                              "i get this weird spacing issue:":http://i.imgur.com/95Qt9.png

                              a bit of the code to show how old and new a laid out:
                              @ if(GLFrame->RenderThread.getColor()==1)
                              {
                              QFile ColourFile(QApplication::applicationDirPath()+"/code_output/colour/colour.txt");
                              ColourFile.open(QIODevice::ReadOnly);
                              QString txt = ColourFile.readAll();
                              ColourFile.close();

                                  QString OutTxt;
                                  QString COLOUR = "FFFFFF";
                              
                                  if(colourChanged) COLOUR = "00FF00";
                              
                                  OutTxt = txt.arg(COLOUR)
                                              .arg(GLFrame->RenderThread.getrCol())
                                              .arg(GLFrame->RenderThread.getgCol())
                                              .arg(GLFrame->RenderThread.getbCol());
                              
                                  ui->textBrowser->append(OutTxt);
                                  colourChanged = false;
                              }
                              
                              
                              QString shapePath(QString("./code_output/shape/shape_") + shapeL + ".txt");
                              
                              QFile Sfile&#40;shapePath&#41;;
                              Sfile.open(QIODevice::ReadOnly);
                              QTextStream stream(&Sfile);
                              QString shape = stream.readAll();
                              Sfile.close();
                              if(shapeChanged)
                              {
                                  ui->textBrowser->setTextBackgroundColor(Qt::green);
                                  ui->textBrowser->append(shape);
                                  ui->textBrowser->setTextBackgroundColor(Qt::white);
                                  shapeChanged = false;
                              }
                              else
                              {
                                  ui->textBrowser->append(shape);
                              }@
                              
                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                RaubTieR
                                wrote on last edited by
                                #15

                                The spacing issue comes from HTML <p> margin style, which if not set, is defaulted to some value. However, I advice you to make all your texts as HTML templates, since they are static, they will only have color template for highlighting. I took my scale.txt file and duplicated it's text twice - no spacing issue with the same executable.

                                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