View Full Version : How to make a simple table and calculate col sums?
teele
25th February 2009, 17:55
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/task-tracker/index_html?method=entry&id=192313
but how to implement the calculable rows in it in a non-complicated way?
Any hints are well appreciated.
teele
spirit
25th February 2009, 18:23
very simple example
h
#ifndef TEST_H
#define TEST_H
#include <QMainWindow>
class QTableWidget;
class QTableWidgetItem;
class Test: public QMainWindow
{
Q_OBJECT
public:
Test(QWidget *parent = 0);
private slots:
void recalculateSums(QTableWidgetItem *);
private:
QTableWidget *m_table;
};
#endif//TEST_H
cpp
#include <QtGui>
#include "test.h"
Test::Test(QWidget *parent)
: QMainWindow(parent)
{
m_table = new QTableWidget(10, 10);
for (int row = 0; row < m_table->rowCount(); ++row) {
for (int column = 0; column < m_table->columnCount(); ++column) {
QTableWidgetItem *tableItem = new QTableWidgetItem;
tableItem->setText(QString::number(row));
m_table->setItem(row, column, tableItem);
}
}
const int lastRow = m_table->rowCount();
m_table->setRowCount(lastRow + 1);
for (int column = 0; column < m_table->columnCount(); ++column)
m_table->setItem(lastRow, column, new QTableWidgetItem);
QTableWidgetItem *item = new QTableWidgetItem;
item->setText(tr("Sum"));
m_table->setVerticalHeaderItem(lastRow, item);
setCentralWidget(m_table);
connect(m_table, SIGNAL(itemChanged(QTableWidgetItem *)), SLOT(recalculateSums(QTableWidgetItem *)));
}
void Test::recalculateSums(QTableWidgetItem *item)
{
const int lastRow = m_table->rowCount() - 1;
if (item && item->row() == lastRow)
return;
for (int column = 0; column < m_table->columnCount(); ++column) {
int sum = 0;
for (int row = 0; row < lastRow; ++row) {
QTableWidgetItem *tableItem = m_table->item(row, column);
sum += tableItem->text().toInt();
}
m_table->item(lastRow, column)->setText(QString::number(sum));
}
}
teele
25th February 2009, 19:04
Thank you, Spirit!
The code runs fine. Now i have to study it carefully to get in the qt world.
teele
zizoumgs
15th June 2009, 14:44
Thank spirit , For newbie like me your code very, very helpful
aprado
19th January 2011, 22:34
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!!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.