Adding more actions to the context menu of a spreadsheet app



  • Hello,

    Here is the program and below is the screenshot.

    0_1502722075092_1.png
    When I have contents in the cells I want to have useful actions —for this time, sum and average—to be able to do more advanced tasks.

    For that I made some changes:
    1- Added two QActions* to the mainwindow.h file named sumAction and aveAction.
    2- Implemented them in mainwindow.cpp this way:

    sumAction = new QAction(tr("&Sum"), this);
    sumAction->setStatusTip(tr("Sum the current selection's "
                                   "contents"));
    connect(sumAction, SIGNAL(triggered()), spreadsheet, SLOT(sum()));
    
    aveAction = new QAction(tr("&Average"), this);
    aveAction->setStatusTip(tr("Average the current selection's "
                                   "contents"));
    connect(aveAction, SIGNAL(triggered()), spreadsheet, SLOT(ave()));
    

    3- Added two more functions to spreadsheet.h and implemented them in spreadsheet.cpp this way:

    double Spreadsheet::sum()
    {
        QList<QTableWidgetItem *> items = selectedItems();
        double d = 0;
        if (!items.isEmpty()) {
            foreach (QTableWidgetItem *item, items)
                d += *item;
            somethingChanged();
        }
        return d;
    }
    
    //*********************************************
    
    double Spreadsheet::ave()
    {
        QList<QTableWidgetItem *> items = selectedItems();
        double d1 = 0;
        if (!items.isEmpty()) {
            foreach (QTableWidgetItem *item, items)
                d1 += *item;
            somethingChanged();
        }
        double d2 = selectedItems().size();
        return d1/d2;
    }
    

    As it's clear I get two errors:

    error: no match for 'operator+=' (operand types are 'double' and 'QTableWidgetItem')
    d += item;

    error: no match for 'operator+=' (operand types are 'double' and 'QTableWidgetItem')
    d1 += item;

    I know that errors are right. They are two different types.
    by the way, the book says:

    A more sophisticated way of providing context menus is to reimplement the QWidget::contextMenuEvent() function,
    create a QMenu widget, populate it with the desired actions, and call exec() on it.

    Which way should I use please, my own one or the one advised by the book?
    And how to get it to work please?


  • Qt Champions 2016

    @tomy said in Adding more actions to the context menu of a spreadsheet app:

    error: no match for 'operator+=' (operand types are 'double' and 'QTableWidgetItem')
    d += item;
    error: no match for 'operator+=' (operand types are 'double' and 'QTableWidgetItem')
    d1 += item;

    You try to add a QTableWidgetItem to a double, which makes little sense
    Why did you mean ?
    something
    like d1 += item.text().toDouble(); ( take the text it has and convert to double)



  • Thank you. I did it and it worked.
    Now I should somehow reach one below the last cell in the selected area to put the result into it.
    I found a function named: QString Spreadsheet::currentLocation() const in spreadsheet. And changed sum() to this:

    void Spreadsheet::sum()
    {
        QList<QTableWidgetItem *> items = selectedItems();
        double d = 0;
        if (!items.isEmpty()) {
            foreach (QTableWidgetItem *item, items)
                d += item->text().toDouble();
            somethingChanged();
        }
        QString test = currentLocation();
        test = d;
        currentLocation().append(test);
    }
    

    It's not one below the last cell in the selected area but since I didn't find it, I chose this one to test and see the output.
    The app runs but nothing happens when I select an area and pick sum! :(


  • Qt Champions 2016

    @tomy
    use the debugger
    single step through the function and look at values.
    you can also use qDebug() << "d is:" << d;
    to see what is going on.



  • @mrjj

    you can also use qDebug() << "d is:" << d;

    I used this in the function and ran the app, entered 12 in a cell and 14 in another cell below it. Then selected both and clicked on sum. The debugger showed this:
    d is: 26

    So it's OK, and I only need to put the value into a cell below the last one.



  • No solution to get the function to work?!


  • Qt Champions 2016

    @tomy
    Nope, it is you app and you must know how to access its data,
    however, you can access other items with
    QTableWidgetItem * item(int row, int column) const
    you can know what row an item has
    http://doc.qt.io/qt-5/qtablewidget.html#row
    So in that way you can calculate and access the row for the sum
    Take the last in items and get row then you can get "the row after"
    make sure to handle special case, where last selected is also last in the list.



  • @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 and j 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 using QTableWidget::setItem(i, j, item);


  • Qt Champions 2016

    @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#setText

    something 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,
    @mrjj

    item->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. :)


  • Qt Champions 2016

    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 and Count 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.


  • Qt Champions 2016

    @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();



  • @mrjj

    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.


  • Qt Champions 2016

    @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 better

    and yes it was copy paste error.
    its last->row/col
    as you found out.



  • Thank you very much. :) ;)


  • Qt Champions 2016

    Good work.
    If possible, please mark as solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.