Results 1 to 8 of 8

Thread: Showing GUID keys in Standart View Classes

  1. #1
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Showing GUID keys in Standart View Classes

    Hello. Sorry for my pure English (i'm from Russia)

    I want to use standart (QSqlQueryModel+QtableView) for working with MSSQL database.
    When i trying to get MSSQL "uniqueidentifier" fileds from database, in View it shows as no-readable symbols.
    I know that GUID supports in QT as UUid Class. But it not encluded in QVariant::type enum.

    I can solve this problem, with subclassing a QSqlQueryModel and reimlplementing a data() - method. But I think that not only me face with this problem. Thank You.

  2. #2
    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: Showing GUID keys in Standart View Classes

    Could you show us your code used for querying the database?

  3. #3
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing GUID keys in Standart View Classes

    This is standart code; In database cdoc field is UNIQUEIDENTIFIER.

    Qt Code:
    1. #include <QApplication>
    2. #include <QSqlQuery>
    3. #include <QVariant>
    4. #include "MainWidget.h"
    5. #include "iostream"
    6.  
    7. QSqlDatabase openconnnection(){
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    9. db.setDatabaseName( "DRIVER={SQL Server};Server=DELTA;Database=Test;Trusted_Connection=yes;");
    10. db.open();
    11. return db;
    12. };
    13. ///////////////////////////////// ** ////////////////////////////////////
    14. int main(int argc, char *argv[]){
    15. QApplication app(argc, argv);
    16. QSqlDatabase db = openconnnection();
    17. QSqlQuery sql("SELECT TOP 1 CDOC, DOCDATE FROM DOCS");
    18. sql.first();
    19.  
    20. const char* strCDOC = sql.value(0).toString().toLocal8Bit();
    21. std::cout << strCDOC << std::endl;
    22.  
    23. const char* strDATE = sql.value(1).toString().toLocal8Bit();
    24. std::cout << strDATE << std::endl;
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Yesterday, i trying to get field as ByteArray. If I divide standart byte to 2 part 4-bit array in each. Each of new 4bit symbol - are the real symbol of UNIQUEIDENTIFIER.

  4. #4
    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: Showing GUID keys in Standart View Classes

    Ouch... your code is bad... it's even Bad. First of all you shoould check if the query returned any result at all using QSqlQuery::next() or QSqlQuery::first() (check the return value). Second of all, you musn't store the value returned by toLocal8Bit() (which by the way returns a byte array not a const char*), because the byte array is a temporary object. You're just asking for your application to crash or work incorrectly.

  5. #5
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing GUID keys in Standart View Classes

    No, it is not use-able code. It may be very bad. It code only shows that I can't show first field in query. I trying write smallest code for you.

  6. #6
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing GUID keys in Standart View Classes

    check first what return db.open() and sql.first()

  7. #7
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Showing GUID keys in Standart View Classes

    I sure that you don't understand my problem..
    If i use qsqlquerymodel - it will be more simple to understand

    Qt Code:
    1. #include <QApplication>
    2. #include <QSqlQueryModel>
    3. #include <QTableView>
    4. #include "MainWidget.h"
    5. #include "iostream"
    6.  
    7. QSqlDatabase openconnnection(){
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    9. db.setDatabaseName( "DRIVER={SQL Server};Server=DELTA;Database=Test;Trusted_Connection=yes;");
    10. db.open();
    11. return db;
    12. };
    13.  
    14. ///////////////////////////////// ** ////////////////////////////////////
    15. int main(int argc, char *argv[]){
    16. QApplication app(argc, argv);
    17. QSqlDatabase db = openconnnection();
    18. QSqlQueryModel sqlmodel;
    19. sqlmodel.setQuery("SELECT TOP 10 CDOC, DOCDATE FROM DOCS");
    20. tbl.setModel(&sqlmodel);
    21. tbl.show();
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Problem step-by-step description:
    In my database DOCS table contains more than 3 millions records. Than I started application, I see 10 records in grid, row's first columns is no-readable, second presents a standart date field.

    wnd.jpg

    How to present row's first column, like that: {4FEE5BC6-8904-45EC-B830-0000433322D2}

  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: Showing GUID keys in Standart View Classes

    The data is probably returned as a byte array which you need to decode into string manually.

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

    Rus (18th April 2008)

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.