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. The sum() action of the spreadsheet app faces problems
Forum Updated to NodeBB v4.3 + New Features

The sum() action of the spreadsheet app faces problems

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 4.7k Views 3 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.
  • T Offline
    T Offline
    tomy
    wrote on 27 Aug 2017, 10:08 last edited by tomy
    #1

    Hi,
    Here is the program and below is the screenshot of the problem:

    0_1503827174256_1.PNG

    I entered two 5 and used the sum action and it showed 10. Then on the next column, I entered two 12 and used sum again. It showed 24. When after that I right clicked on 24 and chose sum again for that cell, it showed that error!

    I'd tested the app in many ways (including this one) with both Release and Debug modes. Everything worked well. I don't know what has happened for the app now!! :(

    For sum():
    I've declared and defined the function sum() in the spreadsheet .h and .cpp files. Below is its implementation:

    void Spreadsheet::pos(QList<QTableWidgetItem *> items, int& i, int& j)
    {
        if(!items.isEmpty()) {
          QTableWidgetItem* last = items.last();
          i = last->row();
          j = last->column();
        }
    }
    
    //****************************************
    
    void Spreadsheet::sum()
    {
        QList<QTableWidgetItem *> items = selectedItems();
        int i, j;
        double d;
    
         if(!items.isEmpty())
            foreach (QTableWidgetItem* item, items)
                d += item->text().toDouble();
    
    
          pos(items, i, j);
          QTableWidgetItem* item = new QTableWidgetItem;
          item->setText((QString::number(d)));
             ++i;
    
            while(QTableWidget::item(i, j))
            i++;
    
            item->setTextAlignment(Qt::AlignRight);
            QTableWidget::setItem(i, j, item);
     }
    

    The other problem is when I run the app using the Release mode, the function sum doesn't even work at all, and just shows trash values!!! :
    Please test it here.

    What is the problem with this sum() function. I've added it to the context menu of the app. I tried to find the issue but couldn't find! :(

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Aug 2017, 10:24 last edited by
      #2

      @tomy said in The sum() action of the spreadsheet app faces problems:

      int i, j;
      double d;

      You do not set a initial value. (zero)

      So when you do
      d += item->text().toDouble();

      it might count funny/randomly.

      T 1 Reply Last reply 27 Aug 2017, 11:26
      1
      • M mrjj
        27 Aug 2017, 10:24

        @tomy said in The sum() action of the spreadsheet app faces problems:

        int i, j;
        double d;

        You do not set a initial value. (zero)

        So when you do
        d += item->text().toDouble();

        it might count funny/randomly.

        T Offline
        T Offline
        tomy
        wrote on 27 Aug 2017, 11:26 last edited by tomy
        #3

        @mrjj

        int i, j;
        double d;

        You do not set a initial value. (zero)

        So when you do
        d += item->text().toDouble();

        it might count funny/randomly.

        I'd previously set them to zero but you asked me these questions:

        What happens if you select nothing and then add sum ?
        Will it not insert 0 anyway ?
        Is that how you want it?

        To be honest, through the C++ book, I've been taught that never leave a variable alone without initializing.

        I modified the code this way:

        bool b = false;
        if(!items.isEmpty()) {
               foreach (QTableWidgetItem* item, items)
                   d += item->text().toDouble();
                 b = true;
               }
        ...
        
        if(b)
               QTableWidget::setItem(i, j, item);
        

        Agree?

        M 1 Reply Last reply 27 Aug 2017, 14:11
        0
        • P Offline
          P Offline
          patrik08
          wrote on 27 Aug 2017, 11:57 last edited by
          #4

          Why you dont use QtXlsxWriter ... this tool calculate read write excel inside all box ... e sum in easy way... this module must take a oscar...

          https://github.com/dbzhang800/QtXlsxWriter

          1 Reply Last reply
          1
          • T Offline
            T Offline
            tomy
            wrote on 27 Aug 2017, 12:42 last edited by
            #5

            Thank you but I've never been in github and can't even use its contents as they are expected.

            1 Reply Last reply
            0
            • T tomy
              27 Aug 2017, 11:26

              @mrjj

              int i, j;
              double d;

              You do not set a initial value. (zero)

              So when you do
              d += item->text().toDouble();

              it might count funny/randomly.

              I'd previously set them to zero but you asked me these questions:

              What happens if you select nothing and then add sum ?
              Will it not insert 0 anyway ?
              Is that how you want it?

              To be honest, through the C++ book, I've been taught that never leave a variable alone without initializing.

              I modified the code this way:

              bool b = false;
              if(!items.isEmpty()) {
                     foreach (QTableWidgetItem* item, items)
                         d += item->text().toDouble();
                       b = true;
                     }
              ...
              
              if(b)
                     QTableWidget::setItem(i, j, item);
              

              Agree?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 27 Aug 2017, 14:11 last edited by
              #6

              @tomy
              What about d ?

              T 1 Reply Last reply 27 Aug 2017, 17:03
              0
              • M mrjj
                27 Aug 2017, 14:11

                @tomy
                What about d ?

                T Offline
                T Offline
                tomy
                wrote on 27 Aug 2017, 17:03 last edited by
                #7

                @mrjj

                What about d ?

                Hi,

                I initialized it to zero.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 27 Aug 2017, 21:52 last edited by
                  #8

                  Hi,

                  What about using the debugger to get a stack trace ?

                  Also, did you check the value of I ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    tomy
                    wrote on 28 Aug 2017, 04:22 last edited by
                    #9

                    I initialized all variables to zero. It works fine now. Thanks.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tomy
                      wrote on 28 Aug 2017, 16:45 last edited by
                      #10

                      One question on graphic visage of the app.
                      Please take a look:

                      0_1503938077733_1.png

                      To me, it looks like a Win 2000 app and old fashioned! People expect more graphical and splendid apps nowadays.
                      I think we can make such new-fashioned apps using Qt, can't we?!

                      I toyed with that and changed two lines of code from Qt_4_3 to Qt_5_9. But no influence.

                      in.setVersion(QDataStream::Qt_4_3);
                      out.setVersion(QDataStream::Qt_4_3);
                      

                      What classes or functions to use to make the app appear new and competitive from the graphics point of view, please?

                      M 1 Reply Last reply 28 Aug 2017, 16:51
                      0
                      • T tomy
                        28 Aug 2017, 16:45

                        One question on graphic visage of the app.
                        Please take a look:

                        0_1503938077733_1.png

                        To me, it looks like a Win 2000 app and old fashioned! People expect more graphical and splendid apps nowadays.
                        I think we can make such new-fashioned apps using Qt, can't we?!

                        I toyed with that and changed two lines of code from Qt_4_3 to Qt_5_9. But no influence.

                        in.setVersion(QDataStream::Qt_4_3);
                        out.setVersion(QDataStream::Qt_4_3);
                        

                        What classes or functions to use to make the app appear new and competitive from the graphics point of view, please?

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 28 Aug 2017, 16:51 last edited by
                        #11

                        @tomy
                        It looks like the OS you are running on.
                        In this case windows 7.
                        It will look as win 10 when running on win 10 and so on.

                        If you want to make other type of look, you can use stylesheets or make a completly new
                        QStyle class for it. In any way, its a lot of work.

                        T 1 Reply Last reply 28 Aug 2017, 20:27
                        1
                        • M mrjj
                          28 Aug 2017, 16:51

                          @tomy
                          It looks like the OS you are running on.
                          In this case windows 7.
                          It will look as win 10 when running on win 10 and so on.

                          If you want to make other type of look, you can use stylesheets or make a completly new
                          QStyle class for it. In any way, its a lot of work.

                          T Offline
                          T Offline
                          tomy
                          wrote on 28 Aug 2017, 20:27 last edited by
                          #12

                          @mrjj

                          If you want to make other type of look, you can use stylesheets or make a completly new
                          QStyle class for it. In any way, its a lot of work.

                          Do you think it's because of the old-fashioned book I use (C++-GUI-Programming-with-Qt-4-2ndEdition) or the look is the basic and I will use the new ones when I get further in the book?

                          M 1 Reply Last reply 28 Aug 2017, 21:34
                          0
                          • T tomy
                            28 Aug 2017, 20:27

                            @mrjj

                            If you want to make other type of look, you can use stylesheets or make a completly new
                            QStyle class for it. In any way, its a lot of work.

                            Do you think it's because of the old-fashioned book I use (C++-GUI-Programming-with-Qt-4-2ndEdition) or the look is the basic and I will use the new ones when I get further in the book?

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 28 Aug 2017, 21:34 last edited by
                            #13

                            @tomy
                            Nope, its not related to the age of the book and it will look the same also later on.
                            Its how widget application looks. unless you make your own style.
                            It looks like all the other win 7 apps which is by design.

                            T 1 Reply Last reply 29 Aug 2017, 08:04
                            2
                            • M mrjj
                              28 Aug 2017, 21:34

                              @tomy
                              Nope, its not related to the age of the book and it will look the same also later on.
                              Its how widget application looks. unless you make your own style.
                              It looks like all the other win 7 apps which is by design.

                              T Offline
                              T Offline
                              tomy
                              wrote on 29 Aug 2017, 08:04 last edited by
                              #14

                              @mrjj

                              Its how widget application looks. unless you make your own style.

                              What do you mean by that, please? do you mean I use some other classes for my app?
                              What minor changes I can do to my app to look a little nicer?

                              It looks like all the other win 7 apps which is by design.

                              Hmm, but my prior app (Calculator) looks more graphical and resembles nowadays Wind 7/10 app!

                              jsulmJ 1 Reply Last reply 29 Aug 2017, 08:39
                              0
                              • T tomy
                                29 Aug 2017, 08:04

                                @mrjj

                                Its how widget application looks. unless you make your own style.

                                What do you mean by that, please? do you mean I use some other classes for my app?
                                What minor changes I can do to my app to look a little nicer?

                                It looks like all the other win 7 apps which is by design.

                                Hmm, but my prior app (Calculator) looks more graphical and resembles nowadays Wind 7/10 app!

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 29 Aug 2017, 08:39 last edited by
                                #15

                                @tomy A Qt app usually looks like any other app on the system where it is executed. On Windows 7 it looks like Windows 7 applications, on Windows 10 it looks like a Windows 10 application. Not sure why you bother about this while reading the book.
                                If you want your app to look differently then you can use stylesheets: http://doc.qt.io/qt-4.8/stylesheet-examples.html

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                M 1 Reply Last reply 29 Aug 2017, 10:30
                                2
                                • jsulmJ jsulm
                                  29 Aug 2017, 08:39

                                  @tomy A Qt app usually looks like any other app on the system where it is executed. On Windows 7 it looks like Windows 7 applications, on Windows 10 it looks like a Windows 10 application. Not sure why you bother about this while reading the book.
                                  If you want your app to look differently then you can use stylesheets: http://doc.qt.io/qt-4.8/stylesheet-examples.html

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 29 Aug 2017, 10:30 last edited by
                                  #16
                                  • What minor changes I can do to my app to look a little nicer?
                                    what would make it look nicer?
                                    You can use other icons or colors but overall it will still look like the platform.
                                  T 1 Reply Last reply 29 Aug 2017, 10:48
                                  1
                                  • M mrjj
                                    29 Aug 2017, 10:30
                                    • What minor changes I can do to my app to look a little nicer?
                                      what would make it look nicer?
                                      You can use other icons or colors but overall it will still look like the platform.
                                    T Offline
                                    T Offline
                                    tomy
                                    wrote on 29 Aug 2017, 10:48 last edited by
                                    #17

                                    @mrjj
                                    So the only solution is http://doc.qt.io/qt-4.8/stylesheet-examples.html although I don't know where in the app (the screen, buttons, tables etc), it changes and still needs a lot of work.

                                    Okey. Thank you.

                                    M 1 Reply Last reply 29 Aug 2017, 11:15
                                    0
                                    • T tomy
                                      29 Aug 2017, 10:48

                                      @mrjj
                                      So the only solution is http://doc.qt.io/qt-4.8/stylesheet-examples.html although I don't know where in the app (the screen, buttons, tables etc), it changes and still needs a lot of work.

                                      Okey. Thank you.

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 29 Aug 2017, 11:15 last edited by
                                      #18

                                      @tomy
                                      Yes its lots of work to make new look.

                                      1 Reply Last reply
                                      1

                                      3/18

                                      27 Aug 2017, 11:26

                                      topic:navigator.unread, 15
                                      • Login

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