I'm having the same problem msimurin.
This code not for csv works but for ordinary txt files works slightly in that something is imported but not correctly
{
clear();
const int TabSize =8;
int endlCount =0;
int spaceCount=0;
int column=0;
while (!in.atEnd())
{
in >> txt;
if (txt=='\n') // row
{
//++endlCount;
//spaceCount=0;
//column=0;
++row;
}
else if (txt=='\t') //column
{
//int size=TabSize-(column%TabSize);
//spaceCount+=size;
//column+=size;
++column;
}
else if (txt==' ') //column
{
//++spaceCount;
++column;
}
/*else
{
//row=endlCount;
}*/
// ++column;
in >> row >> column >> txt;
setFormula(row, column, txt); //spreadsheet specific code given in C++ GUI programming with QT4
}
}
{
clear();
QTextStream in(&file);
QString txt;
const int TabSize =8;
int endlCount =0;
int spaceCount=0;
int column=0;
QApplication::setOverrideCursor(Qt::WaitCursor);
while (!in.atEnd())
{
in >> txt;
if (txt=='\n') // row
{
//++endlCount;
//spaceCount=0;
//column=0;
++row;
}
else if (txt=='\t') //column
{
//int size=TabSize-(column%TabSize);
//spaceCount+=size;
//column+=size;
++column;
}
else if (txt==' ') //column
{
//++spaceCount;
++column;
}
/*else
{
//row=endlCount;
}*/
// ++column;
in >> row >> column >> txt;
setFormula(row, column, txt); //spreadsheet specific code given in C++ GUI programming with QT4
}
}
To copy to clipboard, switch view to plain text mode
The code given in the above book for importing txt files doesn't compile it doesn't like something about the join() function used.
I also found this code posting somewhere which imports CSV files as one row yuo might find useful
Note:
void mywidget::read_line()
{
char* cfg_file = getenv("FILE");
while ( !file.atEnd() ) {
if ( vals.size() == no_of_cols ) { // valid line of text
tbl_trades->insertRow( 0 ); // new row
tbl_trades->setRowHeight( 0, 20 ); // resize new row
for ( int i = 0; i < no_of_cols; i++ ) { //
vals[i] );
tbl_trades->setItem( 0, i, temp );
}
}
}
}
}
Note:
tbl_trades is my own subclassed QTableWidget.
void mywidget::read_line()
{
char* cfg_file = getenv("FILE");
QFile file( cfg_file );
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
while ( !file.atEnd() ) {
QByteArray line = file.readLine();
QStringList vals = QStringList::split( ',', line, 0 );
if ( vals.size() == no_of_cols ) { // valid line of text
tbl_trades->insertRow( 0 ); // new row
tbl_trades->setRowHeight( 0, 20 ); // resize new row
for ( int i = 0; i < no_of_cols; i++ ) { //
QTableWidgetItem *temp = new QTableWidgetItem(
vals[i] );
tbl_trades->setItem( 0, i, temp );
}
}
}
}
}
To copy to clipboard, switch view to plain text mode
I haven't given up on this I think the answer is to stream it in
count delimiters so for txt (how does csv give columns?)
1) count rows using split( '\n') and put this in stringlist
2) then count columns
3) then iterate over rows and column count and use split( '\t') into cells using the tab delimiter
however my attempt to do this has failed in that it seems to put something in the cell 0,0 but not anything visible and nothing in the other cells trying to import a txt file with
one three
two four
while (!in.atEnd())
{
txt=in.readLine(); // read in file
rows=txt.split('\n'); // search for newlines
columns = txt.split('\t'); // search for tabs
}
int numRows=rows.count();
int numColumns=columns.count()+1;
//int numColumns=rows.first().count('\t')+1;
int row=0;
int column=0;
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numColumns; ++j)
{
// columns=txt[i].split('\t');
// int row = range.topRow() + i;
// int column = range.leftColumn() + j;
if (row < RowCount && column < ColumnCount)
{
in >> row >> column >> txt;
setFormula(row, column, txt);
row++;
column++;
}
}
}
}
QTextStream in(&file);
QString txt;
QStringList rows;
QStringList columns;
QApplication::setOverrideCursor(Qt::WaitCursor);
while (!in.atEnd())
{
txt=in.readLine(); // read in file
rows=txt.split('\n'); // search for newlines
columns = txt.split('\t'); // search for tabs
}
int numRows=rows.count();
int numColumns=columns.count()+1;
//int numColumns=rows.first().count('\t')+1;
int row=0;
int column=0;
for (int i = 0; i < numRows; ++i) {
for (int j = 0; j < numColumns; ++j)
{
// columns=txt[i].split('\t');
// int row = range.topRow() + i;
// int column = range.leftColumn() + j;
if (row < RowCount && column < ColumnCount)
{
in >> row >> column >> txt;
setFormula(row, column, txt);
row++;
column++;
}
}
}
}
To copy to clipboard, switch view to plain text mode
It really bugging me since it cannot be that difficult and I want to crack it myself.... When one of us has cracked it why don't we post it here....
Bookmarks