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. tableView update content

tableView update content

Scheduled Pinned Locked Moved General and Desktop
tableviewupdatesignalslot
11 Posts 3 Posters 5.8k 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.
  • C Offline
    C Offline
    cpuin
    wrote on 24 Jun 2015, 09:12 last edited by koahnig
    #1

    I have a tablewView.I also have a dialog which parent is the QWidgets containing the tableView.

    When enter a data in the dialog and click a button "ADD" i insert new row in the SQLite db.This db is connected to the tableView.I need to update also the tableView.

    This is the dialog:

    #include "partnersadddialog.h"
    #include "ui_partnersadddialog.h"
    #include <QSqlQuery>
    #include <QSqlQueryModel>
    #include  <QSqlTableModel>
    #include <QSqlError>
    
    PartnersAddDialog::PartnersAddDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::PartnersAddDialog)
    {
        ui->setupUi(this);
    }
    
    PartnersAddDialog::~PartnersAddDialog()
    {
        delete ui;
    }
    
    void PartnersAddDialog::on_pushButton_add_clicked()
    {
    
        QSqlQuery query;
        query.prepare("INSERT INTO partners (Name, City, Address, MOL, Bulstat, VAT, Phone, eMail)"
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
        query.addBindValue(ui->lineEdit_name->text());
        query.addBindValue(ui->lineEdit_city->text());
        query.addBindValue(ui->lineEdit_address->text());
        query.addBindValue(ui->lineEdit_mol->text());
        query.addBindValue(ui->lineEdit_bulstat->text());
        query.addBindValue(ui->lineEdit_vat->text());
        query.addBindValue(ui->lineEdit_tel->text());
        query.addBindValue(ui->lineEdit_mail->text());
    
        query.exec();
        emit updateTable();
        this->close();
    
    }
    
    void PartnersAddDialog::on_pushButton_cancel_clicked()
    {
        this->close();
    }
    
    This is the QWidget with the table:
    
    Partners::Partners(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Partners)
    {
        ui->setupUi(this);
    
        QSqlQueryModel *model = new QSqlQueryModel;
    
            model->setQuery("SELECT Name, City, Bulstat FROM partners");
            model->setHeaderData(0, Qt::Horizontal, QObject::tr("Име"));
            model->setHeaderData(1, Qt::Horizontal, QObject::tr("Град"));
            model->setHeaderData(2, Qt::Horizontal, QObject::tr("ЕИК:"));
    
            ui->tableView_partners->setModel(model);
            ui->tableView_partners->setColumnWidth(0,299);
            ui->tableView_partners->setColumnWidth(1,225);
            ui->tableView_partners->setColumnWidth(2,225);
            ui->tableView_partners->horizontalHeader()->sectionResizeMode(QHeaderView::Interactive);
            ui->tableView_partners->horizontalHeader()->setStretchLastSection(true);
    
    }
    
    Partners::~Partners()
    {
        delete ui;
    }
    
    void Partners::on_pushButton_new_clicked()
    {
        PartnersAddDialog *d = new PartnersAddDialog(this);
        d->show();
    }
    
    void Partners::updateTable()
    {
    
        ui->tableView_partners->repaint();
        qDebug("called");
    
    }
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on 24 Jun 2015, 10:41 last edited by
      #2

      Hi and welcome to devnet

      Do you have an event loop running?
      AFAIK the update should be automatically then. Anyway you can call the widget update, but this requires also that an event loop is running.

      PS: I have added markdown tags in your code section. This makes the code more readable. in the forum. You find the major tags at the end of this page.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cpuin
        wrote on 24 Jun 2015, 13:56 last edited by
        #3

        Thank you for your answer.
        i ran this.update(); but this update the Dialog.
        I need to update the tableView which belongs to the parent.
        Due to the event loop, my main.cpp looks like this

        QApplication a(argc, argv);

        InvoicePlusPlus w;
        
        QDesktopWidget *desktop = QApplication::desktop();
        
        screenWidth = desktop->width();
        screenHeight = desktop->height();
        
        x = (screenWidth - WIDTH) / 2;
        y = (screenHeight - HEIGHT) / 2;
        
        w.resize(WIDTH, HEIGHT);
        w.move( x, y );
        w.setWindowTitle("Invoice++");
        w.show();
        
        return a.exec();
        
        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on 24 Jun 2015, 14:20 last edited by
          #4

          The event loop shall be fine then.

          I never worked with SqlDatabase and model view, so I am not sure if you need to pull the information from the DB again.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 24 Jun 2015, 20:58 last edited by
            #5

            Hi and welcome to devnet,

            Since you are doing things low-level, then yes you have to refresh your model. Why not use QSqlTableModel to handle that ? You can hide the columns you don't want do see and do your update from it. That way both your views and your database will be up to date.

            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
            • C Offline
              C Offline
              cpuin
              wrote on 24 Jun 2015, 21:06 last edited by
              #6

              In my case, how to update the model?If you thing that the model should be updated, no the table.
              When i click another tab from my app and get back the table has being updated.It means that the model is updated, but the tableView didn't.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 24 Jun 2015, 21:30 last edited by
                #7

                If you want to update the model directly, you have to re-set the query on it. Again, since you are modifying your table, QSqlTableModel is better suited.

                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
                • C Offline
                  C Offline
                  cpuin
                  wrote on 24 Jun 2015, 21:34 last edited by cpuin
                  #8

                  I switched to QSqlTableModel, it doesn't update by itself.How to do it?
                  Also, can i set proxy to QSqlTableModel for filtering the result?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 24 Jun 2015, 21:48 last edited by
                    #9

                    If you are still using QSqlQuery to update the database, you have to call select on the model

                    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
                    • C Offline
                      C Offline
                      cpuin
                      wrote on 25 Jun 2015, 08:24 last edited by
                      #10

                      @cpuin said:

                      QSqlTableModel

                      i switched to QSqlTableModel.Unfortunatley the tableView doesn't update by it's self.
                      Do i have to update the model or the view.As i see the model is updated because when click another tab and get back the new record appears in the table.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 25 Jun 2015, 22:08 last edited by
                        #11

                        Since you switched to QSqlTableModel, are you using it's functions to add new rows ?

                        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

                        4/11

                        24 Jun 2015, 14:20

                        7 unread
                        • Login

                        • Login or register to search.
                        4 out of 11
                        • First post
                          4/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved