[Solved] How to add a list to ScrollArea and select one file from that?



  • Hi guys,

    I am new to Python and QT designer, I got stuck in ScrollArea where I want to insert file names (fnames) to scrollArea and select one file from that. Please help me fix it, Thanks in advance

    Code:

    @@class Main(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton1.clicked.connect(self.buttonClicked)
        self.ui.pushButton_2.clicked.connect(self.buttonToClose)
        
    def buttonToClose(self):
        QtGui.qApp.quit()
    
    def buttonClicked(self):
        fdir = QtGui.QFileDialog.getExistingDirectory()
        if fdir == '':
            self.ui.lineEdit1.setText('Please select the folder...')
        else:
            self.ui.lineEdit1.setText(fdir)
            fnames = os.listdir(path=fdir)
            
        self.listWidget = QtGui.QListWidget(self)
        self.someLayout.addWidget(self.listWidget)
        self.listWidget.addItems(fnames)
    

    if name == 'main':
    app = QtGui.QApplication(sys.argv)
    window = Main()
    window.show()
    sys.exit(app.exec_()) @@

    Error:

    Traceback (most recent call last):
    File "C:\Users\ramas\workspace\PlotGraph_1\Main.py", line 48, in buttonClicked
    self.someLayout.addWidget(self.listWidget)
    AttributeError: 'Main' object has no attribute 'someLayout'



  • Welcome here,

    apparently someone has to define the member-variable called 'someLayout'. At least in the code given it is never actually defined. So in the constructor you would expect something like

    @
    self.someLayout = QtGui.HBoxLayout()
    self.centralWidget().setLayout(self.someLayout)
    @

    Or something of that type.

    However, as you seem to be using a .ui File created witht he QtDesigner and then translated via pyuic I would suppose there is already some Layout set. So you probably want to use
    @
    self.centralWidget().layout().addWidget(self.listWidget)
    @

    instead. Hope it helps.

    Soraltan



  • Thanks for your help, since I had problem in ScrollArea have changed to listWidget and it worked...

    @ def buttonClicked(self):
    fdir = QtGui.QFileDialog.getExistingDirectory()
    if not fdir:
    self.ui.lineEdit1.setText('Please select the folder...')
    return

        self.ui.lineEdit1.setText(fdir)
        fnames = os.listdir(path=fdir)
        
        self.ui.listWidget.clear()
        self.ui.listWidget.addItems(fnames)@
    

    thanks for your help, much appreciated....



  • So, the code that caused the trouble is no longer there, right?!? Ok, so this thread is solved then (even by omitting the original problem)?!?

    I'm a bit confused here :-)



  • Sorry Soraltan, I have removed the old code and replaced with listWidget. This problem is solved for now, like you have mentioned by omitting the original problem. Thanks for your time and help.



  • Ok, glad it works for you now. Please mark this thread as solved by prepending [solved] within the title.

    Best

    Soraltan


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.