PDA

View Full Version : How to show table from QTableWidget in txt file?



RamrajCh
16th July 2019, 05:15
Hi,
I am trying to extract all items of my table and stream to txt file.
This is what I have done


QFile file("/home/ramraj/Desktop/bill.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
qDebug()<<"file not open";

QTextStream out(&file);
QString companyName=ui->companyName->text();
QString companyAddress=ui->addressLabel_2->text();
QString companyPhone=ui->phoneLabel_2->text();
QString companyEmail=ui->emailLabel_2->text();
QString companyVAT=ui->vatLabel_2->text();

out <<" "<<companyName<<endl;
out <<" "<<companyAddress<<endl;
out <<" "<<companyPhone<<","<<companyEmail<<endl;
out <<" "<<"VAT No.:"<<companyVAT<<endl;
out <<" "<<"ABBREVIATED TAX INVOICE"<<endl;
out <<endl<<endl;
out <<"Bill # :"<<endl;
out <<"Date :"<<ui->currentDate->text()<<endl;
out<<"Payment Mode: Cash"<<endl;
out <<"-----------------------------------------------------------------------------------------------------------"<<endl;
out <<"ID\t" <<"Particulars\t\t\t"<<"Rate\t\t"<<"Quantity\t"<<"Amount"<<endl;
out <<"-----------------------------------------------------------------------------------------------------------"<<endl;
QString textData;
int rows = ui->billTable->rowCount();
int columns = ui->billTable->columnCount();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {

textData += ui->billTable->item(i,j)->text();
textData += "\t\t";
}
textData += "\n";

}
out<<textData<<endl;
out<<"---------------------------------------------------------------------------------------------------------------"<<endl;
out<<" "<<"Total:"<<ui->subTotal_2->text()<<endl;



But,I am unable to get good alignment.
Here is what I have got:
13191
Please,could anyone solve it?

Lesiok
16th July 2019, 06:42
Use QString::leftJustified method.