Results 1 to 4 of 4

Thread: how to insert items in tableWidget

  1. #1
    Join Date
    Oct 2008
    Location
    Dar es Salaam,Tanzania
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #include <QtGui>
    2. #include "addressbook.h"
    3. #include "additem.h"
    4. #include <QAbstractTableModel>
    5. AddressBook::AddressBook(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8.  
    9. ui.setupUi(this);
    10. int col=0;
    11. for(col = 0; col < 3; col++)
    12. {
    13. item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
    14. ui.tableWidget->setItem(0,1, item);
    15. }
    16. connect( ui.actionAdd_Item, SIGNAL(triggered()), this, SLOT(addItem()) );
    17. connect( ui.actionEdit_Item, SIGNAL(triggered()), this, SLOT(editItem()) );
    18. connect( ui.actionDelete_Item, SIGNAL(triggered()), this, SLOT(deleteItem()) );
    19. }
    20.  
    21. void AddressBook::addItem()
    22. {
    23.  
    24. AddItem dlg( this );
    25.  
    26.  
    27. if( dlg.exec() == QDialog::Accepted )
    28. {
    29.  
    30. dlg.name(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    31. dlg.email(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    32. dlg.phone(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    33.  
    34.  
    35.  
    36. }
    37. }
    38. AddressBook::~AddressBook()
    39. {
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 



    addressbook.h
    Qt Code:
    1. #ifndef ADDRESSBOOK_H
    2. #define ADDRESSBOOK_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_addressbook.h"
    6. class AddressBook : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. AddressBook(QWidget *parent = 0);
    12. ~AddressBook();
    13.  
    14. private slots:
    15.  
    16. void addItem();
    17. // void editItem();
    18. //void deleteItem();
    19. private:
    20. Ui::AddressBook ui;
    21. };
    22.  
    23. #endif // ADDRESSBOOK_H
    To copy to clipboard, switch view to plain text mode 


    additem.h
    Qt Code:
    1. #ifndef ADDITEM_H
    2. #define ADDITEM_H
    3.  
    4. #include <QtGui/QDialog>
    5. #include "ui_additem.h"
    6.  
    7. class AddItem : public QDialog
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. AddItem(QWidget *parent = 0);
    13. const QString name() const;
    14. const QString email() const;
    15. const QString phone() const;
    16. ~AddItem();
    17.  
    18. private:
    19. Ui::AddItem ui;
    20. };
    21.  
    22. #endif // ADDITEM_H
    To copy to clipboard, switch view to plain text mode 

    additem.cpp
    Qt Code:
    1. #include "additem.h"
    2.  
    3. AddItem::AddItem(QWidget *parent)
    4. : QDialog(parent)
    5. {
    6. ui.setupUi(this);
    7. // QStrinng name;
    8. // QStrinng email;
    9. // QStrinng phone;
    10. }
    11. const QString AddItem::name() const
    12. {
    13. return ui.Name->text();
    14. }
    15.  
    16. const QString AddItem::email() const
    17. {
    18. return ui.Email->text();
    19. }
    20. const QString AddItem::phone() const
    21. {
    22. return ui.Phone->text();
    23. }
    24. AddItem::~AddItem()
    25. {
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to insert items in tableWidget

    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);
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2008
    Location
    Dar es Salaam,Tanzania
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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...

    Qt Code:
    1. QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
    2. (row+1)*(column+1)));
    3. tableWidget->setItem(row, column, newItem);
    To copy to clipboard, switch view to plain text mode 


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

    Qt Code:
    1. #include <QtGui>
    2. #include "addressbook.h"
    3. #include "additem.h"
    4. #include <QAbstractTableModel>
    5. #include<QLayout>
    6. #include<QBoxLayout>
    7. AddressBook::AddressBook(QWidget *parent)
    8. : QMainWindow(parent)
    9. {
    10.  
    11. row = -1;
    12.  
    13. ui.setupUi(this);
    14.  
    15.  
    16. connect( ui.actionAdd_Item, SIGNAL(triggered()), this, SLOT(addItem()) );
    17. connect( ui.actionEdit_Item, SIGNAL(triggered()), this, SLOT(editItem()) );
    18. connect( ui.actionDelete_Item, SIGNAL(triggered()), this, SLOT(deleteItem()) );
    19. }
    20.  
    21. void AddressBook::addItem()
    22. {
    23.  
    24. AddItem dlg( this );
    25.  
    26. if( dlg.exec() == QDialog::Accepted )
    27. {
    28. QString item[3];
    29. item[0] = dlg.name(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    30. item[1] = dlg.email(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    31. item[2] = dlg.phone(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    32. ++row;
    33. int col=0;
    34. for(col = 0; col < 3; col++)
    35. {
    36.  
    37. QTableWidgetItem *newItem = new QTableWidgetItem(tr("item[col]"));
    38. ui.tableWidget->setItem(row, col, newItem);
    39.  
    40. }
    41.  
    42.  
    43. }
    44. }
    45. AddressBook::~AddressBook()
    46. {
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to insert items in tableWidget

    Qt Code:
    1. item[0] = dlg.name(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    2. item[1] = dlg.email(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    3. item[2] = dlg.phone(); //I NEED TO KNOW HOW TO INSERT THIS ITEMS INTO ui.tableWidget
    To copy to clipboard, switch view to plain text mode 
    I also need to know that .
    I want to put a string as a tableWidget item.When I use
    Qt Code:
    1. ui->tableWidget_3->setItem(1,1,&x);
    To copy to clipboard, switch view to plain text mode 
    they ask me to make x as a QTableWidgetItem .I made it , but I can't include QString in QTableWidgetItem.

    Anyone can help us!!
    Last edited by Malek; 24th July 2010 at 19:50.

Similar Threads

  1. Light items for the graphicsView
    By maverick_pol in forum Qt Programming
    Replies: 12
    Last Post: 1st November 2007, 19:51
  2. again problem in add items in tablewidget
    By jyoti in forum Qt Tools
    Replies: 3
    Last Post: 24th November 2006, 13:03
  3. Replies: 2
    Last Post: 23rd November 2006, 12:33
  4. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 13:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.