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 6661161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:14:43+00:00 2026-05-26T02:14:43+00:00

I have this barebones code, I am trying to make it so some items

  • 0

I have this barebones code, I am trying to make it so some items (all even for this example) are preselected inside the QListWidget. Where am I going wrong?

from PyQt4 import QtGui, QtCore, Qt

import sys

class Main(QtGui.QWidget):
    def __init__(self, parent=None):    
        QtGui.QWidget.__init__(self,parent)

        grid = QtGui.QGridLayout()

        self.builtinpatternslist = QtGui.QListWidget()
        self.builtinpatternslist.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
        for i in range(5):
            self.builtinpatternslist.addItem(QtGui.QListWidgetItem(str(i)))
            if i % 2 == 0:
                self.builtinpatternslist.setItemSelected(QtGui.QListWidgetItem(str(i)),True)


        grid.addWidget(self.builtinpatternslist,0,0)

        self.setLayout(grid)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    gui = Main()
    gui.show()
    gui.raise_()
    sys.exit(app.exec_())
  • 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-26T02:14:43+00:00Added an answer on May 26, 2026 at 2:14 am

    Each QtGui.QListWidgetItem(...) call creates a new item. What you are doing in that code is first creating an item, adding it to the list… And then, if it’s in an odd position, creating a whole new item and selecting it without ever adding it to the list.

    You need to make sure that you’re only dealing with one item each time: save the result of a single QtGui.QListWidgetItem(...) call in a variable, and then operate on it:

        for i in range(5):
            item = QtGui.QListWidgetItem(str(i))
            self.builtinpatternslist.addItem(item)
            if i % 2 == 0:
                self.builtinpatternslist.setItemSelected(item, True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited some verbose, repetitious code that I am trying to refactor. The
I have this code in jQuery, that I want to reimplement with the prototype
I have some code that is crashing in a large system. However, the code
I'm trying to get a bare-bones example of logging going in my ASP.NET application.
I have two classes which look like this in barebones form: @interface GraphNode {
Assume I have this barebones structure: project/ main.py providers/ __init.py__ acme1.py acme2.py acme3.py acme4.py
my JMenuBar is not showing AT ALL. This is just a barebones GUI. I
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=

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.