Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding more actions to the context menu of a spreadsheet app
Forum Updated to NodeBB v4.3 + New Features

Adding more actions to the context menu of a spreadsheet app

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 2 Posters 4.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM mrjj

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

    tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #5

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

    1 Reply Last reply
    1
    • tomyT Offline
      tomyT Offline
      tomy
      wrote on last edited by
      #6

      No solution to get the function to work?!

      mrjjM 1 Reply Last reply
      0
      • tomyT tomy

        No solution to get the function to work?!

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #7

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

        tomyT 1 Reply Last reply
        2
        • mrjjM mrjj

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

          tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by
          #8

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

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #9

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

            tomyT 1 Reply Last reply
            1
            • mrjjM mrjj

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

              tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by
              #10

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

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #11

                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?

                tomyT 1 Reply Last reply
                1
                • tomyT Offline
                  tomyT Offline
                  tomy
                  wrote on last edited by
                  #12
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    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?

                    tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by tomy
                    #13

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

                    mrjjM 1 Reply Last reply
                    0
                    • tomyT tomy

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

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

                      tomyT 1 Reply Last reply
                      0
                      • mrjjM mrjj

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

                        tomyT Offline
                        tomyT Offline
                        tomy
                        wrote on last edited by
                        #15

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

                        mrjjM 1 Reply Last reply
                        0
                        • tomyT tomy

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

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

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

                          1 Reply Last reply
                          1
                          • tomyT Offline
                            tomyT Offline
                            tomy
                            wrote on last edited by
                            #17

                            Thank you very much. :) ;)

                            1 Reply Last reply
                            2
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #18

                              Good work.
                              If possible, please mark as solved.

                              1 Reply Last reply
                              2

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved