Results 1 to 4 of 4

Thread: QSqlRelationalTableModel setRelation() unable to select()

  1. #1
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlRelationalTableModel setRelation() unable to select()

    Hi,
    I'm writing a small application with relations between two tables using SQLLITE. The second Table has a relation to the first one, which works fine with model and tableview, but a query to the second table only works if do not use setrelation(). I mnimzed the code as an example and inserted a fixed query that shoud show one line in the second table (and does that if I comment out the line colorModel->setRelation(color_Other, QSqlRelation("other", "id", "name"));.

    What did I wrong with that? Thanks for any hint in advance
    Armin

    mainwindow.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include "mainwindow.h"
    4. MainWindow::MainWindow()
    5. {
    6. createOtherDock();
    7. createColorDock();
    8. }
    9. //---------------------------------------------------------------------------
    10. void MainWindow::createOtherDock()
    11. {
    12. QDockWidget *dock = new QDockWidget(tr("Other"), this);
    13.  
    14. otherModel = new QSqlRelationalTableModel(this);
    15. otherModel->setTable("other");
    16. otherModel->select();
    17.  
    18. otherView = new QTableView(dock);
    19. otherView->setModel(otherModel);
    20.  
    21. dock->setWidget(otherView);
    22. addDockWidget(Qt::RightDockWidgetArea, dock);
    23. }
    24. //---------------------------------------------------------------------------
    25. void MainWindow::createColorDock()
    26. {
    27. QDockWidget *dock = new QDockWidget(tr("Color"), this);
    28.  
    29. colorModel = new QSqlRelationalTableModel(this);
    30. colorModel->setTable("color");
    31. // the following is the line that mixes up the query
    32. colorModel->setRelation(color_Other, QSqlRelation("other", "id", "name"));
    33. colorModel->select();
    34.  
    35. colorView = new QTableView(dock);
    36. colorView->setModel(colorModel);
    37.  
    38. dock->setWidget(colorView);
    39. addDockWidget(Qt::RightDockWidgetArea, dock);
    40. }
    41. //---------------------------------------------------------------------------
    42. void MainWindow::filterColorView()
    43. {
    44. colorModel->setFilter("name like 'Bl%'");
    45. colorModel->select();
    46. colorView->horizontalHeader()->setVisible(colorModel->rowCount() > 0);
    47. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6.  
    7. enum {
    8. other_Id = 0,
    9. other_Name = 1,
    10. };
    11. enum {
    12. color_Id = 0,
    13. color_Name = 1,
    14. color_Other = 2,
    15. };
    16.  
    17. class MainWindow : public QMainWindow
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. MainWindow();
    23. void filterColorView();
    24. private:
    25. void createOtherDock();
    26. void createColorDock();
    27.  
    28.  
    29. QTableView *otherView;
    30. QTableView *colorView;
    31.  
    32. };
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 

    And for convenience if someone wants to compile it, main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QtSql>
    4.  
    5. #include "mainwindow.h"
    6. //---------------------------------------------------------------------------
    7. bool Connect()
    8. {
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    10. db.setDatabaseName("data.dat");
    11. if (!db.open())
    12. {
    13. QMessageBox::warning(0, QObject::tr("Error"), db.lastError().text());
    14. return (false);
    15. }
    16. return (true);
    17. }
    18. //---------------------------------------------------------------------------
    19. void createDataBase()
    20. {
    21. QSqlQuery query;
    22. query.exec("DROP TABLE other");
    23. query.exec("DROP TABLE color");
    24.  
    25. query.exec("CREATE TABLE other ("
    26. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
    27. "name VARCHAR(10) NOT NULL)");
    28.  
    29. query.exec("CREATE TABLE color ("
    30. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
    31. "name VARCHAR(40) NOT NULL, "
    32. "otherid INTEGER NOT NULL, "
    33. "FOREIGN KEY (otherid) REFERENCES other)");
    34.  
    35. query.prepare("INSERT INTO other (id, name) "
    36. "VALUES (:id, :name)");
    37. query.bindValue(":id", 1);
    38. query.bindValue(":name", "A");
    39. query.exec();
    40. query.bindValue(":id", 2);
    41. query.bindValue(":name", "B");
    42. query.exec();
    43.  
    44. query.prepare("INSERT INTO color (id, name, otherid) "
    45. "VALUES (:id, :name, :otherid)");
    46. query.bindValue(":id", 1);
    47. query.bindValue(":name", "Red");
    48. query.bindValue(":otherid", 1);
    49. query.exec();
    50. query.bindValue(":id", 2);
    51. query.bindValue(":name", "Green");
    52. query.bindValue(":otherid", 2);
    53. query.exec();
    54. query.bindValue(":id", 3);
    55. query.bindValue(":name", "Blue");
    56. query.bindValue(":otherid", 1);
    57. query.exec();
    58. }
    59. //---------------------------------------------------------------------------
    60.  
    61. int main(int argc, char *argv[])
    62. {
    63. QApplication app(argc, argv);
    64.  
    65. bool existingData = QFile::exists("data.dat");
    66. if (!Connect())
    67. {
    68. return(1);
    69. }
    70. if (!existingData)
    71. {
    72. createDataBase();
    73. }
    74. MainWindow Window;
    75. Window.show();
    76. Window.filterColorView();
    77. return app.exec();
    78. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSqlRelationalTableModel setRelation() unable to select()

    Use "table_name.column_name" in your filter.

  3. The following 2 users say thank you to norobro for this useful post:

    Armin (1st February 2010), kenez (16th December 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlRelationalTableModel setRelation() unable to select()

    Thanks a lot norobro, now it works

  5. #4
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlRelationalTableModel setRelation() unable to select()

    Thank you very much norobro, your post was very helpful!

Similar Threads

  1. setRelation and NULL in ID
    By Auryn in forum Qt Programming
    Replies: 4
    Last Post: 26th May 2015, 16:20
  2. Unable to modify QSqlRecord in QSqlRelationalTableModel
    By pippo42 in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2010, 23:17
  3. QSqlRelationalTableModel settable unable to find
    By nietzsche in forum Qt Programming
    Replies: 0
    Last Post: 25th May 2009, 22:51
  4. Replies: 1
    Last Post: 7th June 2008, 18:29
  5. Unable to fetch data when searching through select query
    By sinha.ashish in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2008, 14:06

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
  •  
Qt is a trademark of The Qt Company.