QStringListModel

QStringListModel

Create a new Qt widgets application and add a list view at the top and a button at the bottom. Setup the widget .h file as follows with the on_pushbutton_clicked() slot and the string list model member.




class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
    QStringListModel *strListModel;
};



Initialize the StringListModel



Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    strListModel = new QStringListModel(this);
    ui->listView->setModel(strListModel);
}

Populate the model as follows:



void Widget::on_pushButton_clicked()
{

   QProcess process;

   QString processName = "ls";
   QStringList params;
   params << "-a" << "/Users/cgavini";
   process.start(processName,params);
   process.waitForFinished();
   QString output(process.readAllStandardOutput());
   qDebug() << "Output is " << output;


   auto strList =  output.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
   strListModel->setStringList(strList);


}

Comments

Popular posts from this blog

QTreeView and QTableView dynamic changes

C++ strings and string_view