PDA

View Full Version : QComboBox does not function properly



Zyron
25th November 2017, 00:42
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.

Zyron
26th November 2017, 10:00
Ok

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

d_stranz
26th November 2017, 16:54
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.

Zyron
27th November 2017, 02:48
Thank you, d_Stranz

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

Here is my code


#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui

class Window(QtGui.QMainWindow):

def __init__(self):
super(Window, self).__init__()
self.setGeometry(200,200,700,700)
cb = QtGui.QComboBox(self)
cb.move(20,40)

tmod = open('list.txt', 'r')
for i in range(4):
cb.addItem(str(i))
tmodel.close()

self.show()

app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())



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

cb.addItem(str(i)).

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

high_flyer
27th November 2017, 10:18
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?


tmod = open('list.txt', 'r')
for i in range(4):
cb.addItem(str(i))
tmodel.close()

d_stranz
27th November 2017, 16:44
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?

Zyron
28th November 2017, 01:28
I see there is an transcription error, ooops it should be:


tmod = open('list.txt', 'r')
for i in range(4):
cb.addItem(str(i))
tmod.close()



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.

d_stranz
28th November 2017, 21:12
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 (http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python). 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.