我有一个学生标记表软件,应该填充其内容的科目名称,教师,单位,考试类型和分数为两年之一从sqlite表。我已经使用一个combobox改变(切换)数据从第一年到第二年,反之亦然。最初,表填充了第一年的数据,这是成功加载的预期,但一个combobox值改变了表粘在旧的数据和没有任何改变,虽然主题名称的向量...等改变。我使用的是qt C++。有没有办法使这个变化表的值内容根据combobox值的变化
#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[s]);
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[s - 1];
row += "<td style='background-color:'>" + QString::number(m6) + "</td>";
sumM6 += m6; // Add m6 to the sum
tunit += units[s - 1];
}
else if((m1+m4)>=50){
float m6 = (m1 + m4) * units[s - 1];
row += "<td style='background-color:'>" + QString::number(m6) + "</td>";
sumM6 += m6; // Add m6 to the sum
tunit += units[s - 1];
}
}
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);
}
我在互联网上搜索了一个解决方案,但一无所获。此外,我也试过用QlineEdit或QSpinbox取代Combobox,但没有区别。
1条答案
按热度按时间li9yvcax1#
我通过正确更新tableContent变量找到了解决方案:我没有每次都创建一个新的QTextBrowser示例,而是直接更新了tableContent变量。删除创建新
QTextBrowser
的行并设置其HTML内容。相反,我使用ui->textBrowser->setHtml(tableContent);
来设置现有QTextBrowser
小部件的HTML内容。使用以下步骤:1.在Qt Designer或Qt Creator中打开表单文件(通常扩展名为.ui)。
1.确定应用于显示HTML表的小部件(例如
QTextEdit
、QLabel
或QPlainTextEdit
)。1.确保小部件在属性中设置了名称(例如,
textBrowser
)。通过运行uic或重新生成项目,重新生成相应的ui_ms.h
文件(如果需要)。1.在代码中,根据您在步骤3中指定的名称找到正确的小部件对象。
1.使用
setHtml()
函数用表格内容更新小部件的HTML内容。