Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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

    Hi,

    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
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
        QString name()const;
        int age()const;
        double salary()const;
    
    private slots:
        void on_add_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    and

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QStringList titles;
       
        ui->tableWidget->setColumnCount(3);
        titulos<<"name"<<"age"<<"salary";
        ui->tableWidget->setHorizontalHeaderLabels(titles);
    
        qApp->processEvents();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_add_clicked()
    {
    
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());
        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?

    Thanks


  • Lifetime Qt Champion

    @jss193
    Hi
    You can connect
    https://doc.qt.io/qt-5/qlistwidget.html#itemChanged
    https://doc.qt.io/qt-5/qtablewidget.html#itemChanged
    to a slot / lambda and when its called, take it items text and
    set it on the label.

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

    then define slot in .h

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

    and in .cpp

    void MainWindow::updateLabel(QTableWidgetItem *item)
    {
        if (item) {        
            ui->label->setText(item->text());
        }
    }
    

    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