Making Qtableview cell autosize



  • The following code is used by me in my qt c++ application to retrieve data from a database when the user selects an option from a combo box and click the button! Though the data is retreived the text is too long to fully display in the table view cell so only a part of the text is visible! How can I adjust tableview cell size so that the the text is fully visible?

    void Dialog::on_pushButton_clicked()
    {
    {

        QSqlQueryModel *modal = new QSqlQueryModel();
        if(QSqlDatabase::contains("MyConnection")){
    
            QSqlQuery* qry=new QSqlQuery(QSqlDatabase::database("MyConnection"));
            if(ui->comboBox->currentText()=="NEW"){
    
            qry->prepare("Select DISPLAY_NAME from SER_MESSAGES where DISPLAY_NAME like '%New%' ");
            }
    
            else if(ui->comboBox->currentText()=="OLD"){
            qry->prepare("Select DISPLAY_NAME from SER_MESSAGES where DISPLAY_NAME like '%Old%' ");
            }
           
            else if(ui->comboBox->currentText()=="CANCEL"){
            qry->prepare("Select DISPLAY_NAME from SER_MESSAGES where DISPLAY_NAME like '%Cancel%' ");
            }
            
            qry->exec();
            modal->setQuery(*qry);
          ui->tableView->setModel(modal);
        }
    }


  • @Kushan
    Hi,

    go through the link hope it helps,
    http://doc.qt.io/qt-5/qheaderview.html#ResizeMode-enum



  • @Kushan
    Or you can use resizeColumnsToContents() method for the same.



  • This post is deleted!


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