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. About checking an index of a QTableView
Forum Updated to NodeBB v4.3 + New Features

About checking an index of a QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 926 Views 1 Watching
  • 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.
  • U Offline
    U Offline
    U7Development
    wrote on last edited by U7Development
    #1

    Hi!

    I´m looking for a way to check if a QTableView's index has information (is not null) or not..

    For example, if I have an empty table and I try to get value from index(0,0), Qt will crash:

    QAbstractItemModel * model = ui->tbl_data->model();
    QModelIndex _cell;
    _cell = model->index(0,0);        //Crash
    

    I guess is that model has no information, so the _cell is null

    I have tried with Try Catch but still is crashing..

    Any property or way to check if it is null?..

    Now, I know that this has no sense, if there no information in table, why should I check for its indexes ?..
    I want to iterate through the table to count how many rows it has... I can do that, but I need to detect the last used row, check if the next row is null to stop counting...

    Thanks!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Did you actually set a model to the view? I would guess no.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • U Offline
        U Offline
        U7Development
        wrote on last edited by
        #3

        Thanks for your reply,

        If i put a row value from sqlite and then import it into QTableView, then my code works... No crashes.. The above code reads the index 0,0 successfully, but if there is no row to read, then it crashes..
        I guess i did what you said..

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          As @Christian-Ehrlicher answer suggests, your QTableView doesn't have any model until you set one on it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • U Offline
            U Offline
            U7Development
            wrote on last edited by U7Development
            #5

            My friends... this is what I have.. maybe I´m missing what you both said..:

            QAbstractItemModel * model = ui->tbl_data->model();     
            QModelIndex celda = model->index(0, 0);                 //here crashes if cell has no data (not empty string)   
            QString celdaString = celda.data().toString();        
            

            if I´m missing setting model, how can I do it ?
            Thanks again..

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @U7Development said in About checking an index of a QTableView:

              ui->tbl_data

              Create your model and call setModel on your tbl_data widget.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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