Results 1 to 5 of 5

Thread: QTableWidget

  1. #1
    Join Date
    Dec 2010
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableWidget

    Hi,
    First, let me explain, I am an old timer coder, but have never dealt with QT.

    My question is:
    I created a QTableWidget using the gui creator (.ui) I then want to load data
    into the 2 dim. array.

    I attempted associated the slot "on_tablewidget_activated" and used
    tableWidget->setItem to load the data, but when debugging, it never
    enters the function on_tablewidget_activated.

    How do I load data into the TableWidget?

    Snippet of code:
    Qt Code:
    1. ********************************
    2. .h file:
    3.  
    4. class Widget : public QWidget {
    5. Q_OBJECT
    6. public:
    7. Widget(QWidget *parent = 0);
    8. QTableWidget tableWidget(QWidget *parent = 0);
    9. ~Widget();
    10.  
    11. protected:
    12. void changeEvent(QEvent *e);
    13.  
    14. private:
    15. Ui::Widget *ui;
    16.  
    17. private slots:
    18. void on_tableWidget_activated(QModelIndex index);
    19.  
    20. *******************************************
    21.  
    22. .cpp file
    23. #include "widget.h"
    24. #include "ui_widget.h"
    25.  
    26. Widget::Widget(QWidget *parent) :
    27. QWidget(parent),
    28. ui(new Ui::Widget)
    29. {
    30. ui->setupUi(this);
    31. }
    32.  
    33. Widget::~Widget()
    34. {
    35. delete ui;
    36. }
    37.  
    38. void Widget::changeEvent(QEvent *e)
    39. {
    40. QWidget::changeEvent(e);
    41. switch (e->type()) {
    42. case QEvent::LanguageChange:
    43. ui->retranslateUi(this);
    44. break;
    45. default:
    46. break;
    47. }
    48. }
    49.  
    50. void Widget::on_tableWidget_activated(QModelIndex index)
    51. {
    52. QTableWidgetItem *item = new QTableWidgetItem(QString("testing"));
    53. tableWidget->setItem(1,1,item);
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 


    thanks

    John
    Last edited by high_flyer; 28th December 2010 at 15:08. Reason: code tags

  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: QTableWidget

    Are you sure you want to use the activated() signal?
    The slot will be called when you activate the item which has the index 'index', which is not what I think you want.
    Just loading data to the table can be done via a normal method call (for example in the constructor).
    ==========================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
    Dec 2010
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    Hi,
    Thanks for the info. I tried moving it to the constructor and am getting the following syntax error. Can you see what I am doing wrong?

    /home/john/qt/projects/perf1/widget.cpp:10: error: invalid use of member (did you forget the ‘&’ ?)

    h file:
    Qt Code:
    1. #include <QModelIndex>
    2. #include <QTableWidget>
    3.  
    4. namespace Ui {
    5. class Widget;
    6. }
    7.  
    8. class Widget : public QWidget {
    9. Q_OBJECT
    10. public:
    11. Widget(QWidget *parent = 0);
    12. QTableWidget *tableWidget(QWidget *parent = 0);
    13. ~Widget();
    14.  
    15. protected:
    16. void changeEvent(QEvent *e);
    17.  
    18. private:
    19. Ui::Widget *ui;
    20.  
    21. private slots:
    22. void on_tableWidget_activated(QModelIndex index);
    23. };
    24.  
    25. #endif // WIDGET_H
    26.  
    27. c file:
    28. #include "widget.h"
    29. #include "ui_widget.h"
    30.  
    31. Widget::Widget(QWidget *parent) :
    32. QWidget(parent),
    33. ui(new Ui::Widget)
    34. {
    35. ui->setupUi(this);
    36. QTableWidgetItem *item = new QTableWidgetItem(QString("testing"));
    37. tableWidget->setItem(1,1,item);
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 28th December 2010 at 16:13. Reason: code tags

  4. #4
    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: QTableWidget

    you probably meant
    Qt Code:
    1. tableWidget()->setItem(1,1,item);
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

  5. #5
    Join Date
    Dec 2010
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    thanks. that did the trick.

    it was one of those "I can't see the forest through the trees"

Similar Threads

  1. QTableWidget
    By pippo42 in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2009, 13:42
  2. Help with QTableWidget
    By aarelovich in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 12:22
  3. QTableWidget
    By rick_st3 in forum Newbie
    Replies: 12
    Last Post: 26th June 2008, 11:38
  4. QTableWidget
    By Afflicted.d2 in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2006, 03:42
  5. QTableWidget
    By chak_med in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2006, 15:53

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.