View Full Version : How to add data into the QTableWidget

15th October 2009, 07:33
Hi All,
I have a QTableWidget which has 200 rows and 4 columns. The user will input the data from 4 QlineEdits in a different dialog. These 4 QlineEdit Values has to be displayed in the QTableWidget. How to resolve this issue. any snippts will be greatful.

15th October 2009, 08:31
the simplest way. it is to pass pointer to QTableWidget into your dialog.

Dialog::Dialog(QTableWidget *table, QWidget *parent)
: Dialog(parent), m_table(table)

void Dialog::saveDataIntoTable()
if (!m_table)

const int currentRow = m_table->rowCount();
m_table->setRowCount(currentRow + 1);

m_table->setItem(currentRow, 0, new QTableWidgetItem(m_lineEdit1->text()));
m_table->setItem(currentRow, 1, new QTableWidgetItem(m_lineEdit2->text()));

15th October 2009, 09:03
another way is to create getters in your dialog and use them to get values after dialog.exec() returns.

15th October 2009, 09:15
Thanks for the reply my intension is to make application similar to addressbook example in Qt but instead of Listview i need to use TableWidget..

And here is my code snippet

ToolForm::ToolForm( QDialog *parent, Qt::WindowFlags f)
: QDialog( parent, f),


void ToolForm::setupViews()
QTableWidget *tooltableWidget = new QTableWidget(200, 4, this);
tooltableWidget->setHorizontalHeaderLabels(QStringList() << tr("TOOL NUMBER")
<< tr("DIAMETER")
<< tr("LENGTH")
<< tr("UNITS"));
tooltableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
tooltableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
tooltableWidget->setSelectionBehavior(QAbstractItemView::SelectRows );
tooltableWidget->setGeometry(QRect(0, 100, 781, 281));
tooltableWidget->horizontalHeader()->resizeSection(1, 250);
tooltableWidget->horizontalHeader()->resizeSection(2, 250);
tooltableWidget->horizontalHeader()->resizeSection(3, 180);

void ToolForm::add_tool()
ToolAddDialog tooladddialog;

if( tooladddialog.exec())

QTableWidget *tooltableWidget = new QTableWidget;
QString str = tooladddialog.toolgetValues();
QStringList fields = str.split(" ");
//now i need to display the values from tooladddialog. to TableWidget

how to proceed with this..

16th October 2009, 09:59
Hi All,

I tried with the method which was replied by spirit but its not working can anyone tell me how to resolve this issue.

16th October 2009, 11:57
I tried with the method which was replied by spirit but its not working can anyone tell me how to resolve this issue.

Then you probably have an error in your source code. Show us your relevant code, then we can help.

15th November 2013, 04:55
Could some one tell me how to do the same using JavaScript? Really appreciated.