Solved Adding more actions to the context menu of a spreadsheet app
-
@mrjj
Thank you. I read other member functions of the class and reached the point below:void Spreadsheet::sum() { QList<QTableWidgetItem *> items = selectedItems(); int i = 0, j = 0; double d = 0; if (!items.isEmpty()) { foreach (QTableWidgetItem *item, items) d += item->text().toDouble(); i = QTableWidget::currentRow(); j = QTableWidget::currentColumn(); somethingChanged(); } QTableWidgetItem *item = &d; qDebug()<<"Row is: "<< ++i << ' ' << "Column is " << ++j; ++i; QTableWidget::setItem(i, j, item); }
qDebug() shows well and
i
andj
are pointing to the one below the last cell of the selected area. (The target cell)
There is a problem here:QTableWidgetItem *item = &d;
if it's correct, I apparently will be able to put
d
into the target cell usingQTableWidget::setItem(i, j, item);
-
@tomy said in Adding more actions to the context menu of a spreadsheet app:
QTableWidgetItem *item = &d;
Hi. good work trying.
That seems really wrong.
you say
let QTableWidgetItem * called item points to the memory of the double called d;You should use a function to set the text.
http://doc.qt.io/qt-5/qtablewidgetitem.html#setTextsomething like
QTableWidgetItem *item = new QTableWidgetItem ;
item->setText( QString::number(d) );
QTableWidget::setItem(i, j, item);unless there already IS an item there?
Then you shoul dnot add new, but set text on it. -
Hi,
@mrjjitem->setText( QString::number(d) );
This was the key point that I needed! ;)
I wrote two good methods. (Don't you agree? :))
Here they are:void Spreadsheet::sum() { QList<QTableWidgetItem *> items = selectedItems(); int i = 0, j = 0; double d = 0; if (!items.isEmpty()) { foreach (QTableWidgetItem *item, items) d += item->text().toDouble(); i = QTableWidget::currentRow(); j = QTableWidget::currentColumn(); somethingChanged(); } QTableWidgetItem* item = new QTableWidgetItem; item->setText((QString::number(d))); ++i; while(QTableWidget::item(i, j)) i++; QTableWidget::setItem(i, j, item); } //********************************************* void Spreadsheet::ave() { QList<QTableWidgetItem *> items = selectedItems(); int i = 0, j = 0; double d1 = 0; if (!items.isEmpty()) { foreach (QTableWidgetItem *item, items) d1 += item->text().toDouble(); i = QTableWidget::currentRow(); j = QTableWidget::currentColumn(); somethingChanged(); } double d2 = selectedItems().size(); QTableWidgetItem* item = new QTableWidgetItem; item->setText((QString::number(d1/d2))); ++i; while(QTableWidget::item(i, j)) i++; QTableWidget::setItem(i, j, item); }
They work well. You know, I'm aware of C++ programming but not well informed of those numerous methods and classes of Qt, and that is why I face problems. It will gradually be solved. :)
-
Hi
Good work.
What happens if you select nothing and then add sum ?
Will it not insert 0 anyway ?
Is that how you want it? -
This post is deleted! -
@mrjj
I think it's fine now:QList<QTableWidgetItem *> items = selectedItems(); int i, j; double d; if (!items.isEmpty()) foreach (QTableWidgetItem *item, items) { d += item->text().toDouble(); i = QTableWidget::currentRow(); j = QTableWidget::currentColumn(); somethingChanged(); } QTableWidgetItem* item = new QTableWidgetItem; item->setText((QString::number(d))); ++i; while(QTableWidget::item(i, j)) i++; QTableWidget::setItem(i, j, item);
It writes nothing when we want to sum nothing.
After these I implemented
Max
,Min
andCount
for the context menu.
But the only part of the code that I dislike is this:foreach (QTableWidgetItem *item, items) { ... i = QTableWidget::currentRow(); j = QTableWidget::currentColumn(); .... }
That is, the section that I have to traverse all the cells in the selected area to find the lowest cell's position. It's in a very novice manner. If there isn't a proper function for reaching that position (as the number of the row and column) I may write a function for this avoiding repeating that part in many context menu's functions.
-
@tomy said in Adding more actions to the context menu of a spreadsheet app:
part of the code that I dislike is this:
Hmm, wont that always result in the last selected being nominated as the lowest cell pos ?
So it could be expressed as
Q_ASSERT( ! items.isEmpty() );
QTableWidgetItem* last = items.last();
i = item->row();
j = item->column(); -
Hmm, wont that always result in the last selected being nominated as the lowest cell pos?
It works, but it is a little novice-like.
Q_ASSERT( ! items.isEmpty() );
QTableWidgetItem* last = items.last();
i = item->row();
j = item->column();Did you mean:
i = last->row(); j = last->column();
I used it and got a runtime error.
So changed it to this:if(!items.isEmpty()) { QTableWidgetItem* last = items.last(); i = last->row(); j = last->column(); }
I tested the actions, sum, ave, count, max and min, they are all working fine.
-
@tomy said in Adding more actions to the context menu of a spreadsheet app:
I used it and got a runtime error.
it was the Q_ASSUME :)
but the if(!items.isEmpty()) is betterand yes it was copy paste error.
its last->row/col
as you found out. -
Thank you very much. :) ;)
-
Good work.
If possible, please mark as solved.