Important: Please read the Qt Code of Conduct -

Autoupdate cell content

  • Hello

    I have created a tablewidget where values in rows are set from Qlineedit by pressing a button, but, once app is open everything works well, what if I edit content into a cell and I wanna it to be updated.

    For instance, I first introduced in a cell a value "John" and then when app is running I change it to "Phil", it changes (on view) but it really doesnt, is it possible to make it change dynamically when app is running? I guess that I must do a Qevent to do it, but dont know exactly if it is correct nor how to do it.

    Could you please help me?

    Thanks in advance

  • Lifetime Qt Champion


    Can you show the code you are using for that ?

  • #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = nullptr);
        QString name()const;
        int age()const;
        double salary()const;
    private slots:
        void on_add_clicked();
        Ui::MainWindow *ui;
    #endif // MAINWINDOW_H


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        QStringList titles;
        delete ui;
    void MainWindow::on_add_clicked()
        ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,0,new QTableWidgetItem(name()));
        ui->show_name->setText(ui->tableWidget->item(0,0)->text()) ;
    QString MainWindow::nombre()const{
        return ui->name->text();
    int MainWindow::edad()const{
        return ui->age->value();
    double MainWindow::salario()const{
        return ui->salary->value();

  • Lifetime Qt Champion

    There's no need for qApp->processEvents(); in your constructor.

    Can you explain the exact steps you are using and what you are expecting from your widget ?

  • I run the app, then I introduce a name and it is automatically shown in cell on tablewidget (in this case position 0,0), and this value is shown in a label. This works as I want.

    Then I do double click on cell 0,0 and edit it (while app running) and press enter, then, the label doesnt change to its new value, what I wish is to edit this cell while app is running by editing it manually by double click and writing on that cell.

    Thank you!

  • Lifetime Qt Champion

    There's no reason for it to change. There's no connection between your QTableWidget and QLineEdit.

  • Yes, I know this code will never do what I want, but how could I do it?


  • Lifetime Qt Champion

    You can connect
    to a slot / lambda and when its called, take it items text and
    set it on the label.

    connect(ui->tableWidget, &QTableWidget::itemChanged, this, &MainWindow::updateLabel);
    (in ctor)

    then define slot in .h

    class MainWindow : public QMainWindow
    private slots:
        void updateLabel(QTableWidgetItem *item);

    and in .cpp

    void MainWindow::updateLabel(QTableWidgetItem *item)
        if (item) {        

    this will update label with any cell so you might want to add a check with
    item->column() to only do it for some cells.

  • Fixed,
    Thank you very much!

Log in to reply