Results 1 to 2 of 2

Thread: Filling form after shwoing it

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Filling form after shwoing it

    I have this class:
    Qt Code:
    1. #include <QSqlError>
    2. #include <QFile>
    3. #include <QTextStream>
    4. #include <QMessageBox>
    5.  
    6. #include "itemsell.h"
    7. #include "ui_ItemSell.h"
    8. #include "sales.h"
    9.  
    10. ItemSell::ItemSell(QWidget *parent) :
    11. QDialog(parent),
    12. ui(new Ui::ItemSell)
    13. {
    14. ui->setupUi(this);
    15. }
    16.  
    17. void ItemSell::errorLog(QString error)
    18. {
    19. QFile log("errors.log");
    20. log.open(QFile::Append | QFile::Text);
    21. QTextStream out(&log);
    22. out << error;
    23. log.close();
    24. }
    25.  
    26. void ItemSell::getSale(const int& id)
    27. {
    28. sqlQuery = "SELECT Sales.Countity as Scoun, Sales.Totale as Stotale FROM Sales "
    29. "JOIN Items ON Sales.ID_I = Items.ID_I "
    30. "WHERE Sales.ID_S=" + id;
    31. if(!query.exec(sqlQuery))
    32. {
    33. error = "[" + query.lastError().text() + "]\t[" + query.lastQuery() + "]\t[" + QDate::currentDate().toString("yyyy-MM-dd") + "\t" +QTime::currentTime().toString("hh-mm") + "]\n";
    34. this->errorLog(error);
    35. }
    36.  
    37. query.next();
    38. rec = query.record();
    39. ui->itemMat->setText(query.value(rec.indexOf("ID_I")).toString());
    40. ui->nameSale->setText(query.value(rec.indexOf("Name")).toString());
    41. ui->dateSale->setDate(query.value(rec.indexOf("Day")).toDate());
    42. ui->priceSale->setText(query.value(rec.indexOf("Price")).toString());
    43. ui->countitySale->setText(query.value(rec.indexOf("Scoun")).toString());
    44. ui->totaleSale->setText(query.value(rec.indexOf("Stotale")).toString());
    45. sd.setText(query.lastQuery() + "*** " + query.lastError().text());
    46. sd.exec();
    47. }
    48.  
    49. ItemSell::~ItemSell()
    50. {
    51. delete ui;
    52. }
    To copy to clipboard, switch view to plain text mode 
    And I have this slot executed when the user click on action menu:
    Qt Code:
    1. void Store::showItem()
    2. {
    3. int id = ui->lstSells->model()->index(m_ind.row(), 0).data().toInt();
    4. ItemSell *item = new ItemSell(this);
    5. item->getSale(id);
    6. item->exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    My problem is the query is never executed because an SQL error statement, it is always executed like this:
    Qt Code:
    1. CT Sales.Countity as Scoun, Sales.Totale as Stotale FROM Sales JOIN Items ON Sales.ID_I = Items.ID_I WHERE Sales.ID_S=1
    2. Sales.Countity as Scoun, Sales.Totale as Stotale FROM Sales JOIN Items ON Sales.ID_I = Items.ID_I WHERE Sales.ID_S=1
    To copy to clipboard, switch view to plain text mode 
    While it should be like this:
    Qt Code:
    1. SELECT Sales.Countity as Scoun, Sales.Totale as Stotale FROM Sales JOIN Items ON Sales.ID_I = Items.ID_I WHERE Sales.ID_S=1
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,328
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Filling form after shwoing it

    Qt Code:
    1. ItemSell::~ItemSell()
    2. {
    3. delete ui;
    4. }
    To copy to clipboard, switch view to plain text mode 

    This is probably corrupting memory and resulting in your sqlQuery constant string getting messed up. The code in your constructor "ui->setup( this )" passes ownership of the ui pointer to the widget, so therefore the ui pointer is getting deleted twice, once by you and once by the widget. Get rid of the delete in your destructor.

    Qt Code:
    1. void Store::showItem()
    2. {
    3. int id = ui->lstSells->model()->index(m_ind.row(), 0).data().toInt();
    4. ItemSell *item = new ItemSell(this);
    5. item->getSale(id);
    6. item->exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    In this code, if ItemSell is a modal dialog (which it seems to be, since you are calling exec()), then you do not need to create a new one on the heap (using new), you should simply do this:

    Qt Code:
    1. void Store::showItem()
    2. {
    3. int id = ui->lstSells->model()->index(m_ind.row(), 0).data().toInt();
    4. ItemSell item(this);
    5. item.getSale(id);
    6. item.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    The exec() statement causes the dialog to block until the user clicks the OK button, then the ItemSell instance will automatically go away as the showItem() method exits scope. The way you have it written now, you have a memory leak - the ItemSell instances you are creating do not get deleted when the showItem method() exits, but since you aren't saving the pointer anywhere, you have no way to get rid of it later. Not until the Store instance is finally destroyed (when your program exits) do the ItemSell instances it owns (because you set the parent to "this") get deleted. So if the user of your Store software runs the program for weeks or months without ever exiting, eventually their computer will use up all the available memory and your program will probably crash.
    Last edited by d_stranz; 17th March 2012 at 23:46.

Similar Threads

  1. How to hide from taskbar a form showed form mainwindow
    By tonnot in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2011, 14:36
  2. Filling a QGraphicsItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 15
    Last Post: 6th July 2009, 14:34
  3. Filling an image
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 6th July 2008, 21:13
  4. Filling very small polygons.
    By maverick_pol in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2008, 15:19
  5. filling scrollview
    By illuzioner in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2006, 22:00

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.