Moving items up and down in a QListWidget?

2024/9/21 3:36:01

In a QListWidget I have a set of entries. Now I want to allow the user to sort (reorder) these entries through two buttons (Up/Down).

Here's part of my code:

def __init__(self):QtGui.QMainWindow.__init__(self)self.ventana = Ui_MainWindow()self.ventana.setupUi(self)self.connect(self.ventana.btExit, QtCore.SIGNAL('clicked()'), QtCore.SLOT('close()'))self.connect(self.ventana.btAdd, QtCore.SIGNAL('clicked()'), self.addButton)self.connect(self.ventana.btQuit, QtCore.SIGNAL('clicked()'), self.quitButton)self.connect(self.ventana.btQuitAll, QtCore.SIGNAL('clicked()'), self.quitAllButton)self.connect(self.ventana.btUp, QtCore.SIGNAL('clicked()'), self.upButton)self.connect(self.ventana.btDown, QtCore.SIGNAL('clicked()'), self.downButton)def addButton(self):fileNames = QtGui.QFileDialog.getOpenFileNames(self, 'Agregar archivos')self.ventana.listWidget.addItems(fileNames)def quitButton(self):item = self.ventana.listWidget.takeItem(self.ventana.listWidget.currentRow())item = Nonedef quitAllButton(self):self.ventana.listWidget.clear()def upButton(self):# HOW TO MOVE ITEM
Answer

Well, after trying in different ways, this is solved by taking the selected entry and inserting it into a new position.

For the Up Button is something like this:

    currentRow = self.ventana.listWidget.currentRow()currentItem = self.ventana.listWidget.takeItem(currentRow)self.ventana.listWidget.insertItem(currentRow - 1, currentItem)

And for the Down Button it's the same, except that in the third line the "-" sign is changed by a "+".

https://en.xdnf.cn/q/72277.html

Related Q&A

How to resize a tiff image with multiple channels?

I have a tiff image of size 21 X 513 X 513 where (513, 513) is the height and width of the image containing 21 channels. How can I resize this image to 21 X 500 X 375?I am trying to use PILLOW to do …

Losing elements in python code while creating a dictionary from a list?

I have some headache with this python code.print "length:", len(pub) # length: 420pub_dict = dict((p.key, p) for p in pub)print "dict:", len(pub_dict) # length: 163If I understand t…

How to set Font size or Label size to fit all device

How to set kivy font size or label size so that it will fit all phone-device screen? (fit by means does not overlap with the boundary, rectangle, or even the screen)I know the Buttons and some other W…

Using tweepy to access Twitters Streaming API

Im currently having trouble getting example code for using tweepy to access Twitters Streaming API to run correctly (err...or at least how I expect it to run). Im using a recent clone of tweepy from Gi…

Jupyter notebook, how to run multiple cells simultaneously?

I defined a python function which run a bash script. Lets say the function is: calc(x,y,z). If I run this function in python with some variables,>>> calc(1,2,3)It generates a C code which simu…

How to make a slice of DataFrame and fillna in specific slice using Python Pandas?

The problem: let us take Titanic dataset from Kaggle. I have dataframe with columns "Pclass", "Sex" and "Age". I need to fill NaN in column "Age" with a median f…

Pythons difflib SequenceMatcher speed up

Im using difflib SequenceMatcher (ratio() method) to define similarity between text files. While difflib is relatively fast to compare a small set of text files e.g. 10 files of 70 kb on average compar…

create an asymmetric colormap

I am creating a colormap to map colors in a folium choropleth map, using code from here:from branca.colormap import linearcolormap = linear.RdBu.scale(df.MyValue.min(),df.MyValue.max())colormapAs you c…

NLTK - Get and Simplify List of Tags

Im using the Brown Corpus. I want some way to print out all the possible tags and their names (not just tag abbreviations). There are also quite a few tags, is there a way to simplify the tags? By sim…

PolynomialFeatures object has no attribute predict

I want to apply k-fold cross validation on the following regression models:Linear Regression Polynomial Regression Support Vector Regression Decision Tree Regression Random Forest RegressionI am able t…