Results 1 to 18 of 18

Thread: QString problem...

  1. #1
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Question QString problem...

    Hi there guys,

    I have a bit of a problem here. I have the following code:

    Implementation:
    Qt Code:
    1. //This function will open the database file (which should lie in the path indicated by the user)
    2. void mainwindow::loaddatabase() {
    3. //This pretty much says that the database will be of SQLite type, and opens it up from a given path
    4. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    5. db.setDatabaseName(path);
    6.  
    7. //This "if" shows up a messagebox in case the program cannot load a given database
    8. if (!db.open()) {
    9. QMessageBox::warning(0, qApp->tr("Cannot open database"),
    10. qApp->tr("Unable to establish a database connection.\n"
    11. "This program requires a database to operate. Please contact "
    12. "the developer of this software for more information on "
    13. "how to use it.\n\n"), QMessageBox::Ok);
    14. }
    15.  
    16. //Note: "medadmin" is the name of the table in the SQLite3 database
    17. model->setTable ("medadmin");
    18. model->select();
    19. ...
    20. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //this makes the openFile dialog get path of the database file and then invocate loaddatabase with such path as the parameter
    2. void mainwindow::openfile() {
    3.  
    4. path = QFileDialog::getOpenFileName(this,
    5. tr("Open Database File"), "/Users", tr("SQLite Database File (*.db)"));
    6. if (!path.isEmpty()){
    7. loaddatabase();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    This was declared in the header:

    Qt Code:
    1. class mainwindow : public QMainWindow
    2. {
    3. //declaring the path variable
    4. QString path;
    5. ...
    6. }
    To copy to clipboard, switch view to plain text mode 

    ... And it doesn't work! whenever test the program and select a open a .db file he does nothing! I believe that the problem might be associated with the QString path.

    Any code with be greatly appreciated

    Thanks in advance

  2. #2
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QString problem...

    BUMP

    Please help, please...

    If any additional information is required please request it

  3. #3
    Join Date
    Sep 2009
    Posts
    20
    Thanked 2 Times in 2 Posts

    Default Re: QString problem...

    Hi,

    Please try to use debugger, set up breakpoint at
    Qt Code:
    1. if (!path.isEmpty()){
    2. loaddatabase();
    To copy to clipboard, switch view to plain text mode 

    and check path value.

  4. The following user says thank you to lasher for this useful post:

    Nefastious (26th September 2009)

  5. #4
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Red face Re: QString problem...

    Play with something like this ...

    Qt Code:
    1. void mainwindow::openfile()
    2. {
    3. // for testing only ....
    4. qDebug() << "we are in openfile() now";
    5.  
    6. path = QFileDialog::getOpenFileName(this, tr("Open Database File"), "/Users", tr("SQLite Database File (*.db)"));
    7.  
    8. qDebug() << path;
    9.  
    10. if ( !path.isEmpty() )
    11. {
    12. qDebug() << "path not empty";
    13. loaddatabase();
    14. }
    15. else
    16. {
    17. qDebug() << "path is empty";
    18. {
    19. }
    To copy to clipboard, switch view to plain text mode 

    Regards Guenther

  6. The following user says thank you to gboelter for this useful post:

    Nefastious (26th September 2009)

  7. #5
    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: QString problem...

    What exactly "doesn't work"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #6
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QString problem...

    @wysota

    it simply doesn't open up any databases, when requested to do so

    @at the other guys
    will try qdebug and then post another reply with the results

  9. #7
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QString problem...

    well I did play with qDebug for a while it tells me that well, here are the results:

    we are in openfile() now
    database opened
    path not empty

    one thing I added qDebug() << "database opened"
    as part of that if that tests if the database is opened (in loaddatabase() ),
    so aparently the problem doesn't lie in the path String.

    Any ideas where it might lie?

    If you need any code I will show it upon request

    Thanks in advance

  10. #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: QString problem...

    Did you try asking the database what the problem was?
    QSqlDatabase::lastError()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    Nefastious (27th September 2009)

  12. #9
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QString problem...

    By using

    Qt Code:
    1. qDebug() << db.lastError();
    To copy to clipboard, switch view to plain text mode 

    I get the following:

    QSqlError(-1, "", "")

    What does this mean?

  13. #10
    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: QString problem...

    It means there is no error in the database object. How do you know the database failed to open?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    Nefastious (29th September 2009)

  15. #11
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QString problem...

    Well,

    there is a model connected to the database and then a TableView connected to the model, and the View remains blank after I supposedly load the database...


    Any ideas?

  16. #12
    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: QString problem...

    Quote Originally Posted by Nefastious View Post
    there is a model connected to the database and then a TableView connected to the model, and the View remains blank after I supposedly load the database...
    It doesn't mean the database failed to open!

    What do QSqlDatabase::open() and QSqlDatabase::isOpen() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. The following user says thank you to wysota for this useful post:

    Nefastious (29th September 2009)

  18. #13
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QString problem...

    true on both

    so we can assume that the problem doesn't lie on the database object itself, but where could it lie? Any ideas?

  19. #14
    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: QString problem...

    The query is probably incorrect or the table is empty. Check the last error of the model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. The following user says thank you to wysota for this useful post:

    Nefastious (29th September 2009)

  21. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QString problem...

    A scope issue? "QSqlDatabase db" goes out of scope at the end of the mainwindow::loaddatabase().

  22. #16
    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: QString problem...

    Quote Originally Posted by ChrisW67 View Post
    A scope issue? "QSqlDatabase db" goes out of scope at the end of the mainwindow::loaddatabase().
    That's irrelevant. SQL databases are kept within a global singleton hash until they are removed by QSqlDatabase::removeDatabase().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  23. The following user says thank you to wysota for this useful post:

    Nefastious (29th September 2009)

  24. #17
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QString problem...

    Sorry to ask but I am unsure how to use qDebug to check for errors in the model (what member should I use?)

    Not sure if this will help but here is the code for the view and the model:

    Qt Code:
    1. //Note: "medadmin" is the name of the table in the SQLite3 database
    2. model->setTable ("medadmin");
    3. model->select();
    4. qDebug << model->error();
    5.  
    6. //The following lines edit a couple of parameters in the table view "View" and bind it to the model
    7. ui->View->setWindowTitle ("Patients List");
    8. ui->View->setModel (model);
    9. ui->View->setSelectionBehavior (QAbstractItemView::SelectRows);
    10. ui->View->resizeColumnsToContents();
    11. ui->View->setSelectionMode (QTableView::SingleSelection);
    12. ui->View->setVisible (true);
    13. ui->View->setColumnHidden(0, true);
    To copy to clipboard, switch view to plain text mode 

  25. #18
    Join Date
    Sep 2009
    Posts
    34
    Thanks
    28
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QString problem...

    Note: qDebug << model->error(); is the possible way to use qDebug, it obviously doesn't work, I simply copy+paste'd it by accident... =P

Similar Threads

  1. problem copying files and unmount drives
    By alejo in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2009, 20:54
  2. Replies: 9
    Last Post: 6th May 2009, 10:09
  3. QString problem?
    By raphaelf in forum Newbie
    Replies: 7
    Last Post: 24th June 2008, 09:12
  4. char* to QString. Segfault after delete []
    By TheRonin in forum Qt Programming
    Replies: 9
    Last Post: 19th June 2008, 13:20
  5. QHash non-ANSI key problem
    By roxton in forum Qt Programming
    Replies: 4
    Last Post: 27th May 2008, 15:54

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.