Display multiline cells in QTableView with QSqlTableModel
-
Hi, I have a QSqlTableModel with a table from a MySQL database, which I use to populate a table view with setModel function, this works fine but I want multiline cells in all rows of one column in order to be able to display some columns info of that row insted of display all the columns. My sql table is something like this:
And want something like this:
Here is an actual example:
Does anyone know how to achieve this with a QTableView and QSqlTableModel as source to fill the table view?
-
Hi, I have a QSqlTableModel with a table from a MySQL database, which I use to populate a table view with setModel function, this works fine but I want multiline cells in all rows of one column in order to be able to display some columns info of that row insted of display all the columns. My sql table is something like this:
And want something like this:
Here is an actual example:
Does anyone know how to achieve this with a QTableView and QSqlTableModel as source to fill the table view?
- subclass QStyledItemDelegate
- add a QTextDocument instance as a member to your custom delegate
- set the text to the text document in the delegates sizeHint() and paint() overloads
- in sizeHint() return the preferred size of the document
- in the paint() method use the text-documents drawContent() method
-
-
Thanks for your answer, could you provide me a little example? I'm sorry but I'm new in Qt and I I still do not understand subclassing in Qt and delegates, but I think with a simple example I could understand how to apply it to my case.
Thanks in advance.@KevinR
You can follow the StarDelegate example. Although you still just need the sizeHint() and paint() methods for your case. -
@KevinR
You can follow the StarDelegate example. Although you still just need the sizeHint() and paint() methods for your case.@raven-worx said in Display multiline cells in QTableView with QSqlTableModel:
You can follow the StarDelegate example. Although you still just need the sizeHint() and paint() methods for your case.
@raven-worx I tried to reimplement sizeHint() and paint() methods in order to display a static text in all the cells of certain column as first step to achieve what I want with no success. The result was the text appears in another column and only in some cells. It also appears and disappears randomly when I scroll the table or if i click on the cell. It is displayed on top of the original text of the column instead of replacing it, too.
This is the relevant code://qstyledcustomdelegate.cpp QStyledCustomDelegate ( QObject *parent = 0) : QStyledItemDelegate(parent) { document=new QTextDocument; document->adjustSize(); } void QStyledCustomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { document->setPlainText("Hello"); document->adjustSize(); document->drawContents(painter); } QSize QStyledCustomDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { document->adjustSize(); return document->size().toSize(); } //inventariolpm.cpp #include "qstyledcustomdelegate.h" #include "inventariolpm.h" InventarioLPM::InventarioLPM(QWidget *parent) : QMainWindow(parent), ui(new Ui::InventarioLPM) { ui->setupUi(this); ui-> productsTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); ui->productsTable->setAutoScroll(true); this->dataBase=QSqlDatabase::addDatabase("QMYSQL"); this->dataBase.setDatabaseName("sampledatabase"); this->dataBase.setHostName("localhost"); this->dataBase.setPort(3306); this->dataBase.setUserName("kevin"); this->dataBase.setPassword("kevin"); this->dataBase.open(); this->mModel= new QSqlTableModel(this,this->dataBase); this->mModel->setTable("sqltable"); this->mModel->select(); this->ui->productsTable->setModel(this->mModel); ui->productsTable->setItemDelegateForColumn(1, new QStyledCustomDelegate(ui->productsTable)); //I try to display the text in second column }
The result was this:
-
@raven-worx said in Display multiline cells in QTableView with QSqlTableModel:
You can follow the StarDelegate example. Although you still just need the sizeHint() and paint() methods for your case.
@raven-worx I tried to reimplement sizeHint() and paint() methods in order to display a static text in all the cells of certain column as first step to achieve what I want with no success. The result was the text appears in another column and only in some cells. It also appears and disappears randomly when I scroll the table or if i click on the cell. It is displayed on top of the original text of the column instead of replacing it, too.
This is the relevant code://qstyledcustomdelegate.cpp QStyledCustomDelegate ( QObject *parent = 0) : QStyledItemDelegate(parent) { document=new QTextDocument; document->adjustSize(); } void QStyledCustomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { document->setPlainText("Hello"); document->adjustSize(); document->drawContents(painter); } QSize QStyledCustomDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { document->adjustSize(); return document->size().toSize(); } //inventariolpm.cpp #include "qstyledcustomdelegate.h" #include "inventariolpm.h" InventarioLPM::InventarioLPM(QWidget *parent) : QMainWindow(parent), ui(new Ui::InventarioLPM) { ui->setupUi(this); ui-> productsTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); ui->productsTable->setAutoScroll(true); this->dataBase=QSqlDatabase::addDatabase("QMYSQL"); this->dataBase.setDatabaseName("sampledatabase"); this->dataBase.setHostName("localhost"); this->dataBase.setPort(3306); this->dataBase.setUserName("kevin"); this->dataBase.setPassword("kevin"); this->dataBase.open(); this->mModel= new QSqlTableModel(this,this->dataBase); this->mModel->setTable("sqltable"); this->mModel->select(); this->ui->productsTable->setModel(this->mModel); ui->productsTable->setItemDelegateForColumn(1, new QStyledCustomDelegate(ui->productsTable)); //I try to display the text in second column }
The result was this:
@KevinR said in Display multiline cells in QTableView with QSqlTableModel:
Try this:void QStyledCustomDelegate::initTextDocument(const QModelIndex & index) { document->setHtml( index.data(Qt::DisplayRole).toString() ); document->setTextWidth( QWIDGETSIZE_MAX ); document->setTextWidth( document->idealWidth() ); } QSize QStyledCustomDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { this->initTextDocument(index); return document->size().toSize(); } void QStyledCustomDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if( index.column() == XXX ) { this->initTextDocument(index); painter->save(); painter->translate( option.rect.topLeft() ); document->drawContents(painter); } else QStyledItemDelegate::paint(painter,option,index); }
Note: the paint method is still lacking of the selection background.