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] Problem in moving QListWidget's Item either up or down by one position
QtWS25 Last Chance

[SOLVED] Problem in moving QListWidget's Item either up or down by one position

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 7.1k 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.
  • M Offline
    M Offline
    M_31
    wrote on last edited by
    #1

    HI all,
    I want to move the selected item from QListWidget either moving UP or Down by one position....by pressing separate
    PushButton..
    @
    void CTestDlg::UpButtonPressed()
    {
    int nCurRow = ui->listWdgt->currentRow();
    if( nCurRow== 0)
    return;

    QListWidgetItem *curItem = ui->listWdgt->currentItem();
    QListWidgetItem *prevItem = ui->listWdgt->item( nCurRow - 1 );

    ui->listWdgt->scrollToItem( curItem,QAbstractItemView::PositionAtTop );
    }

    void CTestDlg::downButtonPressed()
    {
    int nListCount = ui->listWdgt->count();
    int nCurRow = ui->listWdgt->currentRow();
    if( nListCount == nCurRow )
    return;

    QListWidgetItem *curItem =  ui->listWdgt->currentItem();
    QListWidgetItem *prevItem =  ui->listWdgt->item( nCurRow + 1 );
    
    ui->listWdgt->scrollToItem( curItem,QAbstractItemView::QAbstractItemView::PositionAtBottom );
    

    }
    @

    Please let me know whether i am doing anything wrong on this???

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      from Reference:
      @void QListWidget::scrollToItem ( const QListWidgetItem * item, QAbstractItemView::ScrollHint hint = EnsureVisible ) [slot]@

      bq. Scrolls the view if necessary to ensure that the item is visible.

      as I think you need change item position, not scroll to item.

      yo can do it with two methods:

      @QListWidgetItem * QListWidget::takeItem ( int row )@
      for extracting item from list
      and
      @void QListWidget::insertItem ( int row, QListWidgetItem * item )@
      for inserting to needed position.

      So.. You can get something like:

      @ QListWidgetItem *curItem = ui->listWdgt->takeItem(nCurRow);
      ui->listWdgt->insertItem( nCurRow + 1, curItem );@

      Note: Don't forget check limits.

      --
      Vasiliy

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        [quote author="Vass" date="1317922866"]
        Note: Don't forget check limits.[/quote]

        And don't forget that count() is 1 based, and currentRow() is 0 based, i.e. the first row is #0, not #1. And the last row is count() - 1.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • M Offline
          M Offline
          M_31
          wrote on last edited by
          #4

          Thanks Vass and Volker...its working absolutely fine...

          Thanks for the valuable inputs..

          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