Removing blank lines after filling



  • Good afternoon.
    I need to delete the empty lines after filling in the html form. How to do this?
    Now it looks like this
    0_1514273690954_Снимок1.PNG

        if (ui->create_analise->isChecked())
        {
            QFile file("ReportTemplate.html");
            if(!file.open(QIODevice::ReadOnly))
                return;
            QString htmlReport =  file.readAll();
    
            htmlReport =
                    htmlReport
                    .arg(ui->lineEditNumber->text())
                    .arg(ui->lineEditName->text())
    
                    .arg(ro_oil)
                    .arg(ro_water)
                    .arg(ro_gas)
    
                    .arg(Q_oil)
                    .arg(Q_water);
    
    
            for(int i=0;i<size; ++i)
                htmlReport = htmlReport.arg(p_overpressure[i]);
    
            htmlReport=
                    htmlReport
                    .arg(ui->doubleSpinBox_G->value())
                    .arg(ui->doubleSpinBox_T->value())
                    .arg(ui->doubleSpinBox_mu->value())
                    .arg(mu);
    
            htmlReport= htmlReport.arg(Q_liquid,0,'f',2);
    
            for(int i=0;i<size; ++i)
                htmlReport = htmlReport.arg(ro_liquid_gas[i],0,'f',2);
    
            htmlReport= htmlReport.arg(p_saturation,0,'f',2);
    
            for(int i=0;i<size; ++i)
                htmlReport = htmlReport.arg(V_gas[i],0,'f',2);
    
            for(int i=0;i<size; ++i)
                htmlReport = htmlReport.arg(Q_mix[i],0,'f',2);
    
            for(int i=0;i<size; ++i)
                htmlReport = htmlReport.arg(alpha[i]*100,0,'f',2);
    
            QFile file2("MeansTemplate.html");
            if(!file2.open(QIODevice::ReadOnly))
            {
              return;
            }
            QString htmlMeans =  file2.readAll();
    
            for (int c=0; c<size; ++c)
              for (int r=0; r<20; ++r)
              {
                if(r < means[c].size())
                  htmlMeans = htmlMeans.arg(means[c][r]);
              //  else
              //   htmlMeans = htmlMeans.arg("");
               //    htmlMeans.remove(QRegularExpression("<td class=\"Means\"></td>"));
              }
    
            m_WebEngineView->setHtml(htmlReport.arg(htmlMeans));
            ui->tabWidget->setCurrentWidget(ui->tabReport);
        }
    

    HTML form

              <td style="border: 0px;"><b><u>Рекомендуемые СИ</u></b></td>
              <td class="Means">%0</td>
              <td class="Means">%20</td>
              <td class="Means">%40</td>
              <td class="Means">%60</td>
              <td class="Means">%80</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%1</td>
              <td class="Means">%21</td>
              <td class="Means">%41</td>
              <td class="Means">%61</td>
              <td class="Means">%81</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%2</td>
              <td class="Means">%22</td>
              <td class="Means">%42</td>
              <td class="Means">%62</td>
              <td class="Means">%82</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%3</td>
              <td class="Means">%23</td>
              <td class="Means">%43</td>
              <td class="Means">%63</td>
              <td class="Means">%83</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%4</td>
              <td class="Means">%24</td>
              <td class="Means">%44</td>
              <td class="Means">%64</td>
              <td class="Means">%84</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%5</td>
              <td class="Means">%25</td>
              <td class="Means">%45</td>
              <td class="Means">%65</td>
              <td class="Means">%85</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%6</td>
              <td class="Means">%26</td>
              <td class="Means">%46</td>
              <td class="Means">%66</td>
              <td class="Means">%86</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%7</td>
              <td class="Means">%27</td>
              <td class="Means">%47</td>
              <td class="Means">%67</td>
              <td class="Means">%87</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%8</td>
              <td class="Means">%28</td>
              <td class="Means">%48</td>
              <td class="Means">%68</td>
              <td class="Means">%88</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%9</td>
              <td class="Means">%29</td>
              <td class="Means">%49</td>
              <td class="Means">%69</td>
              <td class="Means">%89</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%10</td>
              <td class="Means">%30</td>
              <td class="Means">%50</td>
              <td class="Means">%70</td>
              <td class="Means">%90</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%11</td>
              <td class="Means">%31</td>
              <td class="Means">%51</td>
              <td class="Means">%71</td>
              <td class="Means">%91</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%12</td>
              <td class="Means">%32</td>
              <td class="Means">%52</td>
              <td class="Means">%72</td>
              <td class="Means">%92</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%13</td>
              <td class="Means">%33</td>
              <td class="Means">%53</td>
              <td class="Means">%73</td>
              <td class="Means">%93</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%14</td>
              <td class="Means">%34</td>
              <td class="Means">%54</td>
              <td class="Means">%74</td>
              <td class="Means">%94</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%15</td>
              <td class="Means">%35</td>
              <td class="Means">%55</td>
              <td class="Means">%75</td>
              <td class="Means">%95</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%16</td>
              <td class="Means">%36</td>
              <td class="Means">%56</td>
              <td class="Means">%76</td>
              <td class="Means">%96</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%17</td>
              <td class="Means">%37</td>
              <td class="Means">%57</td>
              <td class="Means">%77</td>
              <td class="Means">%97</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%18</td>
              <td class="Means">%38</td>
              <td class="Means">%58</td>
              <td class="Means">%78</td>
              <td class="Means">%98</td>
          </tr>
    
          <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%19</td>
              <td class="Means">%39</td>
              <td class="Means">%59</td>
              <td class="Means">%79</td>
              <td class="Means">%99</td>
          </tr>
    

    [edit: koahnig, code tags corrected]


  • Qt Champions 2017

    Hi and welcome to the forums.

    Its a bit unclear how empty lines look vs the ones you want to keep.
    its rest of ? (from some point in file and down ?)

     <tr>
              <td style="border: 0px;"></td>
              <td class="Means">%10</td>
              <td class="Means">%30</td>
              <td class="Means">%50</td>
              <td class="Means">%70</td>
              <td class="Means">%90</td>
    </tr>
    
    

    I assume you must load a html file to use so its not an options just to generate HTML so
    there are not empty lines?



  • @mrjj
    The lines are filled, but remain empty on the form, as shown in the picture. In the future, all this in the PDP is preserved and it does not look quite right

    I was offered to delete this way

    htmlMeans.remove(QRegExp("<td class=\"name\"></td>"));
    //your code here
    

    0_1514352932072_report3.PNG


  • Qt Champions 2017

    @fender said in Removing blank lines after filling:

    QRegExp

    So did it find and remove the ones you wanted?



  • @mrjj said in Removing blank lines after filling:

    @fender said in Removing blank lines after filling:

    QRegExp

    So did it find and remove the ones you wanted?
    No.
    Filled table is moving away0_1514382333608_report2.PNG


  • Qt Champions 2017

    Ok, so you need better regular expression.
    Alternative, instead of read all.
    Read line by line and replace and save to new file.
    When you run out of data , simply stop replacing.



  • @mrjj said in Removing blank lines after filling:

    Ok, so you need better regular expression.
    Alternative, instead of read all.
    Read line by line and replace and save to new file.
    When you run out of data , simply stop replacing.
    Can u show example for me?
    thank u



  • @fender said in Removing blank lines after filling:

    @mrjj said in Removing blank lines after filling:

    Ok, so you need better regular expression.

    Can u show example for me?

    First off, I'd consider making MeansTemplate.html only contain one single template row, and then copy it as many times as you need. But as you seem to be doing an X-Y pivot, that's probably not possible for you?

    Here's an update to your example, but using a regular expression to remove entirely empty rows at the end.

            QString htmlMeans =  file2.readAll();
    
            for (int c=0; c<size; ++c)
              for (int r=0; r<20; ++r)
              {
                if(r < means[c].size())
                  htmlMeans = htmlMeans.arg(means[c][r]);
                else
                 htmlMeans = htmlMeans.arg("");
              }
    
            // Replace all HTML rows that contain nothing but empty (or full of whitespace) cells.
            htmlMeans.replace(QRegularExpression("<tr(\\s[^>]*)?>(\\s*<td(\\s[^>]*)?>\\s*</td>\\s*)*</tr>", QRegularExpression::MultilineOption), "");
    
            m_WebEngineView->setHtml(htmlReport.arg(htmlMeans));
            ui->tabWidget->setCurrentWidget(ui->tabReport);
    

    All I've done is uncomment your else htmlMeans = htmlMeans.arg(""); codes, and added the big htmlMeans.replace line.

    To show the regex in action:

        QString htmlMeans =
    "        <tr>\n"
    "            <td style=\"border: 0px;\"></td>\n"
    "            <td class=\"Means\">%2</td>\n"
    "            <td class=\"Means\">%22</td>\n"
    "            <td class=\"Means\">%42</td>\n"
    "            <td class=\"Means\">%62</td>\n"
    "            <td class=\"Means\">%82</td>\n"
    "        </tr>\n"
    "        <tr>\n"
    "            <td style=\"border: 0px;\"></td>\n"
    "            <td class=\"Means\">%2</td>\n"
    "            <td class=\"Means\">%23</td>\n"
    "            <td class=\"Means\">%43</td>\n"
    "            <td class=\"Means\">%63</td>\n"
    "            <td class=\"Means\">%83</td>\n"
    "        </tr>\n"
    "        <tr>\n"
    "            <td style=\"border: 0px;\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "        </tr>\n"
    "        <tr>\n"
    "            <td style=\"border: 0px;\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "            <td class=\"Means\"></td>\n"
    "        </tr>\n";
    
        std::cout << "before:\n" << htmlMeans.toStdString();
    
        htmlMeans.replace(QRegularExpression("<tr(\\s[^>]*)?>(\\s*<td(\\s[^>]*)?>\\s*</td>\\s*)*</tr>", QRegularExpression::MultilineOption), "");
    
        std::cout << "after:\n" << htmlMeans.toStdString();
    

    Output:

    before:
            <tr>
                <td style="border: 0px;"></td>
                <td class="Means">%2</td>
                <td class="Means">%22</td>
                <td class="Means">%42</td>
                <td class="Means">%62</td>
                <td class="Means">%82</td>
            </tr>
            <tr>
                <td style="border: 0px;"></td>
                <td class="Means">%2</td>
                <td class="Means">%23</td>
                <td class="Means">%43</td>
                <td class="Means">%63</td>
                <td class="Means">%83</td>
            </tr>
            <tr>
                <td style="border: 0px;"></td>
                <td class="Means"></td>
                <td class="Means"></td>
                <td class="Means"></td>
                <td class="Means"></td>
                <td class="Means"></td>
            </tr>
            <tr>
                <td style="border: 0px;"></td>
                <td class="Means"></td>
                <td class="Means"></td>
                <td class="Means"></td>
                <td class="Means"></td>
                <td class="Means"></td>
            </tr>
    after:
            <tr>
                <td style="border: 0px;"></td>
                <td class="Means">%2</td>
                <td class="Means">%22</td>
                <td class="Means">%42</td>
                <td class="Means">%62</td>
                <td class="Means">%82</td>
            </tr>
            <tr>
                <td style="border: 0px;"></td>
                <td class="Means">%2</td>
                <td class="Means">%23</td>
                <td class="Means">%43</td>
                <td class="Means">%63</td>
                <td class="Means">%83</td>
            </tr>
    

    The regular expression I've used is more lenient than you need, but should cover just about any valid HTML table rows. For a visual representation, have a look at:

    https://jex.im/regulex/#!flags=&re=%3Ctr(%5Cs%5B%5E%3E%5D*)%3F%3E(%5Cs*%3Ctd(%5Cs%5B%5E%3E%5D*)%3F%3E%5Cs*%3C%2Ftd%3E%5Cs*)*%3C%2Ftr%3E
    

    (you'll have to cut-and-paste into your browser, because Markdown does not cope with URLs containing ( characters)

    Cheers.



  • @Paul-Colby

    This is the first version of the program(i'm junior dev). In the future, will be optimized as I study Qt.
    Thank you very much for the solution.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.