How to Change QTableView Item Text?



  • Hello all.
    I Have QTableView.
    I have the information QDatabase.

    if(globalall->db.open())
    {
        QSqlQueryModel* users = new QSqlQueryModel();
    
        QSqlQuery* query = new QSqlQuery();
    
        query->prepare("SELECT * FROM valuta");
    
        query->exec();
    
        users->setQuery(*query);
    
        ui->ValutaName->setModel(users);
        ui->ValutaName->verticalHeader()->hide();
        ui->ValutaName->horizontalHeader()->hide();
    
        globalall->db.close();
    }
    

    I want to change the text on DoubleClicked When index = (0,0) || (0,1) || (0,2).

    It Is Posibly?


  • Qt Champions 2016

    @Taz742 said in How to Change QTableView Item Text?:

    QSqlQueryModel

    Hi
    QSqlQueryModel is readonly
    http://doc.qt.io/qt-5/qsqlquerymodel.html

    So i think you should use other model to allow editing.

    This is one sample
    http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html



  • Welcome to the memory leak festival!!

    As @mrjj correctly pointed out:

    QSqlQueryModel is readonly

    if(globalall->db.open())
    {
        QStandardItemModel* users = new QStandardItemModel(this);
        QSqlQuery query;
        query.prepare("SELECT * FROM valuta");
    int fieldCount=0;
    if(query.exec()){
    for(bool firstRow=true;query.next();){
    const QSqlRecord currRecord=query.record();
    if(firstRow){
    fieldCount=currRecord.count();
    firstRow=false;
    users->insertColumns(0,fieldCount);
    for(int i=0;i<fieldCount;++i)
    users->setHeaderData(i,Qt::Horizontal,currRecord.fieldName(i));
    }
    const int newRow=users->rowCount();
    users->insertRow(newRow);
    for(int i=0;i<fieldCount;++i)
    users->setData(users->index(newRow,i),currRecord.value(i));
    }
    }
        ui->ValutaName->setModel(users);
        ui->ValutaName->verticalHeader()->hide();
        ui->ValutaName->horizontalHeader()->hide();
    
        globalall->db.close();
    }
    

    I want to change the text on DoubleClicked When index = (0,0) || (0,1) || (0,2).

    If you want only certain cells/rows/columns to be editable you'll need a proxy model in between, something like http://pastebin.com/ttF5amvC (or use QStandardItem::setFlags but I refuse on poorly justifiable moral grounds)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.