Sql table as Qstring or list?



  • I am making an application that reads in a MS SQL database and displays it as QTableModel. There is an active coloumn (just 1's and 0's not check boxes because I cant figture that out yet). When button is pushed I need to display the records that are marked as active, the records with 1 in the active feild, in a format like this

    Name: Doe, John Q., 35, 714 Main St. any special info
    Service: South Location Monday 1:30 pm any special info
    Other: realtionship info any special info

    there is a feild called special info name of the row. I dont need to worry about the button pushing part right now, I generally know how to do that, but I am just trying to figure out how to read in the database so it can be displayed in this form.

    does anyone know any way to do this or could point me in the direction of some documentation that can help me?

    Any help is greatly appreciated! Im getting close to my deadline and this is a major thing holding me back



  • If you have the data in your model already, there is no need to worry about the SQL part anymore. Simply iterate over your model to find the items you need, and build up your text like that. Something like:

    @
    QString output;

    for (int row(0); row < myModel.rowCount(QModelIndex()); ++row) {
    QModelIndex idx = myModel.index(row, 0, QModelIndex());
    if (idx.isValid()) {
    if (1 == idx.data().toInt()) {
    //extract the data from this row, and put it in output
    }
    }
    }
    @



  • Andre you are a genius, you know everything about this stuff!

    A few questions though
    my model is currently a pointer, only because thats how the examples I saw had it, is there any reason to have it as a pointer? I've learned about pointers but I can't exactly remember what they are for/do so Im not sure if its needed.

    Also what is going on in line 6?

    And would I need the window I am displaying this in to be a listwidget or what do you think would be the best? The only thing I have made is window that displays the table so I'm not sure that would be best.

    Thanks!



  • [quote author="confused" date="1344435540"]
    my model is currently a pointer, only because thats how the examples I saw had it, is there any reason to have it as a pointer? I've learned about pointers but I can't exactly remember what they are for/do so Im not sure if its needed.
    [/quote]
    Yes, there certainly is! The reason is that you need to make sure that the model does not go out of scope at the moment your method is finished. For that, you create the model on the heap instead of on the stack using the new keyword, which returns a pointer. This is all the more needed, because Qt models are QObjects, which you cannot copy.

    [quote]
    Also what is going on in line 6?
    [/quote]
    Line 6 converts the value that is in the model on the index where you expect the 1 or 0 to an integer, and compares that with the value 1.

    [quote]
    And would I need the window I am displaying this in to be a listwidget or what do you think would be the best? The only thing I have made is window that displays the table so I'm not sure that would be best.
    [/quote]
    Eh... no. A window almost never is a QListWidget, though it might contain one. Learn about the difference between inheritance and composition. You need both.



  • [quote author="Andre" date="1344436568"]
    Yes, there certainly is! The reason is that you need to make sure that the model does not go out of scope at the moment your method is finished. For that, you create the model on the heap instead of on the stack using the new keyword, which returns a pointer. This is all the more needed, because Qt models are QObjects, which you cannot copy.
    [/quote]
    How would create my model on heap?

    [quote]
    Line 6 converts the value that is in the model on the index where you expect the 1 or 0 to an integer, and compares that with the value 1.
    [/quote]

    ok so this is only looking at the active column? That is my 1st column so it would need to be
    @if (0 == indx.data().toInt())@
    right? Or does Qt/QModel index start the index at 1 and not 0 like C++?

    [quote]
    Eh... no. A window almost never is a QListWidget, though it might contain one. Learn about the difference between inheritance and composition. You need both.
    [/quote]

    Yeah thats what I mean, should the centralWidget be a QListWidget or is there something better to use to put it in the format I need? I'll look into those 2 things also.

    thanks

    (sorry about all the ?s that are pretty dumb, I'm still in college and doing an internship, my last professor was supposed to teach us about heap storage but then decided not to for some reason)



  • [quote author="confused" date="1344438183"]
    [quote author="Andre" date="1344436568"]
    Yes, there certainly is! The reason is that you need to make sure that the model does not go out of scope at the moment your method is finished. For that, you create the model on the heap instead of on the stack using the new keyword, which returns a pointer. This is all the more needed, because Qt models are QObjects, which you cannot copy.
    [/quote]
    How would create my model on heap?[/quote]
    By using
    @
    TheModelClass* model = new TheModelClass(this);
    @

    Note that Qt does not lift the requirement to learn basic C++ concepts...
    [quote]

    [quote]
    Line 6 converts the value that is in the model on the index where you expect the 1 or 0 to an integer, and compares that with the value 1.
    [/quote]

    ok so this is only looking at the active column? That is my 1st column so it would need to be
    @if (0 == indx.data().toInt())@
    right? Or does Qt/QModel index start the index at 1 and not 0 like C++?
    [/quote]
    No! The 1 there refers not to the column, but to the value that you said you use in the column to mark the rows you are interested in. You said you use 0 and 1, so I compared with 1.
    Qt uses 0 based indices. The actual column number the code uses is in line 4, and is at 0 at the moment.
    [quote]
    [quote]
    Eh... no. A window almost never is a QListWidget, though it might contain one. Learn about the difference between inheritance and composition. You need both.
    [/quote]

    Yeah thats what I mean, should the centralWidget be a QListWidget or is there something better to use to put it in the format I need? I'll look into those 2 things also.[/quote]

    How should we know? You're not telling us how your UI is supposed to look or work, so none of us can comment on that.

    [quote]
    (sorry about all the ?s that are pretty dumb, I'm still in college and doing an internship, my last professor was supposed to teach us about heap storage but then decided not to for some reason)[/quote]
    Go get your tuition money back then...



  • To create your model on heap use the new operator:

    @MyModel *model = new MyModel();@

    Now the model is on the heap and model is a pointer to it.



  • [quote author="Andre" date="1344438581"]
    By using
    @
    TheModelClass* model = new TheModelClass(this);
    @

    Note that Qt does not lift the requirement to learn basic C++ concepts...
    [/quote]
    Ok if I initialize the vairable in the hearder as
    @TheModelClass* model; @
    then define it in the constructor as
    @model = new TheModelClass(this);@
    this would do the same thing right? And also be able to be used throughout other functions in the class?

    and I know, I do know some, I think most, basic C++ concepts theres just a few that were over looked in my classes I suppose, and I just forgot about pointers. But I will definietly be making my professor cover those with me when I get back
    [quote]
    [quote]
    Line 6 converts the value that is in the model on the index where you expect the 1 or 0 to an integer, and compares that with the value 1.
    [/quote]
    No! The 1 there refers not to the column, but to the value that you said you use in the column to mark the rows you are interested in. You said you use 0 and 1, so I compared with 1.
    Qt uses 0 based indices. The actual column number the code uses is in line 4, and is at 0 at the moment.
    [/quote]
    Ooo ok I didn't think about the how the .data() was looking at the value. My bad.
    [quote]
    How should we know? You're not telling us how your UI is supposed to look or work, so none of us can comment on that.
    [/quote]
    This is currently made in a word file I need to it keep the same general format it, something like this:
    My Co Name Current Date Time Made


    Name: Doe, John Q., 35, 714 Main St. any special info 2351
    Service: South Location any special info Monday 1:30 pm
    Other: realtionship info any special info (Jones/Smith)
    Family: Sister, Susy Doe, 555-5555

    The top of each new record has a border on it that goes all the way accross the page. I don't need it to be editable, but if that is possible without too much effort it would be a nice feature. If theres anything else I left out let me know.

    edit: there should be tabs between My co Name and Current date and Time made to put time made and current date over to the right but the space I added didnt show up. Same for the lines of info, there should be a tab before and after special info and anything that shows up after special info should be aligned on the right side. This doesnt really matter for how to make the window I guess but I figured Id clarify since it doesnt show up the same way I intended it to


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.