PDA

View Full Version : how to insert items in tableWidget



roncriss
21st November 2008, 07:47
Hi guys
I am currently learning Qt and now i'm working on address book.I need to know how i can insert name,email and phone Number into TableWidget by using an addItem Dialog.
What i need is this ,after user has added name,email and phoneNumer in the dialog ,when Ok button is clicked the items should be Inserted into tableWidget in mainWindow.
sorry for my english.


Here...
adressbook.cpp

#include <QtGui>
#include "addressbook.h"
#include "additem.h"
#include <QAbstractTableModel>
AddressBook::AddressBook(QWidget *parent)
: QMainWindow(parent)
{

ui.setupUi(this);
int col=0;
for(col = 0; col < 3; col++)
{
QTableWidgetItem *item = new QTableWidgetItem;
item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
ui.tableWidget->setItem(0,1, item);
}
connect( ui.actionAdd_Item, SIGNAL(triggered()), this, SLOT(addItem()) );
connect( ui.actionEdit_Item, SIGNAL(triggered()), this, SLOT(editItem()) );
connect( ui.actionDelete_Item, SIGNAL(triggered()), this, SLOT(deleteItem()) );
}

void AddressBook::addItem()
{

AddItem dlg( this );


if( dlg.exec() == QDialog::Accepted )
{

dlg.name(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
dlg.email(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
dlg.phone(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget



}
}
AddressBook::~AddressBook()
{

}




addressbook.h



#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H

#include <QtGui/QMainWindow>
#include "ui_addressbook.h"
class QTableWidget;
class AddressBook : public QMainWindow
{
Q_OBJECT

public:
AddressBook(QWidget *parent = 0);
~AddressBook();

private slots:

void addItem();
// void editItem();
//void deleteItem();
private:
Ui::AddressBook ui;
};

#endif // ADDRESSBOOK_H



additem.h

#ifndef ADDITEM_H
#define ADDITEM_H

#include <QtGui/QDialog>
#include "ui_additem.h"

class AddItem : public QDialog
{
Q_OBJECT

public:
AddItem(QWidget *parent = 0);
const QString name() const;
const QString email() const;
const QString phone() const;
~AddItem();

private:
Ui::AddItem ui;
};

#endif // ADDITEM_H


additem.cpp

#include "additem.h"

AddItem::AddItem(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
// QStrinng name;
// QStrinng email;
// QStrinng phone;
}
const QString AddItem::name() const
{
return ui.Name->text();
}

const QString AddItem::email() const
{
return ui.Email->text();
}
const QString AddItem::phone() const
{
return ui.Phone->text();
}
AddItem::~AddItem()
{

}

high_flyer
21st November 2008, 09:13
Just like it says in the docs:

Items are created ouside the table (with no parent widget) and inserted into the table with setItem():

QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
(row+1)*(column+1)));
tableWidget->setItem(row, column, newItem);

roncriss
21st November 2008, 20:09
Thank you for the reply but i already went trough the docs and still i can't understand how to use this
example in my code..
please can you give me a lead on this...


QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
(row+1)*(column+1)));
tableWidget->setItem(row, column, newItem);


i modified my addressbook.cpp to this down here but still when i click Ok button nothing is happening..


#include <QtGui>
#include "addressbook.h"
#include "additem.h"
#include <QAbstractTableModel>
#include<QLayout>
#include<QBoxLayout>
AddressBook::AddressBook(QWidget *parent)
: QMainWindow(parent)
{

row = -1;

ui.setupUi(this);


connect( ui.actionAdd_Item, SIGNAL(triggered()), this, SLOT(addItem()) );
connect( ui.actionEdit_Item, SIGNAL(triggered()), this, SLOT(editItem()) );
connect( ui.actionDelete_Item, SIGNAL(triggered()), this, SLOT(deleteItem()) );
}

void AddressBook::addItem()
{

AddItem dlg( this );

if( dlg.exec() == QDialog::Accepted )
{
QString item[3];
item[0] = dlg.name(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
item[1] = dlg.email(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
item[2] = dlg.phone(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
++row;
int col=0;
for(col = 0; col < 3; col++)
{

QTableWidgetItem *newItem = new QTableWidgetItem(tr("item[col]"));
ui.tableWidget->setItem(row, col, newItem);

}


}
}
AddressBook::~AddressBook()
{

}

Malek
24th July 2010, 17:10
item[0] = dlg.name(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
item[1] = dlg.email(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
item[2] = dlg.phone(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget


I also need to know that .
I want to put a string as a tableWidget item.When I use

ui->tableWidget_3->setItem(1,1,&x);
they ask me to make x as a QTableWidgetItem .I made it , but I can't include QString in QTableWidgetItem.

Anyone can help us!!