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. Can not read from QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Can not read from QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 3.0k 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.
  • JonBJ JonB

    @Raul200000000009
    You don't have anything in your table widget so the condition never returns true.
    Your code "crashes" on the last iteration with an "index out of bounds".

    R Offline
    R Offline
    Raul200000000009
    wrote on last edited by
    #3

    @JonB The tableWidget reads data from an file,wich is not empty.

    JonBJ 1 Reply Last reply
    0
    • R Raul200000000009

      @JonB The tableWidget reads data from an file,wich is not empty.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @Raul200000000009 Your code makes no sense and you haven't asked a question or explained what the problem is.

      R 1 Reply Last reply
      1
      • JonBJ JonB

        @Raul200000000009 Your code makes no sense and you haven't asked a question or explained what the problem is.

        R Offline
        R Offline
        Raul200000000009
        wrote on last edited by Raul200000000009
        #5

        @JonB Here is the update version of the code.It crashes.
        The code is reading from an .xml file.
        int x=0;
        bool prim=false;
        QString a= " text";
        QTableWidgetItem *item1(table->item(i+1,0));
        for( i=0;i<table->rowCount();++i){
        if (a==item1->text()) {
        prim=true;
        table->insertRow(i+1);
        QTableWidgetItem *item2(table->item(i+1,0));
        table->setItem(i+1,0,new QTableWidgetItem (" <another text"));
        }
        if(prim!=true)
        {
        ;
        }}

        Christian EhrlicherC D 2 Replies Last reply
        0
        • R Raul200000000009

          @JonB Here is the update version of the code.It crashes.
          The code is reading from an .xml file.
          int x=0;
          bool prim=false;
          QString a= " text";
          QTableWidgetItem *item1(table->item(i+1,0));
          for( i=0;i<table->rowCount();++i){
          if (a==item1->text()) {
          prim=true;
          table->insertRow(i+1);
          QTableWidgetItem *item2(table->item(i+1,0));
          table->setItem(i+1,0,new QTableWidgetItem (" <another text"));
          }
          if(prim!=true)
          {
          ;
          }}

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @Raul200000000009 If it crashes, use a debugger to see where exactly it is crashing.
          Also please properly format your code with the code tags so everyone can read it.

          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
          1
          • R Raul200000000009

            @JonB Here is the update version of the code.It crashes.
            The code is reading from an .xml file.
            int x=0;
            bool prim=false;
            QString a= " text";
            QTableWidgetItem *item1(table->item(i+1,0));
            for( i=0;i<table->rowCount();++i){
            if (a==item1->text()) {
            prim=true;
            table->insertRow(i+1);
            QTableWidgetItem *item2(table->item(i+1,0));
            table->setItem(i+1,0,new QTableWidgetItem (" <another text"));
            }
            if(prim!=true)
            {
            ;
            }}

            D Offline
            D Offline
            DerReisende
            wrote on last edited by
            #7

            @Raul200000000009 check item1 and item2 if they are nullptr…see the doc for item function.

            R 1 Reply Last reply
            0
            • D DerReisende

              @Raul200000000009 check item1 and item2 if they are nullptr…see the doc for item function.

              R Offline
              R Offline
              Raul200000000009
              wrote on last edited by Raul200000000009
              #8

              @DerReisende ,I solved the crashing error,but the code does not work.It should add one "anothertext" row below "text" row if "text" row exists.

              bool prim=false;
              for( i=0;i<table->rowCount();++i){
              if (table->item(i,0)->text()!=" text") {
              prim=true;
              }
              if(prim!=true)
              {
              table->insertRow(i+1);
              table->setItem(i+1,0,new QTableWidgetItem (" anothertext"));
              }}

              SGaistS 1 Reply Last reply
              0
              • R Raul200000000009

                @DerReisende ,I solved the crashing error,but the code does not work.It should add one "anothertext" row below "text" row if "text" row exists.

                bool prim=false;
                for( i=0;i<table->rowCount();++i){
                if (table->item(i,0)->text()!=" text") {
                prim=true;
                }
                if(prim!=true)
                {
                table->insertRow(i+1);
                table->setItem(i+1,0,new QTableWidgetItem (" anothertext"));
                }}

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #9

                Hi,

                What exactly does not work ?
                From the looks of it, your if condition is simply never true. If it should be once then you should really ensure that the string does really exist in your table.

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

                R 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  What exactly does not work ?
                  From the looks of it, your if condition is simply never true. If it should be once then you should really ensure that the string does really exist in your table.

                  R Offline
                  R Offline
                  Raul200000000009
                  wrote on last edited by Raul200000000009
                  #10

                  @SGaist
                  What should happen:
                  The code is reading the string from an file ,and is writing that string in the table.Than writes the new string from the table to the file.
                  What happens:
                  The code does not upgrade the string from the table.
                  So the string from the file is not upgraded.

                  Christian EhrlicherC SGaistS 2 Replies Last reply
                  0
                  • R Raul200000000009

                    @SGaist
                    What should happen:
                    The code is reading the string from an file ,and is writing that string in the table.Than writes the new string from the table to the file.
                    What happens:
                    The code does not upgrade the string from the table.
                    So the string from the file is not upgraded.

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @Raul200000000009 said in Can not read from QTableWidget:

                    The code is reading the string from an file ,and is writing that string in the table.Than writes the new string from the table to the file.

                    Apart from the fact that you still do not properly format your code so that it's readable your code does not read anything from a file nor does it write something out to a file.

                    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
                    0
                    • R Raul200000000009

                      @SGaist
                      What should happen:
                      The code is reading the string from an file ,and is writing that string in the table.Than writes the new string from the table to the file.
                      What happens:
                      The code does not upgrade the string from the table.
                      So the string from the file is not upgraded.

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      Did you check that you are actually entering that loop at least once ?

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

                      R 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Did you check that you are actually entering that loop at least once ?

                        R Offline
                        R Offline
                        Raul200000000009
                        wrote on last edited by
                        #13

                        @SGaist
                        The String exists but qt does not find it .

                        Christian EhrlicherC 1 Reply Last reply
                        0
                        • R Raul200000000009

                          @SGaist
                          The String exists but qt does not find it .

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #14

                          @Raul200000000009 said in Can not read from QTableWidget:

                          The String exists but qt does not find it .

                          If the string " text" exists in the QTableWidget then it would be found. Please provide a minimal, compilable example where you first insert the string " text" and then search for it.

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

                          R 1 Reply Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            @Raul200000000009 said in Can not read from QTableWidget:

                            The String exists but qt does not find it .

                            If the string " text" exists in the QTableWidget then it would be found. Please provide a minimal, compilable example where you first insert the string " text" and then search for it.

                            R Offline
                            R Offline
                            Raul200000000009
                            wrote on last edited by Raul200000000009
                            #15

                            @Christian-Ehrlicher

                            mainwindow.cpp
                            It crashes again.

                            
                            #include "mainwindow.h"
                            #include "ui_mainwindow.h"
                            
                            
                            
                            MainWindow::MainWindow(QWidget *parent)
                                : QMainWindow(parent)
                                , ui(new Ui::MainWindow)
                            {
                                ui->setupUi(this);
                                table=new QTableWidget(this);
                                table->setColumnCount(1);
                                table->insertRow(0);
                                table->insertRow(1);
                                table->setGeometry(200,200,200,200);
                                table->setItem(1,0,new QTableWidgetItem (" text"));
                            
                            }
                            
                            MainWindow::~MainWindow()
                            {
                                delete ui;
                            }
                            
                            
                            void MainWindow::on_pushButton_clicked()
                            {
                            
                                bool prim=false;
                            
                            
                                for(int i=0;i<table->rowCount();++i){
                            
                                    if (table->item(i,0)->text()==" text") {
                                        prim=true;
                            
                                    }
                            
                            
                                    if(prim==false)
                                    {
                                        //table->insertRow(i+1);
                                        table->setItem(i,0,new QTableWidgetItem (" nope"));
                                    }
                                    if(prim==true)
                                    {
                                        table->insertRow(i+1);
                                        table->setItem(i+1,0,new QTableWidgetItem ("another text"));
                                    }
                            
                            
                            
                                }
                            
                            }
                            
                            
                            
                            SGaistS 1 Reply Last reply
                            0
                            • R Raul200000000009

                              @Christian-Ehrlicher

                              mainwindow.cpp
                              It crashes again.

                              
                              #include "mainwindow.h"
                              #include "ui_mainwindow.h"
                              
                              
                              
                              MainWindow::MainWindow(QWidget *parent)
                                  : QMainWindow(parent)
                                  , ui(new Ui::MainWindow)
                              {
                                  ui->setupUi(this);
                                  table=new QTableWidget(this);
                                  table->setColumnCount(1);
                                  table->insertRow(0);
                                  table->insertRow(1);
                                  table->setGeometry(200,200,200,200);
                                  table->setItem(1,0,new QTableWidgetItem (" text"));
                              
                              }
                              
                              MainWindow::~MainWindow()
                              {
                                  delete ui;
                              }
                              
                              
                              void MainWindow::on_pushButton_clicked()
                              {
                              
                                  bool prim=false;
                              
                              
                                  for(int i=0;i<table->rowCount();++i){
                              
                                      if (table->item(i,0)->text()==" text") {
                                          prim=true;
                              
                                      }
                              
                              
                                      if(prim==false)
                                      {
                                          //table->insertRow(i+1);
                                          table->setItem(i,0,new QTableWidgetItem (" nope"));
                                      }
                                      if(prim==true)
                                      {
                                          table->insertRow(i+1);
                                          table->setItem(i+1,0,new QTableWidgetItem ("another text"));
                                      }
                              
                              
                              
                                  }
                              
                              }
                              
                              
                              
                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              Because there's no item in your row 0.

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

                              R 1 Reply Last reply
                              3
                              • SGaistS SGaist

                                Because there's no item in your row 0.

                                R Offline
                                R Offline
                                Raul200000000009
                                wrote on last edited by
                                #17

                                @SGaist The code is working but I need to remove an "invisible" new line character from my text file.Any ideas?

                                Christian EhrlicherC 1 Reply Last reply
                                0
                                • R Raul200000000009 has marked this topic as solved on
                                • R Raul200000000009

                                  @SGaist The code is working but I need to remove an "invisible" new line character from my text file.Any ideas?

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #18

                                  @Raul200000000009 said in Can not read from QTableWidget:

                                  Any ideas?

                                  Take a look into the QString documentation would be a good start.

                                  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
                                  1

                                  • Login

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