Qt's model/view architecture

浪尽此生 提交于 2019-12-23 04:43:57

问题


After making connection with the database , I have written the following code, but after execution view's inner cells are empty. WHy?

enum {
        name = 0,
        clas = 1,
        launched = 2
    };

      QSqlTableModel* model = new QSqlTableModel;
       model->setTable("ships");
       model->setSort(name, Qt::AscendingOrder);
       model->setHeaderData(name, Qt::Horizontal, "Name");
       model->setHeaderData(clas, Qt::Horizontal, "class");
       model->setHeaderData(launched, Qt::Horizontal, "launched");

       model->select();   

     QTableView *  view = new QTableView;   

           view->setModel(model);
           view->setSelectionMode(QAbstractItemView::SingleSelection);
           view->setSelectionBehavior(QAbstractItemView::SelectRows);
           view->resizeColumnsToContents();
           view->setEditTriggers(QAbstractItemView::NoEditTriggers);
           QHeaderView *header = view->horizontalHeader();
           header->setStretchLastSection(true);
           view->show(); 

来源:https://stackoverflow.com/questions/41078500/qts-model-view-architecture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!