PDA

View Full Version : How to read and write data into the QTableView



grsandeep85
20th October 2009, 12:19
Hi All,

I have made a QTableView which has 200 rows and 4 columns, when the user press the enter Key it opens a new QDialog which has 4 QlineEdits and these QlineEdits values has to be displayed in QTableView. here is the code snippet for QDialog

tooladddialog.cpp


QString ToolAddDialog::toolgetValues()
{
QString line;

toolnolineEdit->selectAll();
line.append( toolnolineEdit->text() );
line.append( " " );

diameterlineEdit->selectAll();
line.append( diameterlineEdit->text() );
line.append( " " );

lengthlineEdit->selectAll();
line.append( lengthlineEdit->text() );
line.append( " " );

unitslineEdit->selectAll();
line.append( unitslineEdit->text() );
return line;
}


tooform.cpp


void ToolForm::keyPressEvent( QKeyEvent *event )
{
qDebug( "Key Press Event" );
switch ( event->key() )
{
case Qt::Key_Return: //Mapped with Enter Key
add_tool();

break;

case Qt::Key_Escape: //Mapped with Cancel key
close();
break;

default:
QWidget::keyPressEvent(event);
}
}

void ToolForm::add_tool()
{
ToolAddDialog tooladddialog;

if( tooladddialog.exec())
{
QString str = tooladddialog.toolgetValues();
QStringList fields = str.split(" ");

if (!tooltableView)
return;
// now values should be added to the QTableView
}
}


How to resolve this issue. if any snippet would be advantage.

spirit
20th October 2009, 12:23
use QAbstractItemModel::setData for setting data into model.
PS. read about Model/View programming (http://doc.trolltech.com/4.5/model-view-programming.html) also.

grsandeep85
20th October 2009, 12:31
Hi Spirit,

Thanks for the reply.:)