Results 1 to 12 of 12

Thread: QSqlTableModel/QSqlQueryModel

  1. #1
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSqlTableModel/QSqlQueryModel

    Hello everyone,

    I,am newbie,

    I have a MainWindow with several lineEdits calling: leMateriaal, leDin_norm etc..
    I have a searchDialog with comboboxen calling: materiaalComboBox, dinCombobox etc..
    I have a connection with a extern database with several rows of input.
    If i pop-up my searchDialog in the MainWindow en choose in the combox of the searchDialog the currentText() en push the button ok the seachDialog must be searching in the database for the chosen items en show them in the MainWindow lineEdits.
    The problem is i try several options but nothing works.

    The first option was QSqlTableModel:
    Qt Code:
    1. void MainWindow::search()
    2. {
    3. searchDialog dlg(this)
    4. QString mat = dlg.leMateriaalComboBox->currentText()
    5. QString din = dlg.leDinComboBox->currentText()
    6. if(dlg.exec() == QDialog::Accepted) {
    7.  
    8. QSqltableModel model;
    9. model.setTable("frezen");
    10. model.select();
    11. QString mat = model.record(1).value("Materiaal").toString();
    12. leMateriaal->setText(mat);
    13. QString din = model.record(1).value("Din_norm").toString();
    14. leDin->setText(din);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    In the MainWindow i only see the first record not the the item i choose in my searchDialog combobox.
    My second option was the same with QSqlQueryModel.

    Is it possible when i choose items in the comboboxen in the searchDialog then searchDialog searching in the database en put the items in my MainWindow lineEdits.
    Is something missing in my code?
    Last edited by jacek; 10th December 2006 at 19:55. Reason: changed [html] to [code]

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel/QSqlQueryModel

    Use QSqlTableModel::setFilter() to fetch only the row you want.

  3. #3
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel/QSqlQueryModel

    I have try setFilter in my code like this:

    Qt Code:
    1. QSqltableModel model;
    2. model.setTable("frezen");
    3. model.setFilter("Materiaal = 'Koolstofstaal'");
    4. model.select();
    5.  
    6. for (int i = 0; i < model.rowCount(); ++i) {
    7. QString mat = model.record(i).value("Materiaal").toString();
    8. leMateriaal->setText(mat);
    9. }
    To copy to clipboard, switch view to plain text mode 

    If i read the documentation about setFilter in QSqlTableModel he only accepts one String &filter ?
    In my materiaalCombobox i have 5 different items.
    Example:
    Koolstofstaal, Gelegeerdstaal, Gereedschapstaal etc..
    Is possible for setFilter to take more dan one String &filter.
    Last edited by jacek; 11th December 2006 at 19:51. Reason: changed [html] to [code]

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel/QSqlQueryModel

    Quote Originally Posted by dragon View Post
    Is possible for setFilter to take more dan one String &filter.
    Yes, the filter is exactly the same string which you would place after WHERE in SQL query.

  5. #5
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel/QSqlQueryModel

    Thanks jacek for you answer.

  6. #6
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel/QSqlQueryModel

    I have one thing about setFilter.
    Normally the use of setFilter is this: model.setFilter("Materiaal = 'Koolstofstaal'"); and this works fine.
    Have anybody try this with a bigger string:
    model.setFilter("Materiaal = 'Koolstofstaal' and Materiaal = 'Gelegeerdstaal' and Materiaal = 'Gereeedshapstaal'");
    If i try this there nothing showing up in my lineEdits.
    I want put this strings together to make a choice of a item in my combobox.
    thanks in advance.

  7. #7
    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: QSqlTableModel/QSqlQueryModel

    Try "or" instead of "and"...
    J-P Nurmi

  8. #8
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel/QSqlQueryModel

    I have tried model.setFilter("Materiaal = 'Koolstofstaal' or Materiaal = 'Gelegeerdstaal'or Materiaal = 'Gereedschapstaal'");
    With no succes it shows only the first string Materiaal = 'Koolstofstaal'

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel/QSqlQueryModel

    What value does model.rowCount() return after setFilter() and select()?

  10. #10
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel/QSqlQueryModel

    This a part of the code.
    void MainWindow::search()
    {
    searchDialog dlg(this)
    QString mat = dlg.leMateriaalComboBox->currentText()
    if(dlg.exec() == QDialog::Accepted) {

    QSqltableModel model;
    model.setTable("frezen");
    model.setFilter("Materiaal ='Koolstofstaal' or Materiaal = 'Gelegeerdstaal' or Materiaal = 'Gereedschapstaal'");
    model.select();

    for (int i = 0; i < model.rowCount(); ++i) {
    mat = model.record(i).value("Materiaal").toString();
    leMateriaal->setText(mat);
    }
    }
    The value from model.rowCount() is only the first part of the setFilter string ( Koolstofstaal ) the orther value's doesn't show up in the lineEdit in Mainwindow.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel/QSqlQueryModel

    Quote Originally Posted by dragon View Post
    model.setFilter("Materiaal ='Koolstofstaal' or Materiaal = 'Gelegeerdstaal' or Materiaal = 'Gereedschapstaal'");
    ...
    mat = model.record(i).value("Materiaal").toString();
    Do you really need a model for this? It seems you know a priori what values you want to display (i.e. "Koolstofstaal", "Gelegeerdstaal" and "Gereedschapstaal").

    Quote Originally Posted by dragon View Post
    the orther value's doesn't show up in the lineEdit in Mainwindow.
    QLineEdit::setText() sets the text --- it doesn't append it.
    Qt Code:
    1. lineEdit->setText( "aaa" ); // after this line lineEdit will contain "aaa"
    2. lineEdit->setText( "bbb" ); // now "aaa" is replaced by "bbb"
    To copy to clipboard, switch view to plain text mode 
    If you want to display several values, concatenate them.

    But I think that you would rather want to use a QComboBox instead of QLineEdit.

  12. #12
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel/QSqlQueryModel

    Yes i want to use in searchDialog the combobox.
    Maby i chance the code for this program.
    Thanks jacek voor your time and answers.

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.