Results 1 to 5 of 5

Thread: How to make a simple table and calculate col sums?

  1. #1
    Join Date
    Feb 2009
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to make a simple table and calculate col sums?

    I'm newbie with qt and i'd like to make a simple table with a few rows for double typed numbers.

    Then i'd like to calculate the sums of each column and show the results on the bottom row.

    Yes, i have the book c++ programming with qt4, but the examples are quite complicated for this simple task.

    I found a basic table here

    http://www.qtsoftware.com/developer/...ntry&id=192313

    but how to implement the calculable rows in it in a non-complicated way?

    Any hints are well appreciated.

    teele

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make a simple table and calculate col sums?

    very simple example
    h
    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include <QMainWindow>
    5.  
    6.  
    7. class Test: public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Test(QWidget *parent = 0);
    13.  
    14. private slots:
    15. void recalculateSums(QTableWidgetItem *);
    16.  
    17. private:
    18. QTableWidget *m_table;
    19. };
    20.  
    21. #endif//TEST_H
    To copy to clipboard, switch view to plain text mode 

    cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "test.h"
    3.  
    4. Test::Test(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. m_table = new QTableWidget(10, 10);
    8.  
    9. for (int row = 0; row < m_table->rowCount(); ++row) {
    10. for (int column = 0; column < m_table->columnCount(); ++column) {
    11. tableItem->setText(QString::number(row));
    12. m_table->setItem(row, column, tableItem);
    13. }
    14. }
    15.  
    16. const int lastRow = m_table->rowCount();
    17. m_table->setRowCount(lastRow + 1);
    18. for (int column = 0; column < m_table->columnCount(); ++column)
    19. m_table->setItem(lastRow, column, new QTableWidgetItem);
    20.  
    21. item->setText(tr("Sum"));
    22. m_table->setVerticalHeaderItem(lastRow, item);
    23.  
    24. setCentralWidget(m_table);
    25.  
    26. connect(m_table, SIGNAL(itemChanged(QTableWidgetItem *)), SLOT(recalculateSums(QTableWidgetItem *)));
    27. }
    28.  
    29. void Test::recalculateSums(QTableWidgetItem *item)
    30. {
    31. const int lastRow = m_table->rowCount() - 1;
    32. if (item && item->row() == lastRow)
    33. return;
    34.  
    35. for (int column = 0; column < m_table->columnCount(); ++column) {
    36. int sum = 0;
    37. for (int row = 0; row < lastRow; ++row) {
    38. QTableWidgetItem *tableItem = m_table->item(row, column);
    39. sum += tableItem->text().toInt();
    40. }
    41. m_table->item(lastRow, column)->setText(QString::number(sum));
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following 2 users say thank you to spirit for this useful post:

    27Loco (26th April 2009), aprado (19th January 2011)

  4. #3
    Join Date
    Feb 2009
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to make a simple table and calculate col sums?

    Thank you, Spirit!

    The code runs fine. Now i have to study it carefully to get in the qt world.

    teele

  5. #4
    Join Date
    Apr 2009
    Location
    Indonesia
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make a simple table and calculate col sums?

    Thank spirit , For newbie like me your code very, very helpful

  6. #5
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to make a simple table and calculate col sums?

    Hello, sorry for reviving this thread but i had the same question and it helped me =)

    I have another question.. How can i insert this table as a widget inside a mainwindow? For exemple:

    I have a main window and i want to click in the option "file" and then "table", when i click table i want to show this table in the main area of my mainwindow with a close button inside.


    Other question i have is:

    How can i use the callendar? For exemple.. i want to get the current time and show it in the screen as a calendar.

    Thanks for the help!!

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.