Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to write integer to QTableWidget
Qt 6.12 LTS is out! See what's new in the release blog

how to write integer to QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.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.
  • L Leopold

    sorry some text is missing:
    here it stops in the qtablewidget.h

    inline void QTableWidgetItem::setText(const QString &atext)
    { setData(Qt::DisplayRole, atext); }
    
    aha_1980A Offline
    aha_1980A Offline
    aha_1980
    Lifetime Qt Champion
    wrote on last edited by
    #3

    Hi @Leopold,

    start the program in a debugger. When it crashes, inpect the stack trace to find the root of the crash.

    Regards

    Qt has to stay free or it will die.

    1 Reply Last reply
    2
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #4

      Hi,

      If there's a segmentation fault then you are accessing something that does not exists. You can use the debugger to find out what is going on.

      I would guess that you are accessing a non existing item.

      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
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #5

        Hi
        do note that ->item(row,col) can return a NULL if no item is assigned and that is
        instant crash.

        You do
        for(int i = 1; i <12; ++i)
        m_ui->average->insertRow(i);}

        But i don't see you assign any QTableWidgetItem and as far as i know insertRow
        inserts empty row so
        m_ui->average->item(5,5) might return NULL;

        1 Reply Last reply
        2
        • L Leopold

          Hello,
          have a new problem.I get some numbers from a QTableWidget.Then I do some mathematics with these and want to write the result to a second QTableWidget.
          Programm does the mathematic but when its up to writing to the second Widget I get a segmentation fault```
          double mbar1=0;
          double druck1=0;
          QString drehz,druck;

          void MainWindow::on_sparkadv_clicked()
          {
          int r=m_ui->logfilewidget->rowCount();
          for(int i = 1; i <12; ++i)
          { m_ui->average->insertRow(i);}
          for (int i = 1; i <r; ++i)
          {

              drehz = m_ui->logfilewidget->item(i,1)->text();
              druck = m_ui->logfilewidget->item(i,6)->text();
          
                      if (drehz<1000)
                           {
                           druck1 =druck1 + druck.toDouble();}
                           }
          
          int durchschnitt1=druck1/r;
          m_ui->average->item(5,5)->setText(QString::number(durchschnitt1).toStdString().c_str());
          

          }

          inline void QTableWidgetItem::setText(const QString &atext)
          { setData(Qt::DisplayRole, atext); }

          Thank you for your help!
          Leopold
          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @Leopold said in how to write integer to QTableWidget:

          @mrjj is fully right with the nullptr. By the way:

          m_ui->average->item(5,5)->setText(QString::number(durchschnitt1).toStdString().c_str());

          can be more simply written as:

          m_ui->average->item(5,5)->setText(QString::number(durchschnitt1));

          Qt has to stay free or it will die.

          L 1 Reply Last reply
          3
          • L Offline
            L Offline
            Leopold
            wrote on last edited by
            #7

            Hi mrjj,
            I had extra inserted this loop, because i thaught .possibly it will not recognice the rows.No help after deleting that row.

            @ SGaist and aha_1980

            yes debuging should resolve that problem.But Iam not used to debugging.I know already that inserting stops in line 188 from qtablewidget.h , thats why I attached the code where it stops. I do not know if "setData(Qt::DisplayRole, atext); " corresponds with
            "setText(QString::number(durchschnitt1).toStdString().c_str()"
            Debugger says "Wert" of atext = "0"

            1 Reply Last reply
            0
            • aha_1980A aha_1980

              @Leopold said in how to write integer to QTableWidget:

              @mrjj is fully right with the nullptr. By the way:

              m_ui->average->item(5,5)->setText(QString::number(durchschnitt1).toStdString().c_str());

              can be more simply written as:

              m_ui->average->item(5,5)->setText(QString::number(durchschnitt1));

              L Offline
              L Offline
              Leopold
              wrote on last edited by
              #8

              @aha_1980

              that was one of my first tries, always segmentation fault.

              aha_1980A L 2 Replies Last reply
              0
              • L Leopold

                @aha_1980

                that was one of my first tries, always segmentation fault.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @Leopold

                But Iam not used to debugging.

                Then learn it. Take the 15 minutes for this video, for example: https://www.youtube.com/watch?v=B7UsWtyhXh4

                that was one of my first tries, always segmentation fault.

                If you use the debugger, it will probably tell you that you access a nullptr.

                AS @mrjj said:

                But i don't see you assign any QTableWidgetItem and as far as i know insertRow
                inserts empty row so
                m_ui->average->item(5,5) might return NULL;

                You need to insert a QTableWidgetItem first.

                Regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                3
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Additional note: QTableWidget::insertRow explicitly state: Inserts an empty row into the table at row. The crucial word here is: empty. There's no item there.

                  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
                  2
                  • L Leopold

                    @aha_1980

                    that was one of my first tries, always segmentation fault.

                    L Offline
                    L Offline
                    Leopold
                    wrote on last edited by
                    #11

                    @Leopold

                    i could figure out that my mathematics does not work, i am trying to solve that problem and will report again!

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      Leopold
                      wrote on last edited by
                      #12

                      Thank you for the help, now its working, i have added a counter for the division and changed the write comand.

                      double mbar1=0;
                      double druck1=0;
                      QString drehz,druck;
                      double durchschnitt1;
                      int durchschnitt;
                      double s=0;
                      void MainWindow::on_sparkadv_clicked()
                      {
                          int r=m_ui->logfilewidget->rowCount();
                      
                               for (int i = 1; i <r; ++i)
                                       {
                                       drehz = m_ui->logfilewidget->item(i,1)->text();
                                       druck = m_ui->logfilewidget->item(i,6)->text();
                      
                                      if (drehz<1000)
                                           {
                                           druck1 = druck1 + druck.toDouble();
                                           s=s+1;
                                           }
                                          }
                                      double durchschnitt1 = druck1 / s;
                                      int durchschnitt=durchschnitt1;
                                
                                 m_ui->average->setItem(5,5,new QTableWidgetItem(QString::number(durchschnitt)));
                            }
                      
                      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
                      • Unsolved