PDA

View Full Version : QProgressBar: don't understand in details, help me !!!



haconganh
9th May 2007, 03:13
Hi all,

I begin to write a simple application that load data from database and I want to use QProgressBar but I don't know exactly how it works, pls show it for me in details.

Thanks in advance.

marcel
9th May 2007, 05:25
It is very easy to use one.
Create it,put it in a layout, set it's max and min value, and then call setValue to update it.
You also can look at QProgressDialog beacuse it uses a QProgressBar.

Regards

haconganh
10th May 2007, 05:15
In my program, I want to connect to database that verify user input, please tell me how to make progressbar describe this progress.

Beforehands thanks.

Anh.:)

VireX
10th May 2007, 05:32
Everything you need to know is simple:

1) Create QProgressBar
2) setMin setMAx
3) setValue according to some sort of SIGNAL, like if database connected maybe connect a function to add like 20 more % to progressbar
Take a look at examples in your Qt folder, like HTTP or FTP example.

patrik08
10th May 2007, 07:14
In my program, I want to connect to database that verify user input, please tell me how to make progressbar describe this progress.

Beforehands thanks.

Anh.:)

QProgressDialog *dlg = new QProgressDialog(0,Qt::Popup);

here is a sample wo user can not stop dlg->setCancelButton(0); && query... result is a dump file



void ExportSql()
{
if (OSWinServer()) {
table = table.toUpper(); /* bastard win ! */
}

QDate nowtaga = QDate::currentDate();
bool ok;
QString ba = nowtaga.toString("d.M.yyyy")+"_"+table+".sql";

QString cfileName = QFileDialog::getSaveFileName(this, "Export MYSQL table format",ba, "*.sql");
if (cfileName.size() > 0 && cfileName !=ba ) {
if (!cfileName.endsWith(".sql")) {
cfileName = cfileName+".sql";
}
} else {
return;
}

QProgressDialog *dlg = new QProgressDialog(0,Qt::Popup);
dlg->setLabelText (tr("Export table \"%1\" from %2").arg( table ).arg(db.hostName()));
dlg->setCancelButton(0);
//////dlg->setMaximum(100);
//////dlg->setValue(0);



qDebug() << "### ExportSql " << table;
QDate nowtag = QDate::currentDate();
QSqlQuery query(QString("SELECT * FROM %1").arg(table),db);
QSqlRecord rec = query.record();
QStringList cvslines;
QStringList Fname;
QStringList Fvalue;
cvslines.clear();
cvslines.append(QString("\n###### table %1\n\n").arg(table));
cvslines.append(QString("\n###### date %1\n\n").arg(nowtag.toString("d.M.yyyy")));

bool suppoaffect;
int sumcol = rec.count();
int sumrow = query.numRowsAffected();
int recnum = 0; /* QProgressDialog *dlg; */
int pasero = 0;


while (query.next()) {
recnum++; /* count fake lines */
dlg->setMaximum(100);
dlg->setValue((recnum * 100) / sumrow );
Fvalue.clear();
Fname.clear();

for(int i=0;i<sumcol;i++){
bool oknr = false;
QSqlField fld = rec.field(i);
QString value = sql_Quote(query.value(i).toString());
value = value.replace("\n","\\n");
value = value.replace("\t","\\t");
value = value.replace("\r","\\r");
const QString typeoffield = QString(QVariant::typeToName(fld.type()));
if ( typeoffield == "int" ) {
int vnummer = value.toInt(&oknr);
}
QString fname = sql_Quote(rec.fieldName(i));
if (oknr) {
Fvalue.append(value);
} else if (value.isEmpty()) {
Fvalue.append(DefaultFieldFrom(table,fname));
} else {
Fvalue.append(QString("'%1'").arg(value));
}
}

QString Dvalue = Fvalue.join(",");
cvslines.append(QString("REPLACE INTO %1 VALUES (%2);\n").arg(table).arg(Dvalue));
}
dlg->close();

cvslines.append(QString("\n"));


QString stream = SqlCreateTable();
stream.append(cvslines.join(""));

/////////qDebug() << "### stream " << stream;
QFile f( cfileName );
if ( f.open( QFile::WriteOnly | QFile::Text ) )
{
QTextStream sw( &f );
sw << stream;
f.close();
} else {
QMessageBox::warning(this, tr("Error SQL File!"),tr("Unable to write on this file ..."));
}

}


QString DefaultFieldFrom( const QString table , const QString field )
{
const QString xdefault = "NULL";
QSqlQuery query(QString("SHOW COLUMNS FROM %1").arg(table),db_0);
QSqlRecord rec = query.record();
int sumcol = rec.count();
while (query.next()) {
QString feldname = query.value(0).toString();
if ( feldname == field ) {
QString svl = query.value(4).toString();
if (svl.size() < 1) {
return xdefault;
} else {
return svl;
}

}

}

return xdefault;
}

haconganh
10th May 2007, 08:07
Hi patrik08,

With your code, I understand it exactly now,
thank patrik08 a lot :)