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. [SOLVED] QSqlTableModel::removeRow problem.

[SOLVED] QSqlTableModel::removeRow problem.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 5.5k 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.
  • A Offline
    A Offline
    AwayFromKeyboard
    wrote on last edited by
    #1

    Hi, everyone again!
    I have no idea why, but when i am trying to use QSqlTableModel::removeRow(int numb) method, i've got deleted all records with same fields instead of deleting only 1 of them.
    I am using QSQLITE Database.
    All records adds via "INSERT INTO ..." query.

    Here are 3 screenshots of this process:
    1)
    !http://sphotos-a.ak.fbcdn.net/hphotos-ak-snc6/254358_2949167585391_1829528596_n.jpg(1)!

    !http://sphotos-g.ak.fbcdn.net/hphotos-ak-snc6/254358_2949167625392_663510958_n.jpg(2)!

    !http://sphotos-f.ak.fbcdn.net/hphotos-ak-ash4/254358_2949167665393_807131696_n.jpg(3)!

    @bool MyDelegate::editorEvent(QEvent *event, QAbstractItemModel model, const QStyleOptionViewItem &option, const QModelIndex &index)
    {
    if((event->type() == QEvent::MouseButtonPress) && (dynamic_cast<QMouseEvent
    >(event)->button() == Qt::LeftButton))
    {
    model->removeRow(index.row());
    qDebug()<<"Row removed. "<<model->rowCount()<<"row(s) stayed.";
    return true;
    }
    else
    return QItemDelegate::editorEvent(event, model, option, index);
    }@

    I have traced via qDebug(), that the removeRow() method has called only once... But it is still few records deleted, if theirs fields are same.
    What i am doing wrong?

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi!
      Can you show your CREATE TABLE script for "Текущие заявки"?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AwayFromKeyboard
        wrote on last edited by
        #3

        [quote author="qxoz" date="1363926934"]Hi!
        Can you show your CREATE TABLE script for "Текущие заявки"?[/quote]

        Hi!
        I have solved the problem. it was in lack of PRIMARY KEY in my table. So, the deleteRow() function seems to delete rows (even if there are a few) in database, that have the same fields as a row in QSqlTableModel. I guess removeRow() function it's a query like "DELETE * FROM tableName WHERE modelField1 = DatabaseField1, modelField2 = DatabaseField2 etc..."

        and here is the code, that you were asking about:
        @bool DataBaseZayavka::createTablePotok()
        {
        QSqlQuery query;
        qDebug()<<"Creating table...";

        QString str = "CREATE TABLE potok ( "
                "NF INTEGER, "                      //Номер формы               PRIMARY KEY
                "VP INTEGER, "                      //Вид перевозки             PRIMARY KEY
                "KP INTEGER(3), "                   //Код получателя                PRIMARY KEY
                //here are a lot of fields...
                "Z4 VARCHAR(28) "       //пункт следования по признаку далее
                ");";
        
        if(!query.exec&#40;str&#41;)
        {
            qDebug()<<"Error of creating table potok: " + query.lastError().text();
            return false;
        }
        return true;
        

        }
        @

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          Good. How did you solve it?
          And about table, i think be better always create the ID column even if you don't use it right now:
          @QString str = "CREATE TABLE potok ( "
          "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
          "NF INTEGER, " //Номер формы PRIMARY KEY
          "VP INTEGER, " //Вид перевозки PRIMARY KEY
          "KP INTEGER(3), " //Код получателя PRIMARY KEY
          //here are a lot of fields...
          "Z4 VARCHAR(28) " //пункт следования по признаку далее
          ");";
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AwayFromKeyboard
            wrote on last edited by
            #5

            [quote author="qxoz" date="1364189421"]Good. How did you solve it?
            And about table, i think be better always create the ID column even if you don't use it right now:
            @[/quote]

            Thank you for advice!
            Hm.. seems like "solve" isn't a correct word for such situation. I am just guess, that it is right.

            bool QSqlTableModel::selectRow(int row) [virtual slot]
            Refreshes row in the model with values from the database table row matching on primary key values. Without a primary key, all column values must match.

            In this function you can see the same functionality, i guess.
            It's just "SELECT" operator somewhere deeper instead of "DELETE" like removerRow() has.
            But it's only a guess, so i don't pretend it is truth in final instance =)

            1 Reply Last reply
            0
            • A Offline
              A Offline
              AwayFromKeyboard
              wrote on last edited by
              #6

              Yeah, it's seems like problem has solved by adding a new PRIMARY KEY column. I hide it and table works fine: 2 similar (except hiding column ID) rows are deleting separately.

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qxoz
                wrote on last edited by
                #7

                Всегда пожалуйста.:)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  AwayFromKeyboard
                  wrote on last edited by
                  #8

                  lol :)

                  1 Reply Last reply
                  0

                  • Login

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