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

QListView issue



  • Hello all,

    I am trying to show a QstringList into a ListView, first I create a model Object from class QStringListModel and do a setStringList(listname)
    then I apply the following code:

    listView = new QListView;
       listView->setModel(model);
       listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
    
    

    But it says that cannot initialize a parameter of type QAbstractItemModel with an lvalue of type QStringlistModel

    Can someone help me in solving this?

    Tnaks


  • Qt Champions 2019

    QListView::setModel() takes a pointer.



  • I know, but even passing memory address of model it prompts the following issue “cannot initialize a parameter of type QAbstractItemModel with an lvalue of type QStringlistModel”


  • Qt Champions 2019

    I don't think so - please show us your modified code including the definition of the model variable.



  • Here is the file.h

    class TeamDialog :QDialog
    {
    public:
        TeamDialog(const QStringList &leaders, QWidget *parent);
        void insert();
        void del();
        QStringList leaders() const;
    private:
        QStringListModel *model;
        QListView *listView;
    
    };
    

    and here de cpp

    
    TeamDialog::TeamDialog(const QStringList &leaders, QWidget *parent):QDialog(parent)
    {
    
       model = new QStringListModel(this);
       model->setStringList(leaders);
    
       listView = new QListView;
       listView->setModel(&model);
       listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
       
       
    
    }
    
    void TeamDialog::insert(){
        int row = listView->currentIndex().row();
        model->insertRows(row,1);
    
        QModelIndex index = model->index(row);
        listView->setCurrentIndex(index);
        listView->edit(index);
    
    }
    
    void TeamDialog::del(){
        model->removeRows(listView->currentIndex().row(),1);
    }
    
    QStringList TeamDialog::leaders() const{
        return model->stringList();
    }
    

  • Qt Champions 2019

    @jss193 said in QListView issue:

    listView->setModel(&model);

    now you're passing a pointer to a pointer to a QStringListModel

    listView->setModel(model);



  • I tried what you say and prompts the same issue as before “cannot initialize a parameter of type QAbstractItemModel with an lvalue of type QStringlistModel”.


  • Qt Champions 2019

    #include <QtWidgets>
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        QStringListModel *model = new QStringListModel;
        QListView *listView = new QListView;
        listView->setModel(model);
    }
    

    This compile fine for me - there must be something wrong with your headers - maybe forgot to include QStringListModel oder QListView header file?



  • Thanks it works it prompts the same issue but compiles well, then how would I pass an object of that class to a QListviewWidget?

    Thank you very much


  • Lifetime Qt Champion

    Hi,

    Do you mean you are trying to set a QStringList model on a QListWidget ? If so, you can't. The convenience views like QListWidget or QTreeWidget already have a model and it can't be changed.



  • Thanks , then I should instantiate the class in main window through constructor method, true?


  • Lifetime Qt Champion

    That's unrelated, if you want to use a QStringListModel, then use a QListView and you should be good to go.



  • Thanks, thats just what I meant


  • Lifetime Qt Champion

    Here is your code modified to work:
    @jss193 said in QListView issue:

    model = new QStringListModel(this);
    model->setStringList(leaders);

    listView = new QListView;
    listView->setModel(model);
    listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);


Log in to reply