Forum
Sign Up
× DataGrid for Qt.C++

Export To CSV( my own code, but slow... )

9 years 8 months ago #1 by Kwangho Kim
i want to copy-paste feature with qtitandatagrid. but currently qtitandatagrid does not support this feature. so i decide to make my own code.

--- i edited qtitandatagrid source code because qtitandatagrid cannot signal ctrl+c event.
--- this code does not support banded table view.
--- this code is very slow. so i want built-in fast method and event slot like ctrl+c.
--- this code based on QtitanDataGrid 3.6.2

QtnGrid.h

GridChangeFlags m_changeFlags;
QHash<GridActionType, QAction*> m_actions;
QHash<int, QAction*> m_customActions;
protected Q_SLOTS:
virtual void controllerChanged() = 0;

QtnGridTableView.cpp

void GridTableView::createCustomAction( int actionIdentifier, const QIcon& icon, const QString& text, const QObject* receiver, const char* member, const QKeySequence& shortcut /*= QKeySequence() */ )
{
m_customActions.insert( actionIdentifier, addActionToWidget( grid(), icon, text, receiver, member, shortcut ) );
}


QtnGridTableView.h

virtual void print(QPrinter* printer);
virtual void createCustomAction( int actionIdentifier, const QIcon& icon, const QString& text, const QObject* receiver, const char* member, const QKeySequence& shortcut = QKeySequence() );

public Q_SLOTS:
void expandAll();

============== usage
ui.tblFlexLMLicenseLogs->view< Qtitan::GridTableView >()->createCustomAction( 0, QIcon(), "Copy", this, SLOT( copyDataToClipboardFromFlexLM() ), QKeySequence::Copy );

... wrapper code dropped...
... LM_TRACE_TO, LM_DEBUG_TO macro is my own debug code, so these codes are redundants

void copyDataToClipboardUsingCSV( const Qtitan::Grid* grd )
{
/*!
TEXT(CSV) 형태로 데이터를 클립보드로 복사하는 코드 -> QtitanDataGrid

#. 그룹을 지정하지 않았을 때

첫번째 행 : 헤더 컬럼으로 콜론(,) 으로 구분함
두번째 행 이후 : 데이터 컬럼으로 콜론(,) 으로 구분하며 텍스트 데이터는 "" 로 감싸도록 함

#. 그룹을 지정하였을 때

첫번째 행 : 헤더 컬럼으로 콜론(,) 으로 구분한다. 또한, 그룹으로 묶인 컬럼을 우선적으로 배치한다.
두번째 행 이후 : 데이터 컬럼으로 콜론(,) 으로 구분하며 텍스트 데이터는 "" 로 감싸도록 함

※ 밴드 그리드에서는 지원하지 않음
※ 엑셀에서는 텍스트 마법사를 통해 붙여넣기 가능
*/

QString clipboardData;
LM_TRACE_TO( logger_dlgmain, L"QtitanDataGrid CtrlC Event" );
auto spTableView = grd->view< Qtitan::GridTableView >();
auto stClipboard = QApplication::clipboard();

do
{
auto selectionCells = spTableView->selection();
if( selectionCells == NULLPTR )
break;

// 그룹을 지정했다면 그룹 컬럼 이름을 첫번째 행에 넣음
QVector< Qtitan::GridTableColumn* > vecGroupName;
for( int idx = 0; idx < spTableView->groupColumnCount(); ++idx )
{
auto groupColumn = spTableView->groupColumnByIndex( idx );
QString caption = groupColumn->caption();

LM_DEBUG_TO( logger_dlgmain, L"GROUP INDEX = %1, CAPTION = %2, VISUAL INDEX = %3", idx, caption, groupColumn->visualIndex() );
vecGroupName.push_back( groupColumn );
clipboardData += "\"" + caption + "\",";
}

// 모든걸 다 그룹으로 묶었다면 헤더가 없으므로 마지막의 , 를 제거한다
if( clipboardData.endsWith( "," ) == true && spTableView->getColumnCount() == 0 )
clipboardData = clipboardData.left( clipboardData.length() - 1 );

// 헤더 데이터 얻기, 일반 헤더는 보이지 않는 것도 모두 반환되므로 보이는 것만 추가한다
for( int idx = 0; idx < spTableView->getColumnCount(); ++idx )
{
auto column = spTableView->getColumn( idx );
LM_DEBUG_TO( logger_dlgmain, L"Header Info INDEX = %1, CAPTION = %2, isVisible = %3", idx, column->caption(), column->isVisible() );
if( column->isVisible() == true )
{
clipboardData += "\"" + column->caption() + "\",";
}
}

if( clipboardData.endsWith( "," ) == true )
clipboardData = clipboardData.left( clipboardData.length() - 1 );

if( clipboardData.isEmpty() == false )
clipboardData += "\r\n";

int currentRowIndex = -1;
while( selectionCells->end() == false )
{
auto cell = selectionCells->cell();
auto row = selectionCells->row();

if( cell->rowIndex() != currentRowIndex )
{
if( clipboardData.endsWith("\n") == false )
clipboardData += "\r\n";

if( vecGroupName.isEmpty() == false && row->type() == Qtitan::GridRow::DataRow )
{
QVariant vtData;
Qtitan::GridRow* groupRow = row;
QStack<QString> stack;

do
{
groupRow->groupValue( vtData );
stack.push( vtData.toString() );
groupRow = groupRow->parentGroupRow();

} while( groupRow != NULLPTR );

while( stack.isEmpty() == false )
clipboardData += stack.pop() + ",";

}
}

if( row->type() == Qtitan::GridRow::DataRow )
{
LM_DEBUG_TO( logger_dlgmain, L"ROW = %1, GROUP COLUMN = %4 CELL = %2, DATA = %3",
cell->rowIndex(),
cell->columnIndex(),
cell->modelIndex().data().toString(),
row->groupColumn() );
clipboardData += cell->modelIndex().data().toString() + ",";
}

currentRowIndex = cell->rowIndex();
selectionCells->next();
}

stClipboard->setText( clipboardData );

} while( false );

}

Please Log in or Create an account to join the conversation.

More
9 years 8 months ago #2 by Developer Machines
Could you please tell me how many rows in your model?

Please Log in or Create an account to join the conversation.

More
  • Not Allowed: to create new topic.
  • Not Allowed: to reply.
  • Not Allowed: to edit your message.
Moderators: Developer Machines
Time to create page: 0.113 seconds

Developer Newsletter

Join our Developer Machines newsletter to get informed on all the latest releases of the commercial components for Qt.C++, Delphi FireMonkey, updates and general knowledges.

Quick Support

Should you need any additional information about our products or licensing, please contact us at the following email addresses:

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

Get in Touch

If you would like to purchase our products or services, but don’t know how to do it the right way, please feel free to contact us:

  • This email address is being protected from spambots. You need JavaScript enabled to view it.( any questions related to our products or services )
  • This email address is being protected from spambots. You need JavaScript enabled to view it.( questions related to licensing )