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. Getting changed values of a QTablewidget into a QStringList
QtWS25 Last Chance

Getting changed values of a QTablewidget into a QStringList

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatorqtablewidgetchangeitem
8 Posts 3 Posters 4.9k Views
  • 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.
  • K Offline
    K Offline
    Kushan
    wrote on last edited by A Former User
    #1

    In my qt c++ application I want to get the changed values of a Qtablewidget into a qt stringList! I used the on_tableWidget_cellChanged() method for this! But I get all the values of the table widget into the qstringList

    following is my code
    MainWIndow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
        void on_tableWidget_cellChanged(int row, int column);
    
    
    private:
        QStringList changedValues;
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->tableWidget->setColumnCount(1);
        ui->tableWidget->setRowCount(5);
        for(int i=0;i<5;i++){
            ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i)));
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    
    void MainWindow::on_tableWidget_cellChanged(int row, int column)
    {
    changedValues<<ui->tableWidget->item(row,column)->text();
    
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        QString concat;
        for(int i=0;i<changedValues.size();i++){
            concat+=changedValues[i];
        }
        ui->label->setText(concat);
    
    }
    

    though I changed 0(value of 0th row, 0th column) to 8 and clicked the button it displayed 012348! I want to display only 8 (the new value) how can i achieve this?

    C 1 Reply Last reply
    0
    • K Kushan

      In my qt c++ application I want to get the changed values of a Qtablewidget into a qt stringList! I used the on_tableWidget_cellChanged() method for this! But I get all the values of the table widget into the qstringList

      following is my code
      MainWIndow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      private slots:
          void on_pushButton_clicked();
      
          void on_tableWidget_cellChanged(int row, int column);
      
      
      private:
          QStringList changedValues;
          Ui::MainWindow *ui;
      };
      
      #endif // MAINWINDOW_H
      

      MainWindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          ui->tableWidget->setColumnCount(1);
          ui->tableWidget->setRowCount(5);
          for(int i=0;i<5;i++){
              ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i)));
          }
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      
      void MainWindow::on_tableWidget_cellChanged(int row, int column)
      {
      changedValues<<ui->tableWidget->item(row,column)->text();
      
      }
      
      void MainWindow::on_pushButton_clicked()
      {
          QString concat;
          for(int i=0;i<changedValues.size();i++){
              concat+=changedValues[i];
          }
          ui->label->setText(concat);
      
      }
      

      though I changed 0(value of 0th row, 0th column) to 8 and clicked the button it displayed 012348! I want to display only 8 (the new value) how can i achieve this?

      C Offline
      C Offline
      Charlie_Hdz
      wrote on last edited by
      #2

      @Kushan said in getting changed values of a QTablewidget into a stringList:

      In my qt c++ application I want to get the changed values of a Qtablewidget into a qt stringList! I used the on_tableWidget_cellChanged() method for this! But I get all the values of the table widget into the qstringList

      Which is the signal that is connected to the on_tableWidget_cellChanged() slot?

      Kind Regards,
      Enrique Hernandez
      gearstech.com.mx
      chernandez@gearstech.com.mx

      K 1 Reply Last reply
      1
      • C Charlie_Hdz

        @Kushan said in getting changed values of a QTablewidget into a stringList:

        In my qt c++ application I want to get the changed values of a Qtablewidget into a qt stringList! I used the on_tableWidget_cellChanged() method for this! But I get all the values of the table widget into the qstringList

        Which is the signal that is connected to the on_tableWidget_cellChanged() slot?

        K Offline
        K Offline
        Kushan
        wrote on last edited by
        #3

        @Charlie_Hdz It automatically when you ight click the tablewidget and select slots there no need a signal

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Unfortunately I think this is a limitation of uic.
          ui->setupUi(this); will do the connection to on_tableWidget_cellChanged and then it gets called when you do ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i)));
          Just rename on_tableWidget_cellChanged to onCellChanged and call QObject::connect(ui->tableWidget,&QTableWidget::cellChanged,this,&MainWindow::onCellChanged); at the end of the constructor

          P.S.
          ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i))); this is just bad

          QTableWidgetItem* const newItem= new QTableWidgetItem;
          newItem->setData(Qt::EditRole,i);
          ui->tableWidget->setItem(i,0,newItem);
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          K 1 Reply Last reply
          2
          • VRoninV VRonin

            Unfortunately I think this is a limitation of uic.
            ui->setupUi(this); will do the connection to on_tableWidget_cellChanged and then it gets called when you do ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i)));
            Just rename on_tableWidget_cellChanged to onCellChanged and call QObject::connect(ui->tableWidget,&QTableWidget::cellChanged,this,&MainWindow::onCellChanged); at the end of the constructor

            P.S.
            ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i))); this is just bad

            QTableWidgetItem* const newItem= new QTableWidgetItem;
            newItem->setData(Qt::EditRole,i);
            ui->tableWidget->setItem(i,0,newItem);
            
            K Offline
            K Offline
            Kushan
            wrote on last edited by
            #5

            @VRonin I dont understand what you are saying can u edit in my code and show? its a great help

            1 Reply Last reply
            -1
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6
              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              
              #include <QMainWindow>
              
              namespace Ui {
              class MainWindow;
              }
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  explicit MainWindow(QWidget *parent = 0);
                  ~MainWindow();
              
              private slots:
                  void on_pushButton_clicked();
              
                  void onCellChanged(int row, int column);
              
              
              private:
                  QStringList changedValues;
                  Ui::MainWindow *ui;
              };
              
              #endif // MAINWINDOW_H
              
              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  ui->tableWidget->setColumnCount(1);
                  ui->tableWidget->setRowCount(5);
                  for(int i=0;i<5;i++){
                     QTableWidgetItem* const newItem= new QTableWidgetItem;
              newItem->setData(Qt::EditRole,i);
              ui->tableWidget->setItem(i,0,newItem);
                  }
              QObject::connect(ui->tableWidget,&QTableWidget::cellChanged,this,&MainWindow::onCellChanged);
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              
              
              void MainWindow::onCellChanged(int row, int column)
              {
              changedValues<<ui->tableWidget->item(row,column)->text();
              
              }
              
              void MainWindow::on_pushButton_clicked()
              {
                  QString concat;
                  for(int i=0;i<changedValues.size();i++){
                      concat+=changedValues[i];
                  }
                  ui->label->setText(concat);
              
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              K 1 Reply Last reply
              0
              • VRoninV VRonin
                #ifndef MAINWINDOW_H
                #define MAINWINDOW_H
                
                #include <QMainWindow>
                
                namespace Ui {
                class MainWindow;
                }
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();
                
                private slots:
                    void on_pushButton_clicked();
                
                    void onCellChanged(int row, int column);
                
                
                private:
                    QStringList changedValues;
                    Ui::MainWindow *ui;
                };
                
                #endif // MAINWINDOW_H
                
                #include "mainwindow.h"
                #include "ui_mainwindow.h"
                
                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    ui->setupUi(this);
                    ui->tableWidget->setColumnCount(1);
                    ui->tableWidget->setRowCount(5);
                    for(int i=0;i<5;i++){
                       QTableWidgetItem* const newItem= new QTableWidgetItem;
                newItem->setData(Qt::EditRole,i);
                ui->tableWidget->setItem(i,0,newItem);
                    }
                QObject::connect(ui->tableWidget,&QTableWidget::cellChanged,this,&MainWindow::onCellChanged);
                }
                
                MainWindow::~MainWindow()
                {
                    delete ui;
                }
                
                
                
                void MainWindow::onCellChanged(int row, int column)
                {
                changedValues<<ui->tableWidget->item(row,column)->text();
                
                }
                
                void MainWindow::on_pushButton_clicked()
                {
                    QString concat;
                    for(int i=0;i<changedValues.size();i++){
                        concat+=changedValues[i];
                    }
                    ui->label->setText(concat);
                
                }
                
                K Offline
                K Offline
                Kushan
                wrote on last edited by
                #7

                @VRonin now I get 0000011111222223333344444488888 :O

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8

                  This is strange. could you try if this snippet works for you?

                  #include <QApplication>
                  #include <QWidget>
                  #include <QVBoxLayout>
                  #include <QPushButton>
                  #include <QTableWidget>
                  #include <QLabel>
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      QWidget mainWid;
                      QStringList changedValues;
                      QVBoxLayout* mainLay=new QVBoxLayout(&mainWid);
                      QTableWidget* tableWidget = new QTableWidget(&mainWid);
                      QPushButton* button = new QPushButton(QStringLiteral("Display list"),&mainWid);
                      QLabel* resultLabel = new QLabel(&mainWid);
                      mainLay->addWidget(tableWidget);
                      mainLay->addWidget(button );
                      mainLay->addWidget(resultLabel );
                      tableWidget->setColumnCount(1);
                      tableWidget->setRowCount(5);
                      for(int i=0;i<5;i++){
                          QTableWidgetItem* const newItem= new QTableWidgetItem;
                          newItem->setData(Qt::EditRole,i);
                          tableWidget->setItem(i,0,newItem);
                      }
                      QObject::connect(tableWidget,&QTableWidget::cellChanged,[&changedValues,tableWidget](int row, int column)->void{changedValues<<tableWidget->item(row,column)->text();});
                      QObject::connect(button ,&QPushButton::clicked,[&changedValues,resultLabel ]()->void{resultLabel->setText(changedValues.join(','));});
                      mainWid.show();
                      return a.exec();
                  
                  }
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  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