Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QlistView is empty or not?



  • Hi,
    Is there a method that check if a QlistView is empty or not?



  • @
    if (theView->model()->rowCount() == 0) {
    //do your thing
    }
    @
    Or something along those lines...



  • Hope it's ok to reply to this thread, even it's been created in the jura ära already? ;)

    I have exactly the same question, even have the same solution, but the point is, how can find someone find out, if not the MODEL is empty, but the QListView is empty, means, it does not have a model yet attached? In my case I have a certain use case where there were no items found yet to add (on purpose), so even the model wasn't created and added to the QListView item. So view->model()->rowCount() produces a segmentation fault - which is perfectly ok, but how can I check before this request, if the model does exist already? view->model() == 0 or something? My trials were rejected by the compiler so far.

    Cheers,
    Stephan



  • Hope it's ok to reply to this thread, even it's been created in the jura ära already? ;)

    I have exactly the same question, even have the same solution, but the point is, how can find someone find out, if not the MODEL is empty, but the QListView is empty, means, it does not have a model yet attached? In my case I have a certain use case where there were no items found yet to add (on purpose), so even the model wasn't created and added to the QListView item. So view->model()->rowCount() produces a segmentation fault - which is perfectly ok, but how can I check before this request, if the model does exist already? view->model() == 0 or something? My trials were rejected by the compiler so far.

    Cheers,
    Stephan



  • Check if calling model() returns 0 on your view.



  • Check if calling model() returns 0 on your view.



  • Ok, that works, why did I not get to that idea??? ;)

    Would you use "0" or rather "NULL"?

    Stephan



  • Ok, that works, why did I not get to that idea??? ;)

    Would you use "0" or rather "NULL"?

    Stephan



  • 0 in c++/98, nullptr in c++/11. NULL is C.



  • 0 in c++/98, nullptr in c++/11. NULL is C.



  • Great, thanks!

    Stephan



  • Great, thanks!

    Stephan


Log in to reply