Listing files in ComboBox



  • Hello,

    iam newbie in Qt. I started a small Project by my own to learn more about the environment because it is also part of my apprenticeship. What iam looking for is a tutorial or a documentation about adding a path for the QComboBox. Iam aiming to list special files in it. But at the moment i just know that it is possible to add Elements to it by double clicking it in the Designer.
    How can i give a "Searchpath" to the QComboBox?

    Sorry for misunderstanding and kind regards
    Kopfii


  • Moderators

    You can add your items to the combo box using "addItem()":http://doc.qt.io/qt-5/qcombobox.html#addItem-2 and "addItems()":http://doc.qt.io/qt-5/qcombobox.html#addItems methods.

    Another option is to use the MVC approach and pass a model to the combo box, using "setModel()":http://doc.qt.io/qt-5/qcombobox.html#setModel method.



  • I'd use the model approach. It makes it very easy. You can use the QFileSystemModel to gather the files you need by using the appropriate filters, and just set it as the model for your combobox.



  • Hey, Thanks alot. I Found another Function which is called "QDirIterator Class":http://doc.qt.io/qt-5/qdiriterator.html. I think i will take that function. I read the documentation of this file.



  • Sure, that can work too. The benefit of using a QFileSystemModel is that:

    • it is already doing the QDirIterator for you
    • it works with large numbers of files because it is async
    • it will update if the contents of your file system change at runtime

    But, of course, you can make all of that yourself too, if you have enough time :)



  • [quote author="Andre" date="1423057672"]Sure, that can work too. The benefit of using a QFileSystemModel is that:

    • it is already doing the QDirIterator for you
    • it works with large numbers of files because it is async
    • it will update if the contents of your file system change at runtime

    But, of course, you can make all of that yourself too, if you have enough time :)[/quote]

    Thanks. I tested it and its really a bit "stressful" so i came back to use the listWidget idea again... But thats development..

    edit

    How can i give a folderpath for the list? At the moment it looks like this, but its wrong..

    @#include "taskplaner.h"
    #include "ui_taskplaner.h"
    #include <QListWidgetItem>
    #include <QtCore>
    #include <QtGui>

    Taskplaner::Taskplaner(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Taskplaner)
    {
    ui->setupUi(this);
    ui->listWidget->addItem("P:\Test");
    }

    Taskplaner::~Taskplaner()
    {
    delete ui;
    }@

    edit 2

    Found a great Documentation to QFileSystemModel. I think i will give it a try. Iam a bit scary about testing new things as newbie...

    Thank you a lot :)


Log in to reply
 

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