英文:
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("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());//units.append(query.value("unit").toString());
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>"); // textBrowser->setHtml(tableContent);
/*************************************/
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 += " WHERE mark.stage = '" + st + "'";
}
nquery += " GROUP BY Names ORDER BY sinfo3.sid";
if (key.isEmpty() && stg.isEmpty()) {
nquery = "SELECT * FROM sinfo3 INNER JOIN mark ON sinfo3.sid = mark.sid GROUP BY Names ORDER BY sinfo3.Names ASC";
}
ssql.exec(nquery);
int i = 0;
while (ssql.next()) {
QString snm = ssql.value("Names").toString();
QString gen = ssql.value("Gender").toString();
QString sid = ssql.value("sid").toString();
QString strain= ssql.value("entership").toString();
i++;
QString color = (i % 3 == 1) ? "yellow" : "white";
QString row = "<tr style='background-color:" + color + "; text-align:center; vertical-align:middle;'><td> " + QString::number(i) + " </td><td>" + sid + "</td><td>" + snm + "</td><td>" + gen + "</td>";
QList<int> mtypes = { 1, 2, 3, 4, 5, 6 };
QList<int> subj;
for (int s = 1; s <= 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 = "SELECT * FROM mark WHERE sid = " + sid + " AND exam = " + QString::number(m) + " AND subj = " + QString::number(s) + " ";
msql.exec(markQuery);
if (msql.next()) {
int examtype = msql.value("exam").toInt();
float mrk = msql.value("mark").toFloat();
//float f = mrk + mrk0;
if (m == 1 && examtype == 1) {
row += "<td style='background-color:lightgray'>" + QString::number(mrk) + "</td>";
}
else if (m == 2) {
m2 = mrk;
row += "<td style='background-color:'>" + QString::number(mrk) + "</td>";
}
else if (m == 4 && examtype == 4) {
m4 = mrk;
row += "<td style='background-color:'>" + QString::number(mrk) + "</td>";
}
else {
row += "<td style='background-color: '>""</td>";
}
}
else {
float m1 = 0;
// Get m1 mark
QSqlQuery m1sql;
m1sql.exec("SELECT mark FROM mark WHERE sid = " + sid + " AND exam = 1 AND subj = " + QString::number(s));
if (m1sql.next()) {
m1 = m1sql.value("mark").toFloat();
}
if (m == 3 && m1!=NULL) {
float m3 = m1 + m2;
row += "<td style='color:red;'>" + QString::number(m3) + "</td>";
}
else if (m == 5 && m1!=NULL) {
float m5 = m1 + m4;
row += "<td style='background-color:'>" + QString::number(m5) + "</td>";
}
else if (m == 6 && ((m1+m2)>=50||(m1+m4)>=50)) {
if((m1+m2)>=50){
float m6 = (m1 + m2) * units展开收缩;
row += "<td style='background-color:'>" + QString::number(m6) + "</td>";
sumM6 += m6; // Add m6 to the sum
tunit += units展开收缩;
}
else if((m1+m4)>=50){
float m6 = (m1 + m4) * units展开收缩;
row += "<td style='background-color:'>" + QString::number(m6) + "</td>";
sumM6 += m6; // Add m6 to the sum
tunit += units展开收缩;
}
}
else {
row += "<td style='background-color: '>""</td>";
}
}
}
}
float wave = sumM6/tunit;
// ...
row += "<td>" + QString::number(sumM6) + "</td><td>" + strain + "</td><td>" + QString::number(wave,'f',4) + "</td><td>"+QString::number(wave*0.4)+"</td><td>" + QString::number(av) + "</td><td>" + QString::number(av * 0.6) + "</td><td>Two years Ave</td><td>Result</td><td>Notes</td>";
row += "</tr>";
tableContent.append("</tr>");
tableContent.append(row); // Append the row to the tableContent
}
textBrowser->setHtml(tableContent);
tableContent.append("</table>");
textBrowser->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->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 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论