Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7432457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:27:40+00:00 2026-05-29T09:27:40+00:00

Consider a QTableWidget and two buttons move up and move down. Clicking on move

  • 0

Consider a QTableWidget and two buttons “move up” and “move down”. Clicking on move up, the current row should move up one row, analogously for “move down”.

What’s the easiest way to implement the corresponding move up and move down functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:27:41+00:00Added an answer on May 29, 2026 at 9:27 am

    I have revised my answer because it did not have enough detail previously

    The process involves connecting your buttons to a slot (or slots) that will look at the current selection, and move them by taking them from the view, and inserting them into new locations.

    The following example is actually using a QTableView + QStandardItemModel. The reason is because QTableWidget is much more limited since you can only use methods from the widget. Its a lot easier to be able to work directly with the model and selection model. Although, it is possible to rework this example for a QTableWidget if you use takeItem() multiple times to build up each row…

    Here is a fully working example:

    from PyQt4 import QtCore, QtGui
    from functools import partial
    
    class Dialog(QtGui.QDialog):
    
        DOWN    = 1
        UP      = -1
    
        def __init__(self, parent=None):
            super(Dialog, self).__init__(parent)
            self.resize(800,600)
    
            self.table = QtGui.QTableView(self)
            self.table.setSelectionBehavior(self.table.SelectRows)
    
            self.model = QtGui.QStandardItemModel(20, 6, self)
            self.table.setModel(self.model)
    
            self.upBtn = QtGui.QPushButton('Up', self)
            self.downBtn = QtGui.QPushButton('Down', self)
    
            self.mainLayout = QtGui.QVBoxLayout(self)
            self.mainLayout.addWidget(self.table)
    
            self.buttonLayout = QtGui.QHBoxLayout()
            self.buttonLayout.addWidget(self.upBtn)
            self.buttonLayout.addWidget(self.downBtn)
            self.mainLayout.addLayout(self.buttonLayout)
    
            self.upBtn.clicked.connect(partial(self.moveCurrentRow, self.UP))
            self.downBtn.clicked.connect(partial(self.moveCurrentRow, self.DOWN))
    
            self._initTable()
    
        def _initTable(self):
            for row in xrange(self.model.rowCount()):
                for col in xrange(self.model.columnCount()):
                    item = QtGui.QStandardItem('%d_%d' % (row+1, col+1))
                    self.model.setItem(row, col, item)
    
        def moveCurrentRow(self, direction=DOWN):
            if direction not in (self.DOWN, self.UP):
                return
    
            model = self.model
            selModel = self.table.selectionModel()
            selected = selModel.selectedRows()
            if not selected:
                return
    
            items = []
            indexes = sorted(selected, key=lambda x: x.row(), reverse=(direction==self.DOWN))
    
            for idx in indexes:
                items.append(model.itemFromIndex(idx))
                rowNum = idx.row()
                newRow = rowNum+direction
                if not (0 <= newRow < model.rowCount()):
                    continue
    
                rowItems = model.takeRow(rowNum)
                model.insertRow(newRow, rowItems)
    
            selModel.clear()
            for item in items:
                selModel.select(item.index(), selModel.Select|selModel.Rows)
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication([])
        d = Dialog()
        d.show()
        d.raise_()
        app.exec_()
    

    The init is simple and just sets up the table, model, and buttons. We connect both buttons to the same method using functools.partial, which is really convenient for wrapping up the same function call with different args. Then the table is just filled with 20×6 data.

    When a button is clicked, we make sure they have selected rows. For each selected row, we resolve its item (for re-selection later once it has moved), and determine the new row number by either adding or subtracting one. We also make sure its within the valid range to move, otherwise we skip it. Finally, we call takeRow() to remove the entire row as a list of indexes, and then insert that row back into the new row number. After that loop, we use the items we saved to look up the new indexes and reselect them again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider these two function definitions: void foo() { } void foo(void) { } Is
Say I have a QTableWidget and in each row there is a QComboBox and
Consider these two functions: 'Overload 1 <Extension()> Function ComputeReturnValue(Of TSource, TResult)(ByVal source As TSource,
Consider these two classes: public class A { B b; public A(B b) {
Consider I have two classes. Professor and TimePerDay. public class TimePerDay { private ObservableCollection<TimeSpan>
Consider this one piece of Perl code, $array[$x]->{foo}->[0]= January; I analyze this code as
Consider a scenario in which two applications have to share data among them. I
Consider the below two methods to delete a Set of Employees with the name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.