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 program crashes suddenly when working with a new feature
Forum Updated to NodeBB v4.3 + New Features

The program crashes suddenly when working with a new feature

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 7 Posters 5.1k 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.
  • Christian EhrlicherC Christian Ehrlicher

    @tomy said in The program crashes suddenly when working with a new feature:

    QTableWidgetItem* item = new QTableWidgetItem;

    This is wrong, you have to use 'Cell' ...

    Cell *Spreadsheet::cell(int row, int column) const
    {
        return static_cast<Cell *>(item(row, column));
    }
    
    tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #18

    @Christian-Ehrlicher

    Do you mean I use sum() this way:

    void Spreadsheet::sum()
    {
        double d = 0.0;
    
        QList<QTableWidgetItem *> items = selectedItems();
    
        if(add(items, d)) {
            int row = 0, column = 0;
            position(items, row, column);
    
            ++row;
            Cell *c = cell(row, column);
            c->setTextAlignment(Qt::AlignRight);
            c->setText((QString::number(d)));
    
            somethingChanged();
        }
    }
    

    It still crashes.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #19

      @tomy said in The program crashes suddenly when working with a new feature:

      It still crashes.

      But this time your debugger will show you where...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      tomyT 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        @tomy said in The program crashes suddenly when working with a new feature:

        It still crashes.

        But this time your debugger will show you where...

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

        @Christian-Ehrlicher
        Right!

        0_1552139876140_4.PNG ![0_1552139887443_5.PNG]

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #21

          So when you go up the backtrace you see that "Cell *c = cell(row, column);" returns a nullptr.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          tomyT 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            So when you go up the backtrace you see that "Cell *c = cell(row, column);" returns a nullptr.

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

            @Christian-Ehrlicher

            So when you go up the backtrace you see that "Cell *c = cell(row, column);" returns a nullptr.

            Since it's empty, yeah?

            So how to get to the next row (which may or may not be empty) to set the result into it, please?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #23

              When there is no object (yet) you should create one, or?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              tomyT 2 Replies Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                When there is no object (yet) you should create one, or?

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

                @Christian-Ehrlicher
                I was doing it by:

                QTableWidgetItem* item = new QTableWidgetItem;

                I don't know why we need: Cell *c = cell(row, column); while it's the last filled cell and we can't increase its row number to get to the next cell to put the result of sum() into it!

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #25

                  I already told you that you don't have to use QTableWidgetItem but Cell since the model is working with Cell objects (which derives from QTableWidgetItem) instead QTableWidgetItem and therefore the static cast does undefined stuff -> c++ basics...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  3
                  • Christian EhrlicherC Christian Ehrlicher

                    When there is no object (yet) you should create one, or?

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

                    @Christian-Ehrlicher

                    When there is no object (yet) you should create one, or?

                    Using this statement: Cell* c = cell(row, column); we can only have c as a pointer to a non-empty cell in the sheet. Moreover, we can't make it point to the next cell as our target cell for putting the result of our function, because the program crashes. So that c is useless.

                    What do you mean by creating an object? And object of what? A cell? If so, how? This way: Cell ce = *cell(row, column);?

                    I really don't understand what your intention is.
                    Isn't there any way to solve this issue?

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #27

                      How to create a new object on the heap: http://www.cplusplus.com/reference/new/operator new/

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      tomyT 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        How to create a new object on the heap: http://www.cplusplus.com/reference/new/operator new/

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

                        @Christian-Ehrlicher

                        You mean new? I've tested it beforehand in this version of sum and already for QTableWidgetItem.

                        void Spreadsheet::sum()
                        {
                            double d = 0.0;
                        
                            QList<QTableWidgetItem *> items = selectedItems();
                        
                            if(add(items, d)) {
                                int row = 0, column = 0;
                                position(items, row, column);
                        
                                row++;
                                
                                Cell* c = new cell(row, column);
                                c->setTextAlignment(Qt::AlignRight);
                                c->setText((QString::number(d)));
                               
                                somethingChanged();
                            }
                        }
                        

                        Neither Cell* c = new cell(row, column); nor Cell* c = new Cell(row, column); works.

                        Found it:

                        void Spreadsheet::sum()
                        {
                            double d = 0.0;
                        
                            QList<QTableWidgetItem *> items = selectedItems();
                        
                            if(add(items, d)) {
                                int row = 0, column = 0;
                                position(items, row, column);
                        
                                row++;
                        
                                Cell* c = new Cell();
                                c->setTextAlignment(Qt::AlignRight);
                                c->setText((QString::number(d)));
                                setItem(row, column, c);
                                somethingChanged();
                            }
                        }
                        

                        And apparently works fine. :)

                        1 Reply Last reply
                        0

                        • Login

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