Results 1 to 13 of 13

Thread: QTableView

  1. #1
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QTableView

    Hello,

    I'm a bit confused after reviewing all examples related to SQL and QTableView.So many models and approaches.
    What i need is very simple: QWidget with QTableView. QDialog with fields and button "add" and SQL table.
    When the user enters in the fields of the dialog data and click on the button "add" the new record to appear in the table.

    I have tried to achieve this firstly with QSqlQueryModel.After that i tried with QSqlTableModel.With both i can't managed to release the update of the tableView.The record is in the DB, but the table doesn't update by it's self or manually with some method.

    I need an advice how to achieve this simple example.
    I also need answers of:
    - Do i have to update the model only and the table will be updated autimaticly.Which model, and which method to use?
    - Do i need to update the tableView after inserted a new record in the DB.Which model, and which method to use?

    THANK YOU IN ADVANCE

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView

    I haven't used the SQL models in read/write mode myself yet, but according to the documentation QSqlQueryModel is read-only by default, only QSqlTableModel supports writing.

    How do you add the data to the table model?
    Do you call submit afterwards?

    Or do you add the data to the database table?
    Do you call select() on the table model afterwards?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView

    partners.cpp
    Qt Code:
    1. Partners::Partners(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Partners)
    4. {
    5. ui->setupUi(this);
    6.  
    7. //QSqlQueryModel *model = new QSqlQueryModel;
    8.  
    9. //model->setQuery("SELECT Name, City, Bulstat FROM partners");
    10. //model->setHeaderData(0, Qt::Horizontal, QObject::tr("Име"));
    11. //model->setHeaderData(1, Qt::Horizontal, QObject::tr("Град"));
    12. //model->setHeaderData(2, Qt::Horizontal, QObject::tr("ЕИК:"));
    13.  
    14. model->setTable("partners");
    15. model->select();
    16. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    17. model->setHeaderData(1, Qt::Horizontal, tr("Име:"));
    18. model->setHeaderData(3, Qt::Horizontal, tr("Град:"));
    19. model->setHeaderData(7, Qt::Horizontal, tr("ЕИК:"));
    20.  
    21. ui->tableView_partners->setModel(model);
    22. ui->tableView_partners->hideColumn(0);
    23. ui->tableView_partners->hideColumn(2);
    24. //ui->tableView_partners->hideColumn(3);
    25. ui->tableView_partners->hideColumn(4);
    26. ui->tableView_partners->hideColumn(5);
    27. ui->tableView_partners->hideColumn(6);
    28. //ui->tableView_partners->hideColumn(7);
    29. ui->tableView_partners->hideColumn(8);
    30. ui->tableView_partners->setColumnWidth(1,333);
    31. ui->tableView_partners->setColumnWidth(3,200);
    32. ui->tableView_partners->setColumnWidth(7,200);
    33. //ui->tableView_partners->horizontalHeader()->sectionResizeMode(QHeaderView::Interactive);
    34. //ui->tableView_partners->horizontalHeader()->setStretchLastSection(true);
    35. ui->tableView_partners->show();
    36.  
    37. }
    38.  
    39. Partners::~Partners()
    40. {
    41. delete ui;
    42. }
    43.  
    44.  
    45. void Partners::updatePartnersTable()
    46. {
    47. qDebug() << "record added";
    48. }
    To copy to clipboard, switch view to plain text mode 





    partnersadddialo.cpp

    Qt Code:
    1. void PartnersAddDialog::on_pushButton_add_clicked()
    2. {
    3.  
    4. QSqlQuery query;
    5. query.prepare("INSERT INTO partners (Name, City, Address, MOL, Bulstat, VAT, Phone, eMail)"
    6. "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    7. query.addBindValue(ui->lineEdit_name->text());
    8. query.addBindValue(ui->lineEdit_city->text());
    9. query.addBindValue(ui->lineEdit_address->text());
    10. query.addBindValue(ui->lineEdit_mol->text());
    11. query.addBindValue(ui->lineEdit_bulstat->text());
    12. query.addBindValue(ui->lineEdit_vat->text());
    13. query.addBindValue(ui->lineEdit_tel->text());
    14. query.addBindValue(ui->lineEdit_mail->text());
    15.  
    16. query.exec();
    17. emit updateTable();
    18. this->close();
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView

    So you are adding to the database table directly.
    You'll need to update the model by letting it fetch data from the database again, i.e. by calling select().

    Cheers,
    _

  5. #5
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView

    Sounds good, but i don't know how to access it in the updateTable() function? or is there any way to have global access to the model?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView

    You have three options:

    1) a member variable holding a pointer to the model
    2) getting the model from the view and casting
    3) connect the updateTable() signal to the model's select() slot

    Cheers,
    _

  7. #7
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView

    1.First one have no idea how to make it?

    2. I tried this
    Qt Code:
    1. ui->tableView_partners->model()->select();
    To copy to clipboard, switch view to plain text mode 
    but it doesn't give me this method select()

    3.connect( how to make it, the same question, how to access the model:

    Qt Code:
    1. connect(d, SIGNAL(updateTable()), model, SLOT(select()));
    To copy to clipboard, switch view to plain text mode 
    It gives me error "undeclared identifier model"

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView

    1) member variables are basic C++ knowledge, should be in any C++ tutorial
    2) cast missing
    3) basic C++ compiler error message: no variable with the name "model" in that context

    Cheers,
    _

  9. #9
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView

    2) what should i cast ? please give an example

    3)
    I created the model like this
    Qt Code:
    1. QSqlTableModel *model = new QSqlTableModel(this);
    To copy to clipboard, switch view to plain text mode 

    why i can't access them in the function thru
    Qt Code:
    1. this->model()
    To copy to clipboard, switch view to plain text mode 

    The most important - is there any way to access the model from everywhere (other class, functions) - global access?
    Last edited by cpuinthebrain; 25th June 2015 at 20:45.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView

    Do you actually know C++?

    this->model()
    Does the class that "this" points to have a method called "model()"? If not, and you do actually know C++, then the answer to the question should be pretty obvious.

  11. #11
    Join Date
    Sep 2012
    Posts
    44
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTableView

    You are right, i need time to switch from Python.

    OK, which is the way to skip the repeating of the creation of QSqlQueryModel twice, but access the model in whole class?

    Qt Code:
    1. Partners::Partners(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Partners)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QSqlQueryModel *model = new QSqlQueryModel(this);
    8.  
    9. model->setQuery("SELECT Name, City, Bulstat FROM partners");
    10. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Име"));
    11. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Град"));
    12. model->setHeaderData(2, Qt::Horizontal, QObject::tr("ЕИК:"));
    13.  
    14. ui->tableView_partners->setModel(model);
    15. ui->tableView_partners->setColumnWidth(0,325);
    16. ui->tableView_partners->setColumnWidth(1,200);
    17. ui->tableView_partners->setColumnWidth(2,200);
    18. ui->tableView_partners->horizontalHeader()->sectionResizeMode(QHeaderView::Interactive);
    19. ui->tableView_partners->show();
    20.  
    21. }
    22.  
    23. Partners::~Partners()
    24. {
    25. delete ui;
    26. }
    27.  
    28.  
    29. void Partners::addPartner()
    30. {
    31. QSqlQuery query;
    32. query.prepare("INSERT INTO partners (Name, City, Address, MOL, Bulstat, VAT, Phone, eMail)"
    33. "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    34. query.addBindValue(1);
    35. query.addBindValue(2);
    36. query.addBindValue(3);
    37. query.addBindValue(4);
    38. query.addBindValue(5);
    39. query.addBindValue(6);
    40. query.addBindValue(7);
    41. query.addBindValue(8);
    42. query.exec();
    43.  
    44. QSqlQueryModel *model = new QSqlQueryModel(this);
    45. model->setQuery("SELECT Name, City, Bulstat FROM partners");
    46. ui->tableView_partners->setModel(model);
    47. qDebug() << "record added";
    48. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView

    Define variable model as a class member not a local variable.

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTableView

    You are right, i need time to switch from Python.
    At least all you will need to worry about is punctuation, and not whether your code is indented properly...

    Define variable model as a class member not a local variable.
    What Lesiok means is define a variable "QSqlQueryModel * model;" as a member of your Partners class. In the constructor, simply say:

    Qt Code:
    1. model = new QSqlQueryModel(this);
    To copy to clipboard, switch view to plain text mode 

    (i.e., omit the type declaration). This will assign the value of the new model instance to the member variable, and more importantly, allow you to access the variable value in other methods of the Partners class. If you need access to this value from outside the partners class, then you can define a member function for Partners that returns the pointer:

    Qt Code:
    1. // Partners.h
    2.  
    3. class Partners : public // whatever
    4. {
    5. // ...
    6. public:
    7. QSqlQueryModel * model() const { return model; }
    8.  
    9. private:
    10. QSqlQueryModel * model;
    11. };
    12.  
    13. // SomeOtherClass.cpp
    14. void SomeOtherClass::useTheModel()
    15. {
    16. // Assumes a SomeOtherClass member variable Partners * thePartners
    17. thePartners->model()->clear( );
    18. // ...
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 26th June 2015 at 19:26.

Similar Threads

  1. set XML tag to QTableView
    By mythili in forum Qt Programming
    Replies: 10
    Last Post: 7th May 2013, 13:43
  2. QTableView and QLineEdit OUTSIDE of QTableView, like Excel
    By JoZCaVaLLo in forum Qt Programming
    Replies: 10
    Last Post: 13th May 2011, 14:20
  3. Get the value from QTableview
    By umulingu in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 10:55
  4. Replies: 2
    Last Post: 26th November 2009, 05:45
  5. QTableView
    By jamesjara in forum Newbie
    Replies: 5
    Last Post: 26th June 2009, 07:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.