Results 1 to 9 of 9

Thread: Help newbie with QTableView...

  1. #1
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Help newbie with QTableView...

    Hi folks... firstly let me appologize for such a newbie question.

    I am transitioning from C++ Builder on Windows and am struggling a bit with the way things are done in QT.

    I have a simple form with a QTableView on it and want to display some data from my database in the View.

    I have followed some code from "The Book fo QT4" and have the data coming out of the query OK, but the View remains totally empty.

    The code I'm using is as follows

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5.  
    6. QSqlQuery query("select p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid");
    7. QSqlRecord record = query.record();
    8. while(query.next())
    9. {
    10. //loop
    11. QString res;
    12. res = query.value(record.indexOf("sharename")).toString() + " : ";
    13. res += query.value(record.indexOf("totalvalue")).toString();
    14. qDebug() << query.at() << ": " << res;
    15. }
    16.  
    17. QSqlQueryModel queryModel;
    18. queryModel.setQuery("select p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid");
    19. if(queryModel.lastError().isValid())
    20. qDebug() << queryModel.lastError();
    21.  
    22. ui->tableView->setModel(&queryModel);
    23. queryModel.setHeaderData(0, Qt::Horizontal, QObject::tr("heading 1"));
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    I know the query is working because the first loop to qDebug is putting out the correct data.

    Now.. I know I'm not doing this correctly and its going to be stupidly easy.. but.. I simply can't work out how to play with widgets that i've put on the main window using QtCreator's GUI maker (or whatever its called).

    Any advice please??

    Thanks

    Peter Nunn
    Last edited by jpn; 15th February 2009 at 11:54. Reason: changed [quote] to [code] tags

  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: Help newbie with QTableView...

    Hi, allocate the model on the heap with C++ keyword "new" so that it won't go out of the scope immediately after the constructor.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    pnunn (15th February 2009)

  4. #3
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: Help newbie with QTableView...

    JP thankyou so much.... I'm slapping myself in the head.. such an obvious problem..

    Just out of interest.. is the code layout I'm using correct? Should I be doing all of this manipulation in mainwindow.cpp or should I be doing the code in some other module? Just a matter of Style I guess.

    Thanks again.

    Peter.

  5. #4
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help newbie with QTableView...

    Sorry... more lack of understanding...

    I have now progressed a bit with this project.

    I have a comboBox that I'm populating with the share name correctly and have hooked up a signal with the row count from that to a slot for the listview query to try and limit the data being displayed.

    Qt Code:
    1. void MainWindow::updatePriceData(int idx)
    2. {
    3. QSqlRecord record = comboQueryModel->record(idx);
    4. int shareidx = record.value("shareid").toInt();
    5.  
    6. priceQueryModel = new QSqlQueryModel;
    7. query = new QSqlQuery;
    8. query->prepare("select p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid where p.shareid = :shareidx");
    9. query->bindValue(":shareidx", shareidx);
    10. priceQueryModel->setQuery(*query);
    11. if(priceQueryModel->lastError().isValid())
    12. qDebug() << priceQueryModel->lastError();
    13.  
    14. ui->tableView->setModel(priceQueryModel);
    15. priceQueryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("heading 1"));
    16. }
    17.  
    18. void MainWindow::populateGroupBox(void)
    19. {
    20. comboQueryModel = new QSqlQueryModel;
    21. comboQueryModel->setQuery("select shareid, sharename from shareid");
    22. comboQueryModel->removeColumn(0);
    23. ui->comboBox->setModel(comboQueryModel);
    24. }
    To copy to clipboard, switch view to plain text mode 

    I can see the idx value changing correctly (the signal is working) but the shareidx value is always 0.

    What am I missing please??

    Ta

    Peter.

  6. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help newbie with QTableView...

    In line 4, is the attribute shareid ? or shud it be shareidx ??
    just a guess

  7. #6
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help newbie with QTableView...

    No sadly the attribute is shareid (that is the name of the field in the table).

    Peter.

  8. #7
    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: Help newbie with QTableView...

    But You don't get them from db. You have specified only this columns p.pricedate, p.valuecol, p.totalvalue, s.sharename. Just add 5'th column p.shareid and set them in model invisible.

  9. #8
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help newbie with QTableView...

    Thanks Lesiok but I think I am getting the value I'm trying to use in the where clause in line 21. The problem seems to be that I'm not able to recover the value from the combobox model's record based on the selected line number.


    Actually, it might be something to do with the prepare statement. I tried replacing the parameter with a fixed number and it still failed to work. I've removed the text for the query and put it in the QSqlQuery constructor, with a fixed value for the parameter and it works...

    So it seems there is a problem with the parameter as well perhaps.

    Peter.

  10. #9
    Join Date
    Dec 2008
    Location
    Upper Ferntree Gully, Victoria, Australia
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help newbie with QTableView...

    Got some more questions with this learning if I may.

    I managed to get the program working to some extent, however, I suspect I'm missing something fundamental here.

    I found that removing a column in one query caused me not to be able to recover the value in another part. My understanding was that removecolumn simply hid the data from the UI, apparently this isn't the case. I fixed this by reordering the data retrieval put in the combo box model thus

    Qt Code:
    1. void MainWindow::populateGroupBox(void)
    2. {
    3. comboQueryModel = new QSqlQueryModel;
    4. comboQueryModel->setQuery("select sharename, shareid from shareid");
    5. //comboQueryModel->removeColumn(0);
    6. ui->comboBox->setModel(comboQueryModel);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Now.. the remaining issue is to do with parameterised queries.

    The following works (building the string by hand each time)
    Qt Code:
    1. void MainWindow::updatePriceData(int idx)
    2. {
    3. QSqlRecord record = comboQueryModel->record(idx);
    4. if(record.isEmpty())
    5. {
    6. qDebug() << "Error! Record is empty";
    7. return;
    8. }
    9. QVariant shareidx = record.value("shareid");
    10. priceQueryModel = new QSqlQueryModel;
    11. QString string = "select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid where p.shareid =";
    12. string = string + shareidx.toString();
    13. query = new QSqlQuery(string);
    14. priceQueryModel->setQuery(*query);
    15. if(priceQueryModel->lastError().isValid())
    16. qDebug() << priceQueryModel->lastError();
    17.  
    18. ui->tableView->setModel(priceQueryModel);
    19. priceQueryModel->removeColumn(0);
    20. priceQueryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Closing Date"));
    21. priceQueryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Share Price"));
    22. priceQueryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Total Value"));
    23. priceQueryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Company"));
    24. }
    To copy to clipboard, switch view to plain text mode 

    This however does not work
    Qt Code:
    1. void MainWindow::updatePriceData(int idx)
    2. {
    3. QSqlRecord record = comboQueryModel->record(idx);
    4. if(record.isEmpty())
    5. {
    6. qDebug() << "Error! Record is empty";
    7. return;
    8. }
    9. QVariant shareidx = record.value("shareid");
    10. priceQueryModel = new QSqlQueryModel;
    11. query = new QSqlQuery;
    12. query->prepare("select p.shareid, p.pricedate, p.valuecol, p.totalvalue, s.sharename from pricedata p inner join shareid s on p.shareid = s.shareid where p.shareid = :shareidx");
    13. query->bindValue(":shareidx", shareidx.toInt());
    14. priceQueryModel->setQuery(*query);
    15. if(priceQueryModel->lastError().isValid())
    16. qDebug() << priceQueryModel->lastError();
    17.  
    18. ui->tableView->setModel(priceQueryModel);
    19. priceQueryModel->removeColumn(0);
    20. priceQueryModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Closing Date"));
    21. priceQueryModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Share Price"));
    22. priceQueryModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Total Value"));
    23. priceQueryModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Company"));
    24. }
    To copy to clipboard, switch view to plain text mode 

    and I'm not sure why. The parameterised query would, I'm guessing be much more efficient.

    I also found that when I had the shareidx value as an integer and tried to add that to the query string in the first instance that it added a \ infront of the number each time for some reason. Why is that?

    ie.. the string would become s.shareid where p.shareid = \7 for example instead of =7.

    Thanks in anticipation. I'm slowly getting there.

    Peter.

Similar Threads

  1. Replies: 3
    Last Post: 29th January 2009, 08:38
  2. QTableView in ui with model/delegate
    By tpf80 in forum Qt Programming
    Replies: 7
    Last Post: 6th November 2008, 23:00
  3. QTableView
    By dragon in forum Qt Programming
    Replies: 0
    Last Post: 22nd September 2008, 16:53
  4. make QTableView work as a multi-column list view
    By wesley in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 14:43
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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.