tableContent在搜索条件更改时不会更新内容。

huangapple go评论73阅读模式
英文:

tableContent wont update the contents when the search criteria changed

问题

I understand that you want a translation of the code you provided. Here is the translated code:

#include ... //some includes here
ms::ms(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ms)
{
    ui->setupUi(this);
    displayTable("1");

    /********** Populate combobox with data ******/
    QSqlQuery stage;
    if (stage.exec("SELECT IDst FROM stages ORDER BY stage ASC")) {
        while (stage.next()) {
            QString value = stage.value(0).toString();
            bool ok;
            int intValue = value.toInt(&ok);
            //ui->cmbxstage->addItem(QString::number(intValue));
            ui->cmbxstage->insertItem(0, QString::number(intValue));
        }
    }
}

void ms::on_cmbxstage_activated(int)
{
    QString st = ui->cmbxstage->currentText();
    displayTable(st);
}

void ms::displayTable(const QString& st)
{
    SRecord conn;
    conn.connOpen();
    QSqlQuery query;
    query.prepare("SELECT * FROM subj WHERE stg =" + st + " ");

    // Execute the query
    if (!query.exec()) {
        ui->lblmessage->setText("Error"); // Handle the error
    }
    // Fetch the results
    QVector<QString> Ar;
    QVector<QString> ensubj;
    QVector<int> units;
    QVector<QString> stn;
    QVector<int> thr;
    QVector<int> lhr;

    while (query.next()) {
        Ar.append(query.value("subj").toString());
        ensubj.append(query.value("ensubj").toString());
        units.append(query.value("unit").toInt());
        stn.append(query.value("teacher").toString());
        thr.append(query.value("thr").toInt());
        lhr.append(query.value("lhr").toInt());
    }
    qDebug() << "Subj: " << Ar;
    // Create a QString to hold the HTML table content
    QString tableContent;
    tableContent.append("<table align='left' border='1' cellspacing='0' width='350%' cellpadding='0'>");
    // ... Add the rest of the HTML table content (see step 7)

    // Display the table content in a QTextBrowser or QLabel
    QTextBrowser* textBrowser = new QTextBrowser(this);
    textBrowser->setHtml(tableContent);
    textBrowser->setGeometry(10, 10, 1450, 600);
    // Resize the QTextBrowser
    textBrowser->resize(1450, 600);
    QString cssStyle = "<style>" //some stylesheet conf "</style>";
    tableContent.append(cssStyle);

    /**********************************************/

    tableContent.append("<tr>"
        "<th rowspan='6' class='horzontal'>S</th>"
        "<th rowspan='6' class='horzontal'>sid</th>"
        "<th rowspan='6' class='horzontal'>names</th>"
        "<th rowspan='6' class='horzontal'>gender</th>");
    
    for (int s = 0; s < Ar.size(); s++) {
        tableContent.append("<th colspan='2' class='horzontal'>Subject name</th><th colspan='4'>");
        tableContent.append(Ar
展开收缩
);
tableContent.append("</th>"); } tableContent.append("<th rowspan='6' class='vertical'>weights sum</th>"); tableContent.append("<th rowspan='6' class='vertical'>training</th>"); tableContent.append("<th rowspan='6' class='vertical'>year 1 100%</th>"); tableContent.append("<th rowspan='6' class='vertical'>weight y1 40%</th>"); tableContent.append("<th rowspan='6' class='vertical'>year2 100%</th>"); tableContent.append("<th rowspan='6' class='vertical'>weight y2 60%</th>"); tableContent.append("<th rowspan='6' class='vertical'>two years ave 100%</th>"); tableContent.append("<th rowspan='6' class='vertical'>results</th>"); tableContent.append("<th rowspan='6' class='vertical'>notes</th></tr>"); // ... Add the rest of the HTML table content tableContent.append("<tr>"); for (int e = 0; e < Ar.size(); e++) { tableContent.append("<th colspan='2'>other lang Subject name</th><th colspan='4'>"); tableContent.append(ensubj[e]); tableContent.append("</th>"); } tableContent.append("</tr>"); /*************************************/ tableContent.append("<tr>"); for (int e = 0; e < Ar.size(); e++) { tableContent.append("<th colspan='2'>subj unit</th>"); tableContent.append("<th colspan='1'>theoretical=</th>"); tableContent.append(QString::number(thr[e])); tableContent.append("<th colspan='1'>practical=</th>"); tableContent.append(QString::number(lhr[e])); tableContent.append("<th colspan='2'>unit=</th>"); tableContent.append(QString::number(units[e])); tableContent.append("</th>"); } tableContent.append("</tr>"); /******************************************/ tableContent.append("<tr>"); for (int e = 0; e < stn.size(); e++) { tableContent.append("<th colspan='2'>teacher name</th><th colspan='4'>"); tableContent.append(stn[e]); tableContent.append("</th>"); } tableContent.append("</tr>"); /*************************************/ tableContent.append("<tr>"); for (int e = 0; e < stn.size(); e++) { tableContent.append("<th colspan='3'>Final</th><th colspan='2'>Subl</th><th rowspan='2' class='vertical'>weights</th>"); } tableContent.append("</tr>"); /*************************************/ tableContent.append("<tr>"); for (int e = 0; e < stn.size(); e++) { tableContent.append("<th class='vertical'>Quizes ave</th>"); tableContent.append("<th class='vertical'>Final exam</th>"); tableContent.append("<th class='vertical'>Sum</th>"); tableContent.append("<th class='vertical'>Sublemantry</th>"); tableContent.append("<th class='vertical'>Sum</th>"); } tableContent.append("</tr>"); textBrowser->setHtml(tableContent); /*****************************************Adding the marks**********************************/ QString key = ui->txtserchnames->text(); QString st = ui->cmbxstage->currentText(); QSqlQuery ssql; QString nquery = "SELECT * FROM sinfo3 INNER JOIN mark ON sinfo3.sid = mark.sid"; if (!key.isEmpty() && !st.isEmpty()) { nquery += " WHERE mark.stage = '" + st + "' AND sinfo3.Names LIKE '%" + key + "%'"; } else if (!key.isEmpty()) { nquery += " WHERE sinfo3.Names LIKE '%" + key + "%'"; } else if (!st.isEmpty()) { nquery += <details> <summary>英文:</summary> I have a student marksheet software that should populate its content by subject names, teachers, units, exam types and marks for one of the two years from sqlite table. I have used a combobox to change (switch) the data from year one to year two and vise versa. Initialy the table is populate with the data for year one, which is succefully loaded as expected, but one combobox value changed the table stick on the old data and no thing changed, Although the vecters for the subject name ... etc changed. I am using qt C++. Is there any way to make this chaning table value contents according to combobox value change

#include ... //some includes here
ms::ms(QWidget *parent) :
QDialog(parent),
ui(new Ui::ms)
{
ui->setupUi(this);
displayTable("1");

/********** pubulate combobox by data ******/
QSqlQuery stage;
if (stage.exec(&quot;SELECT IDst FROM stages ORDER BY stage ASC&quot;)) {
while (stage.next()) {
QString value = stage.value(0).toString();
bool ok;
int intValue = value.toInt(&amp;ok);
//ui-&gt;cmbxstage-&gt;addItem(QString::number(intValue));
ui-&gt;cmbxstage-&gt;insertItem(0, QString::number(intValue));
}
}

}

void ms::on_cmbxstage_activated(int)
{
QString st=ui->cmbxstage->currentText();
displayTable(st);
}
void ms::displayTable(const QString& st)
{
SRecord conn;
conn.connOpen();
QSqlQuery query;
query.prepare("SELECT * FROM subj WHERE stg ="+st+" ");

// Execute the query
if (!query.exec()) {
ui-&gt;lblmessage-&gt;setText(&quot;Error&quot;);// Handle the error
}
// Fetch the results
QVector&lt;QString&gt; Ar;
QVector&lt;QString&gt; ensubj;
QVector&lt;int&gt; units;
QVector&lt;QString&gt; stn;
QVector&lt;int&gt; thr;
QVector&lt;int&gt; lhr;
while (query.next()) {
Ar.append(query.value(&quot;subj&quot;).toString());
ensubj.append(query.value(&quot;ensubj&quot;).toString());
units.append(query.value(&quot;unit&quot;).toInt());//units.append(query.value(&quot;unit&quot;).toString());
stn.append(query.value(&quot;teacher&quot;).toString());
thr.append(query.value(&quot;thr&quot;).toInt());
lhr.append(query.value(&quot;lhr&quot;).toInt());
}
qDebug()&lt;&lt;&quot;Subj: &quot;&lt;&lt;Ar;
// Create a QString to hold the HTML table content
QString tableContent;
tableContent.append(&quot;&lt;table align=&#39;left&#39; border=&#39;1&#39; cellspacing=&#39;0&#39; width=&#39;350%&#39; cellpadding=&#39;0&#39;&gt;&quot;);
// ... Add the rest of the HTML table content (see step 7)
// Display the table content in a QTextBrowser or QLabel
QTextBrowser* textBrowser = new QTextBrowser(this);
textBrowser-&gt;setHtml(tableContent);
textBrowser-&gt;setGeometry(10, 10, 1450, 600);
// Resize the QTextBrowser
textBrowser-&gt;resize(1450, 600);
QString cssStyle = &quot;&lt;style&gt;&quot; //some stylesheet conf &quot;&lt;/style&gt;&quot;;
tableContent.append(cssStyle);
/**********************************************/
tableContent.append(&quot;&lt;tr&gt;&quot;
&quot;&lt;th rowspan=&#39;6&#39; class=&#39;horzontal&#39;&gt;S&lt;/th&gt;&quot;
&quot;&lt;th rowspan=&#39;6&#39; class=&#39;horzontal&#39;&gt;sid&lt;/th&gt;&quot;
&quot;&lt;th rowspan=&#39;6&#39; class=&#39;horzontal&#39;&gt;names&lt;/th&gt;&quot;
&quot;&lt;th rowspan=&#39;6&#39; class=&#39;horzontal&#39;&gt;gender&lt;/th&gt;&quot;);
for (int s = 0; s &lt; Ar.size(); s++) {
tableContent.append(&quot;&lt;th colspan=&#39;2&#39; class=&#39;horzontal&#39;&gt;Subject name&lt;/th&gt;&lt;th colspan=&#39;4&#39;&gt;&quot;);
tableContent.append(Ar
展开收缩
); tableContent.append(&quot;&lt;/th&gt;&quot;); } tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;weights sum&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;training&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;year 1 100%&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;weight y1 40%&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;year2 100%&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;weight y2 60%&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;two years ave 100%&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;results&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th rowspan=&#39;6&#39; class=&#39;vertical&#39;&gt;notes&lt;/th&gt;&lt;/tr&gt;&quot;); // ... Add the rest of the HTML table content tableContent.append(&quot;&lt;tr&gt;&quot;); for (int e = 0; e &lt; Ar.size(); e++) { tableContent.append(&quot;&lt;th colspan=&#39;2&#39;&gt;other lang Subject name&lt;/th&gt;&lt;th colspan=&#39;4&#39;&gt;&quot;); tableContent.append(ensubj[e]); tableContent.append(&quot;&lt;/th&gt;&quot;); } tableContent.append(&quot;&lt;/tr&gt;&quot;); /*************************************/ tableContent.append(&quot;&lt;tr&gt;&quot;); for (int e = 0; e &lt; Ar.size(); e++) { tableContent.append(&quot;&lt;th colspan=&#39;2&#39;&gt;subj unit&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th colspan=&#39;1&#39;&gt;theoretical=&lt;/th&gt;&quot;); tableContent.append(QString::number(thr[e])); tableContent.append(&quot;&lt;th colspan=&#39;1&#39;&gt;practical=&lt;/th&gt;&quot;); tableContent.append(QString::number(lhr[e])); tableContent.append(&quot;&lt;th colspan=&#39;2&#39;&gt;unit=&lt;/th&gt;&quot;); tableContent.append(QString::number(units[e])); tableContent.append(&quot;&lt;/th&gt;&quot;); } tableContent.append(&quot;&lt;/tr&gt;&quot;); /******************************************/ tableContent.append(&quot;&lt;tr&gt;&quot;); for (int e = 0; e &lt; stn.size(); e++) { tableContent.append(&quot;&lt;th colspan=&#39;2&#39;&gt;teacher name&lt;/th&gt;&lt;th colspan=&#39;4&#39;&gt;&quot;); tableContent.append(stn[e]); tableContent.append(&quot;&lt;/th&gt;&quot;); } tableContent.append(&quot;&lt;/tr&gt;&quot;); // textBrowser-&gt;setHtml(tableContent); /*************************************/ tableContent.append(&quot;&lt;tr&gt;&quot;); for (int e = 0; e &lt; stn.size(); e++) { tableContent.append(&quot; &lt;th colspan=&#39;3&#39;&gt;Final&lt;/th&gt;&lt;th colspan=&#39;2&#39;&gt;Subl&lt;/th&gt;&lt;th rowspan=&#39;2&#39; class=&#39;vertical&#39;&gt;weights&lt;/th&gt;&quot;); } tableContent.append(&quot;&lt;/tr&gt;&quot;); /*************************************/ tableContent.append(&quot;&lt;tr&gt;&quot;); for (int e = 0; e &lt; stn.size(); e++) { tableContent.append(&quot;&lt;th class=&#39;vertical&#39;&gt;Quizes ave&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th class=&#39;vertical&#39;&gt;Final exam&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th class=&#39;vertical&#39;&gt;Sum&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th class=&#39;vertical&#39;&gt;Sublemantry&lt;/th&gt;&quot;); tableContent.append(&quot;&lt;th class=&#39;vertical&#39;&gt;Sum&lt;/th&gt;&quot;); } tableContent.append(&quot;&lt;/tr&gt;&quot;); textBrowser-&gt;setHtml(tableContent); /*****************************************Adding the marks**********************************/ QString key = ui-&gt;txtserchnames-&gt;text(); QString st = ui-&gt;cmbxstage-&gt;currentText(); QSqlQuery ssql; QString nquery = &quot;SELECT * FROM sinfo3 INNER JOIN mark ON sinfo3.sid = mark.sid&quot;; if (!key.isEmpty() &amp;&amp; !st.isEmpty()) { nquery += &quot; WHERE mark.stage = &#39;&quot; + st + &quot;&#39; AND sinfo3.Names LIKE &#39;%&quot; + key + &quot;%&#39;&quot;; } else if (!key.isEmpty()) { nquery += &quot; WHERE sinfo3.Names LIKE &#39;%&quot; + key + &quot;%&#39;&quot;; } else if (!st.isEmpty()) { nquery += &quot; WHERE mark.stage = &#39;&quot; + st + &quot;&#39;&quot;; } nquery += &quot; GROUP BY Names ORDER BY sinfo3.sid&quot;; if (key.isEmpty() &amp;&amp; stg.isEmpty()) { nquery = &quot;SELECT * FROM sinfo3 INNER JOIN mark ON sinfo3.sid = mark.sid GROUP BY Names ORDER BY sinfo3.Names ASC&quot;; } ssql.exec(nquery); int i = 0; while (ssql.next()) { QString snm = ssql.value(&quot;Names&quot;).toString(); QString gen = ssql.value(&quot;Gender&quot;).toString(); QString sid = ssql.value(&quot;sid&quot;).toString(); QString strain= ssql.value(&quot;entership&quot;).toString(); i++; QString color = (i % 3 == 1) ? &quot;yellow&quot; : &quot;white&quot;; QString row = &quot;&lt;tr style=&#39;background-color:&quot; + color + &quot;; text-align:center; vertical-align:middle;&#39;&gt;&lt;td&gt; &quot; + QString::number(i) + &quot; &lt;/td&gt;&lt;td&gt;&quot; + sid + &quot;&lt;/td&gt;&lt;td&gt;&quot; + snm + &quot;&lt;/td&gt;&lt;td&gt;&quot; + gen + &quot;&lt;/td&gt;&quot;; QList&lt;int&gt; mtypes = { 1, 2, 3, 4, 5, 6 }; QList&lt;int&gt; subj; for (int s = 1; s &lt;= Ar.size(); s++) { subj.append(s); } float av = 0; float m2 = 0; float m4 = 0; float sumM6 =0; float tunit = 0.000; // ... foreach (int s, subj) { foreach (int m, mtypes) { QSqlQuery msql; QString markQuery = &quot;SELECT * FROM mark WHERE sid = &quot; + sid + &quot; AND exam = &quot; + QString::number(m) + &quot; AND subj = &quot; + QString::number(s) + &quot; &quot;; msql.exec(markQuery); if (msql.next()) { int examtype = msql.value(&quot;exam&quot;).toInt(); float mrk = msql.value(&quot;mark&quot;).toFloat(); //float f = mrk + mrk0; if (m == 1 &amp;&amp; examtype == 1) { row += &quot;&lt;td style=&#39;background-color:lightgray&#39;&gt;&quot; + QString::number(mrk) + &quot;&lt;/td&gt;&quot;; } else if (m == 2) { m2 = mrk; row += &quot;&lt;td style=&#39;background-color:&#39;&gt;&quot; + QString::number(mrk) + &quot;&lt;/td&gt;&quot;; } else if (m == 4 &amp;&amp; examtype == 4) { m4 = mrk; row += &quot;&lt;td style=&#39;background-color:&#39;&gt;&quot; + QString::number(mrk) + &quot;&lt;/td&gt;&quot;; } else { row += &quot;&lt;td style=&#39;background-color: &#39;&gt;&quot;&quot;&lt;/td&gt;&quot;; } } else { float m1 = 0; // Get m1 mark QSqlQuery m1sql; m1sql.exec(&quot;SELECT mark FROM mark WHERE sid = &quot; + sid + &quot; AND exam = 1 AND subj = &quot; + QString::number(s)); if (m1sql.next()) { m1 = m1sql.value(&quot;mark&quot;).toFloat(); } if (m == 3 &amp;&amp; m1!=NULL) { float m3 = m1 + m2; row += &quot;&lt;td style=&#39;color:red;&#39;&gt;&quot; + QString::number(m3) + &quot;&lt;/td&gt;&quot;; } else if (m == 5 &amp;&amp; m1!=NULL) { float m5 = m1 + m4; row += &quot;&lt;td style=&#39;background-color:&#39;&gt;&quot; + QString::number(m5) + &quot;&lt;/td&gt;&quot;; } else if (m == 6 &amp;&amp; ((m1+m2)&gt;=50||(m1+m4)&gt;=50)) { if((m1+m2)&gt;=50){ float m6 = (m1 + m2) * units
展开收缩
; row += &quot;&lt;td style=&#39;background-color:&#39;&gt;&quot; + QString::number(m6) + &quot;&lt;/td&gt;&quot;; sumM6 += m6; // Add m6 to the sum tunit += units
展开收缩
; } else if((m1+m4)&gt;=50){ float m6 = (m1 + m4) * units
展开收缩
; row += &quot;&lt;td style=&#39;background-color:&#39;&gt;&quot; + QString::number(m6) + &quot;&lt;/td&gt;&quot;; sumM6 += m6; // Add m6 to the sum tunit += units
展开收缩
; } } else { row += &quot;&lt;td style=&#39;background-color: &#39;&gt;&quot;&quot;&lt;/td&gt;&quot;; } } } } float wave = sumM6/tunit; // ... row += &quot;&lt;td&gt;&quot; + QString::number(sumM6) + &quot;&lt;/td&gt;&lt;td&gt;&quot; + strain + &quot;&lt;/td&gt;&lt;td&gt;&quot; + QString::number(wave,&#39;f&#39;,4) + &quot;&lt;/td&gt;&lt;td&gt;&quot;+QString::number(wave*0.4)+&quot;&lt;/td&gt;&lt;td&gt;&quot; + QString::number(av) + &quot;&lt;/td&gt;&lt;td&gt;&quot; + QString::number(av * 0.6) + &quot;&lt;/td&gt;&lt;td&gt;Two years Ave&lt;/td&gt;&lt;td&gt;Result&lt;/td&gt;&lt;td&gt;Notes&lt;/td&gt;&quot;; row += &quot;&lt;/tr&gt;&quot;; tableContent.append(&quot;&lt;/tr&gt;&quot;); tableContent.append(row); // Append the row to the tableContent } textBrowser-&gt;setHtml(tableContent); tableContent.append(&quot;&lt;/table&gt;&quot;); textBrowser-&gt;setHtml(tableContent);

}


I have search the internet for a solution but nothing. Also I have tried to replace the combobox by QlineEdit, or Qspinbox, but no difference.
</details>
# 答案1
**得分**: -1
I found the solution by Update the tableContent variable correctly: Instead of creating a new QTextBrowser instance every time, I have updated the tableContent variable directly. Remove the lines where I created a new `QTextBrowser` and set its HTML content. Instead, I used `ui->textBrowser->setHtml(tableContent);` to set the HTML content of the existing `QTextBrowser` widget. using the following steps:
1. Open the form file (usually with a .ui extension) in Qt Designer or Qt Creator.
2. Identify the widget (such as a `QTextEdit`, `QLabel`, or `QPlainTextEdit`) that should be used to display the HTML table.
3. Make sure the widget has a name set in the properties (e.g., `textBrowser`).
Regenerate the corresponding `ui_ms.h` file (if needed) by running uic or rebuilding the project.
4. In the code, locate the correct widget object based on the name you assigned in step 3.
5. Use the `setHtml()` function to update the HTML content of the widget
with the table content.
<details>
<summary>英文:</summary>
I found the solution by Update the tableContent variable correctly: Instead of creating a new QTextBrowser instance every time, I have updated the tableContent variable directly. Remove the lines where I created a new `QTextBrowser` and set its HTML content. Instead, I used `ui-&gt;textBrowser-&gt;setHtml(tableContent);` to set the HTML content of the existing `QTextBrowser` widget. using the following steps:
1. Open the form file (usually with a .ui extension) in Qt Designer or Qt Creator.
2. Identify the widget (such as a `QTextEdit`, `QLabel`, or `QPlainTextEdit`) that should be used to display the HTML table.
3. Make sure the widget has a name set in the properties (e.g., `textBrowser`).
Regenerate the corresponding `ui_ms.h` file (if needed) by running uic or rebuilding the project.
4. In thecode, locate the correct widget object based on the name you assigned in step 3.
5. Use the `setHtml()` function to update the HTML content of the widget
with the table content.
</details>

huangapple
  • 本文由 发表于 2023年6月8日 23:47:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定