c++qtqlistviewqitemdelegateqstyleditemdelegate

QListView with Two QTextEdit 's as Item


In Qt Widget Application (c++), a part of my *.ui file consists of a QListView with two QTextEdits as its items like the below figure.

enter image description here

As you can see custom widget includes two QTextEdit that each one has its text and stylesheet. As I searched on the net, there are solutions like HtmlDelegate classes for rendering text of items on QListView. But these classes only present ONE QTextEdit as item for QListView. In future, I want sync the QListView scrol status with a QMultimedia status like Podcast apps. Does anyone have any idea?


Solution

  • well first of all you should create your custom widget like this:

    enter image description here

    Then you add it to your Model of QListView by using setIndexWidget function like this:

        QStandardItemModel *model = new QStandardItemModel(120, 1);
        ui->listView->setModel(model);
    
        for (int r = 0; r < 120; r++)
        {
            ui->listView->setIndexWidget(model->index(r, 0), new CustomTextEdits);
        }
    
    

    Final Result:

    enter image description here

    you can see the source code here:

    https://github.com/parisa-hr/SO-addwidgettoqlistview