Check if qlistwidget item has been selected

Hi,

How do i check if the user has selected an item in my qlistwidget? i could not find any resources elsewhere.

this is what i want to achieve:

if an item has not been selected when the user presses the delete button. then a message box should appear saying please select an item.

this is my delete function for the delete button

def delete_it(self):

        # create a data base or connect to one
        conn = sqlite3.connect('ab.db')
        # create a cursor
        c = conn.cursor()

        symbol = self.i_listwidget.currentItem().text()
        clicked = self.i_listwidget.currentRow()
        self.i_listwidget.takeItem(clicked)

        
        c.execute("DELETE FROM tone WHERE first_name = '%s'" % symbol)

        # commit the changes
        conn.commit()

        # close our connection
        conn.close() 

Have you tried reading the qt documentaton on QListWidget?
https://doc.qt.io/qt-6/qlistwidget.html
I think it will answer you question.

Sorry i didn’t understand the website and what it says about the qlistwidget. I’m in year 7.

Let’s try to break down the problem step by step. In this way, you will be able to understand what the docs are saying on your own.

I do know how to convert a school year into your age, that is country specify.

Qt, via PyQt, is an amazing tool for making GUI programs.

You are asking about the selection in the QListWidget.
On the page I pointed you to you will find that it documents a function to get the selected items.

https://doc.qt.io/qt-6/qlistwidget.html#selectedItems

Call this function and see what it returns when you have
no items selected, one item selected and more then one selected.

Does this help?

It gives me a syntax error when I use a colon.

C++ uses : and python use . So replace the : with . and try again.

Thankyou so much, it worked.

I wanted to check if the item was selected by the user so i could put up a yes and no message box to double check if the user wanted to delete the item from the data base. When you told me to use ‘.’ instead of ‘:’ i used another piece of code and it was the solution for my problem:

    def delete_it(self):
        
        msgBox = QMessageBox()
        msgBox.setWindowTitle("title")
        msgBox.setText("Question")
        msgBox.setStandardButtons(QMessageBox.Yes)
        msgBox.addButton(QMessageBox.No)
        msgBox.setDefaultButton(QMessageBox.No)
        if(msgBox.exec() == QMessageBox.Yes):

            print("1")

            # create a data base or connect to one
            conn = sqlite3.connect('ab.db')
            # create a cursor
            c = conn.cursor()

            symbol = self.i_listwidget.currentItem().text()
            clicked = self.i_listwidget.currentRow()
            self.i_listwidget.takeItem(clicked)

            c.execute("DELETE FROM tone WHERE first_name = '%s'" % symbol)

            # commit the changes
            conn.commit()

            # close our connection
            conn.close() 

            # clear the item box
            self.fn_lineedit.setText("")
            self.ln_lineedit.setText("")
            self.pn_lineedit.setText("")
            self.ea_lineedit.setText("")
            self.a_textedit.setText("")

                

                

            self.close_pushbutton.setEnabled(False)
            self.save_pushbutton.setEnabled(False)

            self.disable()
            self.disable_dd()
            self.disable_u()
            self.disable_c()
        else:

            pass
1 Like