I am self-learner and new to qt(I just want to learn to write program).
Trying to load the csv file to qtablewidget but it overriding all columns and row with same data. I tried to follow How to import a CSV file to a QTableWidget but i did not get it correctly.
Code:
QFile file("testData.txt");
QTextStream in(&file);
QStringList setDataInrow;
QStringList rowNumbers;
QString allLine;
if(file.open(QIODevice::ReadOnly)){
allLine=in.readAll();
rowNumbers=allLine.split("\n");
file.close();
}
QTableWidget *myTable=new QTableWidget();
myTable->setRowCount(rowNumbers.size());
for(int row=0;row<rowNumbers.count();row++)
{
setDataInrow=allLine.split(";");
for(int column=0;column<setDataInrow.count();column++){
myTable->setColumnCount(setDataInrow.size());
//myTable->item(row,column)->setText(setDataInrow[column]);
QTableWidgetItem *item=new QTableWidgetItem(setDataInrow[column]);
myTable->setItem(row,column,item);
}
}
qDebug()<<"Numbers of row needed:"<<"\n"<<rowNumbers<<"\n";
qDebug()<<"Set following data to each column as per row:"<<"\n"<<setDataInrow<<setDataInrow.size();
window->setCentralWidget(myTable);
window->show();
return app.exec();
}
i am trying to load :
John Kl;34;1335532;CA;0444344
Kuma jo;54;44432;NY;0322355
Lebal ho;24;44022;NY;0110004
It should be loaded within 3 rows and 5 columns but it is setting 13 columns . Perhaps i am not able think this correctly. I need your help with some code example so that i can study about it more.
Since my English is not good(Sucks like my codes :)) I have taken an screenshot of the current program which is not working as expected:
http://imageshack.us/a/img801/1601/le59.png
setDataInrow=allLine.split(";");
You do this once for each line. But you don't split the line, as you intended, but each time the whole file content.
Edit:
Don't split allLine. You have already a QStringList, which contains your lines: rowNumbers.
This you must split.
for(int row=0;row<rowNumbers.count();row++){
QStringList rowCells = rowNumbers.at(row).split(";");
.....
}
I hope this gives you the idea. No guarantee for details.
Edit 2:
When you do
setDataInrow=allLine.split(";");
your stringlist contains:
John Kl 34 1335532 CA 0444344 Kuma jo 54 44432 NY 0322355 Lebal ho 24 44022 NY 0110004.
This is the whole text in your file. The line breaks do not matter. They are just another character. You add all this in one row. And you do it three times.
What you want is first split the text into lines. You already do this here: rowNumbers=allLine.split("\n");
With your example data the rowNumbers stringlist contains three entries. Each entry one line from your file. These lines you have to split.
Related
I have a QTableWidget and an object of class product called p.
I wanna add items to this table.
I tried the code below but no use.
void MainWindow:: add_to_basket (product p){
ui->tableWidget->insertRow(1);
QLineEdit *qle=new QLineEdit();
qle->setText(p.get_name());
ui->tableWidget->setCellWidget(1,1,qle);
QLineEdit *qle1=new QLineEdit();
qle1->setText(QString::number(p.get_price()));
ui->tableWidget->setCellWidget(1,2,qle1);
QSpinBox *qsb=new QSpinBox();
qsb->setValue(p.get_count());
ui->tableWidget->setCellWidget(1,3,qsb);
}
what should I do?
QTableWidget should have defined rowCount and columnCount properties. It can be done either via QTableWidget constructor (https://doc.qt.io/qt-5/qtablewidget.html#QTableWidget-1) or via appropriate methods (setRowCounts and setColumnCounts). If it is done already, that's great.
insertRow inserts an empty row into the table at given position. ui->tableWidget->insertRow(1) would insert a new row at position 1 only if you have previously defined rowCount and columnCount (see point 1).
It depends what is your idea here - if you would like to have at least 4 columns (please note that we are counting from 0 and the QSpinBox is attempted to be put into third column) and insert new product always at first row, your code with point 1 fullfilled will be work fine. But, if your idea is to add a new row each time new product is added, you should rather call ui->tableWidget->insertRow(tableWidget->rowCount()) and use that value to address appropriate row.
You may also want to have a look here for example how to setup QTableWidget: https://wiki.qt.io/How_to_Use_QTableWidget
I have a shell script, in which it connects to db and extracts the data into .CSV file. but when i downloaded the file and opened in excel it is appending zeros to last three digits of a column, because the column has 18 digits, but excel can support only upto 15 for number, is there any way that I can change my logic in shell script so when opening in excel doesn't show last three digits as zeros
Thanks in Advance
A number that contains more than 15 digits in Excel, it changes any digits past the fifteenth place to zeros. You can cast the column values to string on export.
// Use customizeData
exportOptions: {
//--
},
customizeData: function (data) {
// This code is to find the column name's index which you want to cast.
var ind = data.header.indexOf("ColumnName");
for (var i = 0; i < data.body.length; i++) {
// will cast the number to string.
data.body[i][ind] = '\u200C' + data.body[i][ind];
}
}
Error_Image:
If you need to look data in Excel, you must put number data in text format. So, Excel doesn't try to convert it to number. To do so, put numbers in CSV file as below
="123123123123123123123";="123123123123123123123"
I have a qtablewidget with some rows. I need to sort these row randomly.
What is the best way to do this ? I have no idea except remove and add all row randomly ...
"I assume you are looking for some custom sort. Not column header click sort"
I have a way to do it. may be much more efficient way will be there.
get the "QList" of "QTableWidgetItem" using below function.
QList<QTableWidgetItem *> findItems(const QString & text, Qt::MatchFlags flags) const
Then sort your list as shown below with your condiion
bool sortFun(const QTableWidgetItem* v1, const QTableWidgetItem* v2)
{
return //your sor criteria;
}
int doComparison()
{
QList<QTableWidgetItem *> fieldsList;
// Add items to fieldsList.
qSort(fieldsList.begin(), fieldsList.end(), sortFun);
}
Not clear your "QTableWidget".
Then re assign your sorted list of "QTableWidgetItems" to QTableWidget
Finally I have done something different.
What I need to do:
- random the rows of my table
- I have a column with a "Time" and I initialize it with 15:00 for the first row and then add 1 minute for each row.
The solution I used:
- create a qlist with my times (15:00, 15:01, 15:02 ...)
- randomize the qlist (15:01, 15:03, ....)
- assign the qlist's items to the column "Time" of my table
- order my table with the column "Time" with the sort column method.
I am trying to read and parse and excel and some unclear things come into play as usual for me.
Here is what i have:
while (true)
{
comVariantCell1 = cells.item(row, 1).value().variantType();
comVariantCell2 = cells.item(row, 2).value().variantType();
//if an empty cell is found, processing will stop and user will get an error message in order to solve the inconsistency.
if (comVariantCell1 != COMVariantType::VT_EMPTY && comVariantCell2 != COMVariantType::VT_EMPTY)
{
//both cells have values, check their types.
importedLine = conNull();
progress1.setText(strfmt("Importing row %1", row));
if (cells.item(row, 1).value().variantType() == COMVariantType::VT_BSTR)
{
importedLine += cells.item(row, 1).value().bStr();
}
else
{
importedLine += cells.item(row, 1).value().double();
}
importedLine += cells.item(row, 2).value().double();
importedLinesCollection += [importedLine]; //conIns(importedLinesCollection, row - 1, (importedLine));
row++;
}
else
{
info (strFmt("Empty cell found at line %1 - import will not continue and no records were saved.", row));
break;
}
}
Excel format:
Item number Transfer Qty
a100 50.5
a101 10
a102 25
This worked well to check if the cell type is string: COMVariantType::VT_BSTR
but what should i use to check for a real or integer value ?
I am pretty sure in this case, the quantity will be not contain real values but anyway, it could be useful in the future to make the difference between these two types.
I have to mention that, even if i have an int value and I use cells.item(row, 1).value().int() it won't work. I can't see why.
Why do i want to make the difference? Because if it's forbidden to have real values in the quantity column ( at least in my case ), i want to check that and give the user the opportunity to put a correct value in that place and maybe further investigate why that happened to be there.
Take a look on how it is done in \Classes\SysDataExcelCOM\readRow.
It is basically using switch to test the type. This is really boring!
Also take a look on ExcelIO, a class I made some years ago. It reads Excel and returns each row as a container. This is a more high-level approach.
As a last resort you could save the Excel as a tab separated file. Then use TextIO to read the content. This will be at least 10 times faster than using Excel!
I have read this article and tested, it works but my problem is that there are one wide empty column (column A) and one wide empty row (row 1) in every sheet (in excel file). I know that this is the setting of PrintingBase class. But how can i remove those first empty column and row ?
i have found the answer to my own question:
var compositeLink = new CompositeLinkBase();
var link1 = new PrintableComponentLinkBase();
// this is the margins in sheet1
link1.Margins.Left = 0;
link1.MinMargins.Left = 0;
link1.Component = DG1;
compositeLink.Links.Add(link1);
// then export to excel :)