Getting changed values of a QTablewidget into a QStringList
-
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?
-
@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?
-
@Charlie_Hdz It automatically when you ight click the tablewidget and select slots there no need a signal
-
Unfortunately I think this is a limitation of uic.
ui->setupUi(this);
will do the connection toon_tableWidget_cellChanged
and then it gets called when you doui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i)));
Just renameon_tableWidget_cellChanged
toonCellChanged
and callQObject::connect(ui->tableWidget,&QTableWidget::cellChanged,this,&MainWindow::onCellChanged);
at the end of the constructorP.S.
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i)));
this is just badQTableWidgetItem* const newItem= new QTableWidgetItem; newItem->setData(Qt::EditRole,i); ui->tableWidget->setItem(i,0,newItem);
-
#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); }
-
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(); }