Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget See difference between 0 and NULL

QTableWidget See difference between 0 and NULL

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtablewidget
7 Posts 4 Posters 723 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by
    #1

    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.

    sierdzioS JonBJ 2 Replies Last reply
    0
    • H hobbyProgrammer

      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.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @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.

      (Z(:^

      H 1 Reply Last reply
      1
      • sierdzioS sierdzio

        @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.

        H Offline
        H Offline
        hobbyProgrammer
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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.

          (Z(:^

          1 Reply Last reply
          0
          • HoMaH Offline
            HoMaH Offline
            HoMa
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            2
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              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.

              (Z(:^

              1 Reply Last reply
              1
              • H hobbyProgrammer

                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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved