Results 1 to 8 of 8

Thread: QComboBox does not function properly

  1. #1
    Join Date
    Nov 2017
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QComboBox does not function properly

    Hi
    I am a newbie

    I have a QComboBox in my code that I have populates with text from a file when I run the script the combobox displays but it selects every thing in
    the the combobox ie I can't select one item, when i hover over the combox it shows all the items vertically.
    I am using python
    Thank you, any help would be apppreciated.

  2. #2
    Join Date
    Nov 2017
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QComboBox does not function properly

    Ok

    What is the "Qt" method of populating a combobox from a file.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox does not function properly

    What is the "Qt" method of populating a combobox from a file.
    For each line in the file:
    - read the line
    - insert it into the combobox

    Do you really expect a serious answer to a question like that?

    Why don't you give us a bit of help by posting your non-working code, especially the parts where you create the combobox, set its properties, and populate it with the file contents? If we could see what you are doing, we might be able to point out what's wrong.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Nov 2017
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: QComboBox does not function properly

    Thank you, d_Stranz

    On reflection I relise that my previous post was not the best, sorry about that.

    Here is my code

    Qt Code:
    1. #!/usr/bin/env python
    2. import sys
    3. from PyQt4 import QtCore, QtGui
    4.  
    5. class Window(QtGui.QMainWindow):
    6.  
    7. def __init__(self):
    8. super(Window, self).__init__()
    9. self.setGeometry(200,200,700,700)
    10. cb = QtGui.QComboBox(self)
    11. cb.move(20,40)
    12.  
    13. tmod = open('list.txt', 'r')
    14. for i in range(4):
    15. cb.addItem(str(i))
    16. tmodel.close()
    17.  
    18. self.show()
    19.  
    20. app = QtGui.QApplication(sys.argv)
    21. GUI = Window()
    22. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    When I run this code I get the numbers 0 - 3 in the combobox. The list contains name eg Bob Sue etc

    In my first post I identified a different senerio in part i solved that by putting the 'str' in this line of code
    Qt Code:
    1. cb.addItem(str(i))
    To copy to clipboard, switch view to plain text mode 
    .

    And I used the range(4) for testing, and that there are only 4 items in the file, I have not figured out what to put there

    Thank you for your help

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox does not function properly

    Your problem has nothing to do with Qt, but with basic programming knowledge.
    This type of a problem is outside the scope of this forum.
    To make my point, can you explain in words what this snippet of your code is doing?
    Qt Code:
    1. tmod = open('list.txt', 'r')
    2. for i in range(4):
    3. cb.addItem(str(i))
    4. tmodel.close()
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox does not function properly

    For each line in the file:
    - read the line
    - insert it into the combobox
    To reinforce what high_flyer is asking, where are you performing the first step in this loop ("read the line")?

    And since everything in python depends on indentation, what do you think happens to "tmod" (or "tmodel" - you seem to have not cut and pasted very carefully) after the first time through the for loop?
    Last edited by d_stranz; 27th November 2017 at 18:42.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Nov 2017
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QComboBox does not function properly

    I see there is an transcription error, ooops it should be:
    Qt Code:
    1. tmod = open('list.txt', 'r')
    2. for i in range(4):
    3. cb.addItem(str(i))
    4. tmod.close()
    To copy to clipboard, switch view to plain text mode 


    The first line opens the file called list.txt for reading as indicated by the 'r'.
    The second line is a for loop , that loops through 4 times -range(4)
    the third line adds the names(re-previous post) to the combobox
    The fourth line closes the the file.

    The above snippet of code works the problem is, it display is the numbers from 0 to 3. My question is how do I display the name in the file?. My suspicison is I should be not
    be using range(4)


    I will be away on business for the next few days, I will catch up with you again on Friday in my part of the world.
    Thank you

    PS the code snippet does not show once posted the way I wrote it in the message.
    Last edited by Zyron; 28th November 2017 at 02:33.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox does not function properly

    it display is the numbers from 0 to 3
    Well, if you set up a for loop that goes from 0 to 3 (i.e. range(4)) using the index "i" and you tell the combobox to insert the conversion of the current value of "i" to a string, why are you surprised when your combobox contains the numbers 0 through 3?

    Simply opening a file and assigning a reference to the file object to the variable "tmod" does nothing more than that. It doesn't read anything. Even if it did, there is nothing in your code that does anything with the values that might be read. That fact that you've indented the for loop under the open() statement means nothing.

    I think you need to start with reading about python file I/O. As a hint, you need to open the file (which you have done) and then while looping until you reach the end of the file, read each line into a string and append that string to the combobox. When you reach the end of the file, you exit the loop and close the file.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QCombobox contents not adjusting itself properly
    By anju123 in forum Qt Programming
    Replies: 4
    Last Post: 6th April 2016, 22:11
  2. Replies: 1
    Last Post: 27th November 2014, 10:11
  3. QComboBox Signals doesn't invoke the called function
    By ladiesfinger in forum Qt Programming
    Replies: 6
    Last Post: 30th December 2010, 17:20
  4. How to pass a QComboBox to a function?
    By Ricardo_arg in forum General Programming
    Replies: 4
    Last Post: 9th March 2008, 23:16
  5. Contents not Properly Visible in QComboBox
    By merry in forum Qt Programming
    Replies: 4
    Last Post: 25th September 2007, 13:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.