QTableWidget and SQL (dynamic) not working



  • Hi!
    In my program I have a QTableWidget that shows information queried, this is the code:

       int i = 0;
        while (query.next()) {
            //Establecemos un QTableWidget por cada columna
            ui->tableWidget->setItem(i,0,new QTableWidgetItem(query.value(0).toString()));
            ui->tableWidget->setItem(i,1,new QTableWidgetItem(query.value(1).toString()));
            ui->tableWidget->setItem(i,2,new QTableWidgetItem(query.value(2).toString()));
            ui->tableWidget->setItem(i,3,new QTableWidgetItem(query.value(3).toString()));
            ui->tableWidget->setItem(i,4,new QTableWidgetItem(query.value(4).toString()));
            ui->tableWidget->setItem(i,5,new QTableWidgetItem(query.value(5).toString()));
            ui->tableWidget->setItem(i,6,new QTableWidgetItem(query.value(6).toString()));
            ui->tableWidget->setItem(i,7,new QTableWidgetItem(query.value(7).toString()));
            i++;
            recCount++;
        }
    

    Unfortunately it only shows 1 result even though the query is correct.

    "i" is the number of the row and "recCount" doesn't has anything to do with the table.

    Thanks in advance


  • Lifetime Qt Champion

    Hi,

    You don't add any row to your QTableWidget or did you do before running that loop ?

    Also, why not use a QTableView with QSqlQueryModel ?



  • @SGaist "i" is the number of rows that add's +1 at the end of the loop



  • @SGaist But how QtableView works ?



  • I can recommend reading these two articles to not only understand the model/view concept but also how Qt implements it:

    As @SGaist pointed out there's an existing model that allows you to display SQL query results in a QTableView. The model is called QSqlQueryModel. You'd basically use a QTableView instead of a QTableWidget and use QTableView::setModel() to set the QSqlQueryModel as the model for the table view.



  • @Joel-Bodenmann I tried to use QTableView but actually it would break a lot of my code so unless it's completely necessary I won't use it. I'm focusing on trying to solve my current issue.


  • Lifetime Qt Champion

    What would break ?

    In any case, I don't see you creating new rows. The incrementation of i doesn't mean your QTableWidget will have an additional row unless you did it before your while loop.



  • @cxam QTableWidget actually inherits from QTableView so it shouldn't break anything in terms of the GUI. In terms of the data handling: You just outsource that to the model. As there's an existing model that is designed specifically for handling SQL queries I can imagine that the only thing it would do is drastically simplify your life.
    The QTableWidget really is just a QTableView with an internal model. That model is a generic model. Using the QTableView would allow you to use the SQL query specific QSqlQueryModel.

    @SGaist QTableWidget::setItem() actually creates the row internally.



  • @Joel-Bodenmann Hmm I see. I'll check it out then, thanks for your help.


  • Lifetime Qt Champion

    @Joel-Bodenmann No it doesn't. First thing done, it checks whether the row, column combination returns a valid index if not then it returns immediately.



  • @SGaist Yes, you are correct. I am sorry for giving wrong information.

    @cxam So as @SGaist pointed out in his first post: Make sure that your QTableWidget "has enough rows".
    But anyway, I'd still recommend going with QTableView and QSqlQueryModel in your case.



  • @SGaist Indeed, you're right When I set the properties for my table I set that the number of rows was "1" so I have to create an "int" variable and then say that the number of rows is that variable starting from 1 and then increment that number on the loop.


Log in to reply
 

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