Results 1 to 2 of 2

Thread: Copying QTableView rows

  1. #1
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Copying QTableView rows

    I'm trying to provide copy funcationality in a QTableView so the user can copy an paste rows from the TableView into Excel, for instance. I've got the following set for the TableView (The table has 11 fixed columns, model set as a QAbstractItemModel):

    myTableView->setSelectionBehavior(QAbstractItemView::SelectRow s);
    myTableView->setSelectionMode(QAbstractItemView::ExtendedSelec tion);

    The slot that gets called when 'copy' is selected is :

    void IOSAnalyzer::copyRows()
    {
    QString str;


    QModelIndexList selectionList = selectionModel->selectedIndexes();
    QModelIndex index;

    if (selectionList.size() > 0)
    {

    int k, j = 0;
    for (int i=0; i<selectionList.size(); ++i) {

    qDebug()<<"row is "<<str.setNum(k)<<"...column is <<"<<str.setNum(selectionList[i].column());
    str+=model->index(selectionList[i].row(),selectionList[i].column(),QModelIndex()).data().toString();
    str+="\t";
    }
    }
    QApplication::clipboard()->setText(str);
    }

    Naturally, this doesn't work, but the funny part is - if I select rows 5-7, qDebug() shows :

    row is 5...column is 0
    row is 6...column is 0
    row is 7...column is 0
    row is 5...column is 1
    row is 6...column is 1
    row is 7...column is 1
    ..and so on.

    Now, if I hold down the control key and select row 5, then 7, then 6 and hit 'copy', qDebug shows:

    row is 5...column is 0
    through
    row is 5...column is 10
    row is 7...column is 0
    through
    row is 7...column is 10
    row is 6...column is 0
    through
    row is 6...column is 10

    These two results into the QModelIndexList make it difficult to add lines into QString str. If I select all rows, and interate as follows:

    for (int i = 0; i < totalRows; ++i)
    {
    for (int x=0; x<11; ++x)
    {
    str+=model->index(i,x,QModelIndex()).data().toString();
    str+="\t";
    }
    str += "\n";
    }

    This works like a charm... but not for holding down the control key, or selecting a smaller range of rows.

    Is there an easier way??? Every example I can find either only copies a single cell, or doesn't even copy it to the clipboard.

    Thanks,
    --D

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Copying QTableView rows

    First of all use "the proper way" of dealing with copy/paste which is using QAbstractItemModel::mimeData(). As for the problem with ordered items, either just reorder the items or find the minimum and maximum values for row/column and encode the range of items they hold. The first idea will work properly with multiple selections, but the second one should be easier/faster (and will work for extended selection as well).

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. Copying contents of QTableView cell to clipboard
    By Conel in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2006, 15:50
  3. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49
  4. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 06:55
  5. Get list of selected rows from QTableView
    By jnk5y in forum Qt Programming
    Replies: 8
    Last Post: 17th February 2006, 16:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.