how to insert items in tableWidget
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
Code:
#include <QtGui>
#include "addressbook.h"
#include "additem.h"
#include <QAbstractTableModel>
AddressBook
::AddressBook(QWidget *parent
){
ui.setupUi(this);
int col=0;
for(col = 0; col < 3; col++)
{
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
Code:
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include <QtGui/QMainWindow>
#include "ui_addressbook.h"
{
Q_OBJECT
public:
~AddressBook();
private slots:
void addItem();
// void editItem();
//void deleteItem();
private:
Ui::AddressBook ui;
};
#endif // ADDRESSBOOK_H
additem.h
Code:
#ifndef ADDITEM_H
#define ADDITEM_H
#include <QtGui/QDialog>
#include "ui_additem.h"
{
Q_OBJECT
public:
~AddItem();
private:
Ui::AddItem ui;
};
#endif // ADDITEM_H
additem.cpp
Code:
#include "additem.h"
{
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()
{
}
Re: how to insert items in tableWidget
Just like it says in the docs:
Quote:
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);
Re: how to insert items in tableWidget
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...
Code:
(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..
Code:
#include <QtGui>
#include "addressbook.h"
#include "additem.h"
#include <QAbstractTableModel>
#include<QLayout>
#include<QBoxLayout>
AddressBook
::AddressBook(QWidget *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 ) {
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++)
{
ui.tableWidget->setItem(row, col, newItem);
}
}
}
AddressBook::~AddressBook()
{
}
Re: how to insert items in tableWidget
Quote:
Code:
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
Code:
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!!