Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableWidget See difference between 0 and NULL



  • I have an application that insert data into a database from a qtablewidget. The tablewidget has 100 rows, but not all need to be filled in to insert into the database.
    The data shouldn't be NULL but can be 0.

    I tried to filter it like this:

    int dataID = getDataID(ui->listView->currentIndex().data().toString());
    
        for(int i = 0; i < ui->tableWidget->rowCount(); i++)
        {
            int col0 = NULL;
            int col1 = NULL;
            int col2 = NULL;
    
            for(int j = 0; j < ui->tableWidget->columnCount(); j++)
            {
                if(ui->tableWidget->item(i, j)->text() != nullptr)
                {
                    if(j == 0)
                    {
                        col0 = ui->tableWidget->item(i, j)->text().toInt();
                    }
                    else if(j == 1)
                    {
                        col1 = ui->tableWidget->item(i, j)->text().toInt();
                    }
                    else if(j ==2)
                    {
                        col2 = ui->tableWidget->item(i, j)->text().toInt();
                    }
                }
            }
            if(col0 != NULL &&
               col1 != NULL &&
               col2 != NULL)
            {
                insertData(dataID, col0, col1, col2);
            }
        }
    

    But this won't see the difference between 0 and NULL

    Is there anyone who can help me with this?
    I only need to know how to see the difference between 0 and NULL.


  • Moderators

    @hobbyProgrammer said in QTableWidget See difference between 0 and NULL:

    I only need to know how to see the difference between 0 and NULL.

    0 and NULL are the same thing (typedef). There is no difference and there won't be any.

    If you want to have some value to act as "not set", use -1. Or some custom type / struct that will have a distinction between a set number and not set number.



  • @sierdzio Is it possible to change this by making a custom tableview?
    this = seeing the difference between null and 0.

    I really don't want to set each cell to -1 as a start value, since this will be seen in the tableview and I don't want that.


  • Moderators

    You can't change the NULL typedef. It's got nothing to do with table view, it is defined in core C++ headers (stddef.h). You can, however, create some custom struct and use that in your table, instead of int.



  • I guess the "dataWidgetItem" has a "data" method that returns a Variant. Did you try "isValid" or "isNull" on that before inserting the data?
    Regards
    Holger


  • Moderators

    Hey, but I suppose your table is really holding QVariant, right? Then sure, you can check QVariant::toString().isEmpty() - if true, then you don't have any value set. Possibly even QVariant::isNull() will work.



  • @hobbyProgrammer
    After fetching data from database, https://doc.qt.io/qt-5/qvariant.html#isNull or (if you are SQL) https://doc.qt.io/qt-5/qsqlfield.html#isNull is the thing to test.


Log in to reply