QAbstractTableModel empty last row, without sorting

I have my own table model (DragDropTableModel) subclassed from QAbstractTableModel. Inside it I would like for the last row to remain always empty.

I implement it by adding insertRows() into setData() function:

bool DragDropTableModel :: setData (const QModelIndex &index, const QVariant &value, int role) { ... if (role == Qt::EditRole) { ... if (row == this->rowCount() - 1) insertRows(this->rowCount(), 1); } }

After that I have QSortFilterProxyModel installed on it. The problem is: when I do sorting, the last row is also sorted. But I want to make it not obeying sorting and to stand on the same last position. So no matter the sorting order is (Ascending or Descending) the last row should always be empty.

How to make it? May be I should use another approach to realize "making empty row" in my model?

-------------Problems Reply------------

Thanks, murison. Your idea worked. I set up a value "EMPTY_ROW" for Qt::UserRole for the items of empty row. After that I reimplemented lessThan() method in QSortFilterProxyModel:

bool MySortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
if (left.data(Qt::UserRole) == "EMPTY_ROW")
return (this->sortOrder() == Qt::DescendingOrder);
else if (right.data(Qt::UserRole) == "EMPTY_ROW")
return (this->sortOrder() == Qt::AscendingOrder);
else
return QSortFilterProxyModel::lessThan(left, right);
}

So this row always appears in the bottom of the table after sorting. Also it's nescesary not to forget to clear Qt::UserRole when this empty row is filled with data.

Category:c# Views:4 Time:2017-09-10
Tags: sorting

Related post

  • Selecting 'highest' X rows without sorting 2011-03-29

    I've got a table with huge amount of data. Lets say 10GB of lines, containing bunch of crap. I need to select for example X rows (X is usually below 10) with highest amount column. Is there any way how to do it without sorting the whole table? Sortin

  • Any last row without VBA 2014-08-23

    Is this possible to write below formula in excel but without vba. =SUM(A1:A6) something like as below =SUM(A1:A&COUNTA(A:A)) --------------Solutions------------- Hi, How will use the same combination in below formula at bold marked =IFERROR(IF(LE

  • How do I select last 5 rows in a table without sorting? 2008-11-22

    I want to select the last 5 records from a table in SQL Server without arranging the table in ascending or descending order. --------------Solutions------------- This is just about the most bizarre query I've ever written, but I'm pretty sure it gets

  • How do I select top 10 rows in a table without sorting? 2008-11-22

    Possible Duplicate: How do I select last 5 rows in a table without sorting? I want to select the top 10 records from a table in SQL Server without arranging the table in ascending or descending order. --------------Solutions------------- if random or

  • SQL get last rows in table WITHOUT primary ID 2011-05-03

    I have a table with 800,000 entries without a primary key. I am not allowed to add a primary key and I cant sort by TOP 1 ....ORDER BY DESC because it takes hours to complete this task. So I tried this work around: DECLARE @ROWCOUNT int, @OFFSET int

  • Select last n rows without use of order by clause 2011-10-31

    I want to fetch the last n rows from a table in a Postgres database. I don't want to use an ORDER BY clause as I want to have a generic query. Anyone has any suggestions? A single query will be appreciated as I don't want to use FETCH cursor of Postg

  • Select range from worksheet, copy, and paste to first empty row without data in other worksheet 2012-03-22

    I want to create a code that will check the range A2:A for data in sheet ‘Number2’. Once the program arrives at the last row with data in sheet ‘Number2’ I want it to copy the range A2:AH(last row with data). Next I want the code to paste the data it

  • Perl MySQL - How do I return a tables last row id without looping through whole table? 2010-11-03

    my $sth = $dbh->prepare("SELECT id FROM user WHERE group == '1' ORDER BY id DESC LIMIT 1"); I was trying to get the id of the last row in a table without reading the whole table. I am already accessing via: my $sth = $dbh->prepare("SELECT name,

  • Sorting 2d-array by last row with Arrays.sort 2011-05-26

    Is it possible to sort 2d-array by last row with Arrays.sort(,) in Java. The following snippet works great for sorting by last column but it doesn't seem to have a way to be adjusted for sorting by last row. My first thought was to use tranforming co

  • How to sort a DefaultTableModel without sorting the first row? 2014-06-11

    Hi I would like to sort a Jable without sorting the first row(which should always be at the top, as it contains constants and variables that must not be sorted). I used: RowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableMo

  • Macro to show last row empty 2014-08-06

    I have macro the fill a spreadsheet log automatically. However, I would like to have a macro statement to show the last row empty all the time. Do you think is possible? Thanks in advance. Maperalia --------------Solutions------------- Worksheets(1).

  • Insert rows without deleting empties? 2014-09-30

    Excel 2007: If create a small worksheet (4 columns by 35 rows), there is no way to insert new rows. In order to insert a row, I have to go to the last entry and delete a bunch of empty rows. Every time I want to add rows, I have to go through the sam

  • VBA - find last row with data (skip over empty rows) 2013-12-03

    Hello all.. I'm looking for a more elegant way to find the last row with data when there are embedded blank rows. So far I have used: Dim RowsToProcess As Long Range("a1000000").Select Selection.End(xlUp).Select RowsToProcess = Selection.Row For i =

  • non-volatile worksheet function to find last used row (without VBA)? 2014-08-01

    The title says it all; I'm looking for a non-volatile method to find the last used row on a worksheet (Columns A: CZ). Any column might include blanks, so COUNT or COUNTA doesn't help. Each row will have at least one cell with numeric or text value i

  • How to read the last row with SQL Server 2008-10-07

    What is the most efficient way to read the last row with SQL Server? The table is indexed on a unique key -- the "bottom" key values represent the last row. --------------Solutions------------- If you're using MS SQL, you can try: SELECT TOP 1 * FROM

  • Datagridview scrolling/last row problem (.Net Winforms) 2009-02-11

    I have a grid with 7 and half rows visible. I have managed to display the last row fully but the half row space after the last row does not repaint and shows whatever was there previously. Even if I drag another window over my form with the grid and

  • mysql select from n last rows 2009-02-21

    I have a table with index (autoincrement) and integer value. The table is millions of rows long. How can I search if a certain number appear in the last n rows of the table most efficiently? --------------Solutions------------- Starting from the answ

  • Retrieving a single column of last row in sqlite/Android using ORDER ID desc limit 1 2010-11-10

    I'm attempting to create a method that will allow me to retrieve a value from the last row of a database, and then insert it into an EditText field. (This is a value that the user will not change all that often, so it would be helpful if when they do

  • Displaytag last row's jquery dialog cant load ajax 2011-09-16

    I am using struts 2 + display tag and Struts jQuery plugins in my project. I am having some issues using the display tag. This is my current markup: <display:table style="width:100%" cellpadding="5" cellspacing="1" name="interviewAssList" class="d

  • iOS app crashes when deleting the last row from a tableview section 2012-03-12

    I am learning editable sectioned UITableViews and I am having trouble figuring out why my app crashes when deleting the last row in one of the sections. I've been looking around here and google and it looks to me that I have already done what most pe

  • How can I find the last row where the formula evalutes to something other than ""? 2014-07-19

    Hi all, I have a simple macro to sort rows in a spread sheet, which is supposed to find the last used row in the spreadsheet and sort just those rows. However, because the cells in column A all contain formulas, the Macro is using the last row where

  • Fill down formula based on last row in another column 2014-08-20

    My Report starts at row 16 (headers). The data that will be updated starts in A17 thru J (and then added to the next line when updated) I have formulas hidden in R14 thru K14 that calculate based on data in A thru J When I choose, I need to run code

  • Identify the last row in filtered data 2014-12-21

    Hi, This code has already established that the last row number (before filtering was applied) was 15. .... lnglastRowS = SSh.Cells(Cells.Rows.Count, "A").End(xlUp).Row .... Once filtering has been applied, I was hoping to use lngLastRow to determine

  • How to incorporate dynamic ranges in a formula to include the latest data in the last row for calculations 2014-12-29

    I have a data table (lets assume for the sake of simplicity a data column for date and another data column for sales composed of 35 rows as today). Everyday I am adding new rows for the new data. However I am also making some calculations everyday (a

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.127 (s). 11 q(s)