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. insert one row and display tow rows in qtableview, with an additional blank row, that's why?
Forum Updated to NodeBB v4.3 + New Features

insert one row and display tow rows in qtableview, with an additional blank row, that's why?

Scheduled Pinned Locked Moved Solved General and Desktop
qtableview
3 Posts 2 Posters 2.8k 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.
  • niartircN Offline
    niartircN Offline
    niartirc
    wrote on last edited by niartirc
    #1

    hello everyone,

    yesterday, i wrote a test programe using QTableView. i define class CustomModel inheriting from QAbstractTableModel , which implemented rowCount, columnCount, data and headerData. the CustomModel has a member customs storing the data, its a QList. i also add a button to add one item to the data.

    But when i click the button, the view shows tow columns, one is the item i add, the other is a blank row.

    i can't solve it, thanks for your help!

    binding model

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        model = new CustomModel(this);
        ui->tableView->setModel(model);
        connect(ui->actionAddItem, &QAction::triggered, this, &MainWindow::onAddCustom);
    }
    

    when click a button

    void MainWindow::onAddCustom()
    {
        Custom* new_custom = new Custom();
    
        model->appendCustom(new_custom);
    }
    

    It is the code inserting one row

    bool CustomModel::appendCustom(Custom *custom, const QModelIndex &parent)
    {
        beginInsertRows(parent, customs.count(), customs.count()+1);
        customs.append(custom);
        endInsertRows();
        return true;
    }
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      asanka424
      wrote on last edited by
      #2

      Hi,
      Parameters you pass to beginInsertRows are wrong. They should be left first and right last index positions after your insert is done. For example if you have 0 items and you insert 1 item, in resulting model will have 1 item who's index is 0. In your function you say it is 1. Therefore view think now you insert two rows 0 and 1

      niartircN 1 Reply Last reply
      0
      • A asanka424

        Hi,
        Parameters you pass to beginInsertRows are wrong. They should be left first and right last index positions after your insert is done. For example if you have 0 items and you insert 1 item, in resulting model will have 1 item who's index is 0. In your function you say it is 1. Therefore view think now you insert two rows 0 and 1

        niartircN Offline
        niartircN Offline
        niartirc
        wrote on last edited by
        #3

        @asanka424 it works, thanks for you help.

        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