Nominate our 2022 Qt Champions!

[solved] QListView delegated items are displayed only by in runtime created QListView

  • Hello all,

    I think my problem is easy to solve and needs only short explanation.

    My problem description: If I add a QListView to my MainWindow in Qt-Creator designer and overwrite it's pointer in my code with derived QListView, my custom items are not displayed in the QListView. But if I create my derived QListView and add it to the ui in MainWindow constructor, the items are displayed well.

    My code:

    @QListView *listView = new DerivedListView(); // ignores MouseMove events
    ui->listView = listView; // this QListView was added in Designer and doesn't display the items
    ui->verticalLayout->addWidget(listView); // this QListView displays the items well.@

    Later the QListView gets a derived AbstractItemDelegate and AbstractListModel.

    What could be the reason for this behavior?

    Thank you.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    @ui->listView = listView@

    You only replace the pointer value so you still see the original listView since it's already been layouted in your MainWindow, call show on it and see what happens.


    Now, you are adding your new widget to the layout of your Designer based MainWindow, thus making your custom list view managed by it

  • If I call the show methode the ListView will be displayed outside the MainWindow.

    Ok, thank you.

  • Lifetime Qt Champion

    If your derived QListView only ignores mouse event, you could avoid it and set an eventFilter on it, it might simplify your code (or depending on why you want mouse event to be ignored, you can also just disable the selection mode)

  • I have to ignore the DragSelection in MultiSelectionMode. But because of MVC architecture I want to separate the QListView actions from MainWindow - and want avoid the event filtering on MainWindow.

  • Lifetime Qt Champion

    You can also create a dedicated object that does the filtering

Log in to reply