The sum() action of the spreadsheet app faces problems



  • 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! :(


  • Qt Champions 2016

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



  • @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?



  • 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



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


  • Qt Champions 2016

    @tomy
    What about d ?



  • @mrjj

    What about d ?

    Hi,

    I initialized it to zero.


  • Lifetime Qt Champion

    Hi,

    What about using the debugger to get a stack trace ?

    Also, did you check the value of I ?



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



  • 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?


  • Qt Champions 2016

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



  • @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?


  • Qt Champions 2016

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



  • @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!


  • Moderators

    @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


  • Qt Champions 2016

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


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


  • Qt Champions 2016

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


Log in to reply
 

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