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. Autoupdate cell content
Qt 6.11 is out! See what's new in the release blog

Autoupdate cell content

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.9k Views 2 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.
  • J Offline
    J Offline
    jss193
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show the code you are using for that ?

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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jss193
        wrote on last edited by
        #3
        #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();
        }
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

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

          1 Reply Last reply
          1
          • J Offline
            J Offline
            jss193
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

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

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jss193
                wrote on last edited by
                #7

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

                Thanks

                mrjjM 1 Reply Last reply
                0
                • J jss193

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

                  Thanks

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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.

                  1 Reply Last reply
                  2
                  • J Offline
                    J Offline
                    jss193
                    wrote on last edited by
                    #9

                    Fixed,
                    Thank you very much!

                    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