Results 1 to 8 of 8

Thread: QSqlQueryModel + set Write

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QSqlQueryModel + set Write

    Hello everybody,

    How can i set a model (QSqlQueryModel) as write only. The model is read-only by default.


    thanks

    Qt Code:
    1. model->setQuery(test);
    2. ui.tableView->setModel(model);
    3. ui.tableView->show();
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSqlQueryModel + set Write

    From docs:
    The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QSqlQueryModel + set Write

    hi jpn..
    I am using QSqlTableModel too, and it works fine. But i would like to change values from a complex query. And i have not found a example how to subclass it
    Should be possible to change values from a query with QSqlQueryModel

    use QSqlTableModel, which provides a read-write model based on a single database table.
    Think DigitalGasoline

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQueryModel + set Write

    Quote Originally Posted by raphaelf
    Hello everybody,
    How can i set a model (QSqlQueryModel) as write only. The model is read-only by default.

    thanks
    Qt Code:
    1. model->setQuery(test);
    2. ui.tableView->setModel(model);
    3. ui.tableView->show();
    To copy to clipboard, switch view to plain text mode 
    IMO ...
    Same I do not have wasted time to study the model of sql qt ... but they are passed to the heart of sql and I have made a porting qt3 => qt4 from the super sql class from http://sourceforge.net/projects/sqlitebrowser/ the porting work is a week-end ....
    the study of model is longer....
    Have a look on http://sourceforge.net/forum/forum.p...orum_id=301849 now i undestand / learning sql heart and can display data on table forms tree ecc.... and i have a StaticLibs to not depend on dll or plug-in....

  5. #5
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QSqlQueryModel + set Write

    Hi Patrik08,
    Thanks for your post, but i would like to know if it is possible to set QSqlQueryModel to write Mode. The Docs say that is possible by Subclassing it, but i have not much experience on qt.
    So have somebody experience with QsqlQueryModel and write Mode..
    Think DigitalGasoline

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSqlQueryModel + set Write

    Quote Originally Posted by raphaelf
    The Docs say that is possible by Subclassing it, but i have not much experience on qt.
    No offense but subclassing doesn't require any Qt experience.. only a bit of C++ knowledge.
    J-P Nurmi

  7. #7
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQueryModel + set Write

    offense no .... my projekt http://sourceforge.net/projects/qtexcel-xslt/ work on the default qt4 sql model looks so... and put table on tabwidged... 1 or moore...

    But i not like long wrapper ... my goal is to make raod fast and simple from db to table...
    often i show db in a form .. not on table method ... whit method...

    textList= this->findChildren<QLineEdit *>();
    boxList= this->findChildren<QComboBox *>();

    http://www.qtforum.de/forum/viewtopic.php?t=2192


    Qt Code:
    1. #include "include_table.h"
    2.  
    3.  
    4. Include_Table::Include_Table( QWidget* parent )
    5. : QWidget( parent )
    6. {
    7. setupUi( this );
    8. noloop = 100;
    9. connect(insert_row, SIGNAL(clicked()), this , SLOT(AppendRow()));
    10. connect(create_index_button, SIGNAL(clicked()), this , SLOT(CreateIndexTable()));
    11.  
    12. }
    13.  
    14. /* line_table_name->setText( tablesql ); */
    15. void Include_Table::AppendRow()
    16. {
    17. QSqlQuery query;
    18. QString rowone = "NULL";
    19. QString line;
    20. int loop;
    21. int loopnewroow;
    22. bool ok = false;
    23. int appendrow = QInputDialog::getInteger(this, tr("New row append on table") + " "+tablename,
    24. tr("Select a nummer:"), 1, 1, 25, 1, &ok);
    25. if (ok) {
    26. for(loopnewroow=0; loopnewroow < appendrow; loopnewroow++) {
    27.  
    28. for(loop=0; loop < summ_cools; loop++) {
    29. if (loop !=summ_cools-1) {
    30. line = line+QString( "%1," ).arg( rowone ); /* construct query */
    31. } else {
    32. line = line+rowone; /* construct query */
    33. }
    34. }
    35.  
    36. line.prepend("INSERT INTO "+tablename+" VALUES ("); /* construct query */
    37. line.append(")"); /* construct query */
    38. query.exec("BEGIN TRANSACTION");
    39. query.exec(line);
    40. query.exec("COMMIT");
    41. loop = 0; /* reset */
    42. line = ""; /* reset */
    43. }
    44. }
    45. RefillTable( tablename );
    46. }
    47. void Include_Table::RefillTable( QString tablesql )
    48. {
    49. noloop = 10;
    50. structuresql = GetStructureTable(tablesql);
    51. bool have_index = structuresql.contains("integer primary key", Qt::CaseInsensitive);
    52. tablename = tablesql; /* save on class !*/
    53. QSqlQuery query;
    54. /* check if as INTEGER PRIMARY KEY */
    55.  
    56. sqlmodel = new QSqlTableModel();
    57. sqlmodel->setTable( tablesql );
    58. sqlmodel->setEditStrategy(QSqlTableModel::OnRowChange);
    59. sqlmodel->select();
    60. tabella_core->setModel(sqlmodel);
    61. tabella_core->setItemDelegate(new QSqlRelationalDelegate(tabella_core));
    62. horizontalheader = new QHeaderView(Qt::Horizontal,this);
    63. horizontalheader = tabella_core->horizontalHeader();
    64. connect (horizontalheader,SIGNAL(sectionClicked(int)),this,SLOT(sortColumn(int)));
    65. if (have_index) {
    66. vertikalheader = new QHeaderView(Qt::Vertical,this);
    67. vertikalheader = tabella_core->verticalHeader();
    68. connect (vertikalheader,SIGNAL( sectionDoubleClicked(int)),this,SLOT(VertikalRow(int)));
    69. }
    70. /* discovery total row/cools register on class ..... start */
    71. summ_rows = sqlmodel->rowCount(); /* save on class !*/
    72. summ_cools = sqlmodel->columnCount(); /* save on class !*/
    73. /* discovery total row/cools ..... end */
    74. if (have_index) {
    75. insert_row->setEnabled(true);
    76. create_index_button->setEnabled(false);
    77. /* debugline->setText( QString( "%1 index ok" ).arg( int2char( summ_cools ) ) ); */
    78. row_total->display( QString( "%1" ).arg( int2char( summ_rows ) ) );
    79. } else {
    80. insert_row->setEnabled(false);
    81. create_index_button->setEnabled(true);
    82. /* debugline->setText( QString( "%1 noindex" ).arg( int2char( summ_cools ) ) ); */
    83. row_total->display( QString( "%1" ).arg( int2char( summ_rows ) ) );
    84. }
    85. }
    86.  
    87.  
    88. void Include_Table::sortColumn(int i)
    89. {
    90. if (i > 0) {
    91. sqlmodel->sort(i,Qt::AscendingOrder);
    92. sqlmodel->sort(i,Qt::AscendingOrder);
    93. }
    94. }
    95.  
    96. void Include_Table::VertikalRow(int i)
    97. { /* Header QMessageBox *msgb; */
    98.  
    99. sqlmodel->revertRow(i); /* not save or update nothing on this row! undo! */
    100. noloop++;
    101. if (noloop == 11) {
    102. noloop = 0;
    103. QString msgDB =tr("<p>Make a selection .. </p><p>You Confirm to remove row:</p><p>Click Ok (remove) button or press Esc.</p>");
    104. int reply = msgb->question(this, tr("Confirm to Delete this row?")+QString( " row nr.%1" ).arg( int2char( i ) ),
    105. msgDB,
    106. msgb->Ok,
    107. msgb->No);
    108. if (reply == 1 and reply !=0x200) { /* 0x200 = ESC doc! */
    109. bool remove_confirm = sqlmodel->removeRows(i,1);
    110. if (!remove_confirm ) {
    111. msgb->information( this , tr("Error delete row!"),tr("Sqlite 3 cant remove this row ")+QString( "%1" ).arg( int2char( i ) ));
    112. }
    113. }
    114. }
    115. noloop = 100;
    116. RefillTable( tablename ); /* set noloop to 10 here ...*/
    117. RefillTable( tablename ); /* set noloop to 10 here ...*/
    118. }
    119.  
    120.  
    121.  
    122. void Include_Table::CreateIndexTable()
    123. {
    124. /* structuresql = GetStructureTable(tablesql); */
    125. QString only_name_one;
    126. QString only_name_all;
    127. QString tmp_table_end = "CREATE TABLE "+tablename;
    128. int numer;
    129. QString struct_table_original = structuresql;
    130. QString table_para = structuresql.replace(tmp_table_end, QString(""));
    131. int resize = table_para.lastIndexOf(")");
    132. table_para = table_para.left(resize); /* remove () */
    133. table_para = table_para.right(resize - 2); /* remove () and space */
    134.  
    135. QString sendtolist = table_para;
    136. sendtolist.replace(QString(" "), QString("#"));
    137. QStringList allnamelist = sendtolist.split(",");
    138.  
    139. for (int i = 0; i < allnamelist.size(); ++i) {
    140. only_name_one = XML_utf8(QString( allnamelist.at(i) ));
    141. numer = only_name_one.lastIndexOf("#");
    142. QString trimm_str = only_name_one.left(numer);
    143. numer = trimm_str.lastIndexOf("#");
    144. trimm_str.left(numer);
    145. trimm_str.replace(QString("#"), QString(""));
    146. if (i !=allnamelist.size() - 1) {
    147. only_name_all = only_name_all+QString( "%1," ).arg( trimm_str );
    148. } else {
    149. only_name_all = only_name_all+QString( "%1" ).arg( trimm_str );
    150. }
    151. numer =0;
    152. only_name_one ="";
    153. trimm_str = "";
    154. }
    155.  
    156. QStringList prepare_query = ( QStringList()
    157. << "CREATE TEMPORARY TABLE TEMP_TABLE ("+only_name_all+")"
    158. << "INSERT INTO TEMP_TABLE SELECT "+only_name_all+" FROM "+tablename
    159. << "DROP TABLE "+tablename
    160. << "CREATE TABLE "+tablename+" (ID INTEGER PRIMARY KEY,"+table_para+")"
    161. << "INSERT INTO "+tablename+"("+only_name_all+") SELECT "+only_name_all+" FROM TEMP_TABLE"
    162. << "DROP TABLE TEMP_TABLE" );
    163. QSqlQuery query;
    164. for (int i = 0; i < prepare_query.size(); ++i) {
    165. query.exec("BEGIN TRANSACTION");
    166. query.exec(QString( prepare_query.at(i) ));
    167. query.exec("COMMIT");
    168. }
    169. /* reload qtableview*/
    170. RefillTable( tablename );
    171. }
    172.  
    173. /*
    174.  result to have a new cool index (id INTEGER PRIMARY KEY) and not restart db!
    175. CREATE TEMPORARY TABLE TEMP_TABLE(Male, Sente);
    176. INSERT INTO TEMP_TABLE SELECT Male, Sente FROM Mustermann;
    177. DROP TABLE Mustermann;
    178. CREATE TABLE Mustermann (id INTEGER PRIMARY KEY, Male TEXT, Sente TEXT);
    179. INSERT INTO Mustermann(Male, Sente) SELECT Male, Sente FROM TEMP_TABLE;
    180. DROP TABLE TEMP_TABLE;
    181. */
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQueryModel + set Write

    Quote Originally Posted by raphaelf
    hi jpn..
    I am using QSqlTableModel too, and it works fine. But i would like to change values from a complex query. And i have not found a example how to subclass it
    Should be possible to change values from a query with QSqlQueryModel

    use QSqlTableModel, which provides a read-write model based on a single database table.
    You have to subclass QSqlQueryModel and reimplement setData(). Just remember that it may be very hard to do it for complex queries and it might prove simpler to subclass QAbstractTableModel and hide the sql layer from the user.

Similar Threads

  1. Using Qt to write Cross Platform Library
    By ball in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2006, 09:58
  2. How can I write vertically in a QPushbutton ?
    By castorvert in forum Qt Programming
    Replies: 1
    Last Post: 2nd April 2006, 21:27
  3. The server cannot write to the client. Why?
    By probine in forum Newbie
    Replies: 12
    Last Post: 24th March 2006, 17:14
  4. Write protecting cells
    By therealjag in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2006, 09:47
  5. Replies: 4
    Last Post: 1st February 2006, 17:17

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.