Formatting tableView header text
-
Hi,
Is it possible to format the header of a tableView? I'd like to change the font size, color and font.```
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase db;
db = QSqlDatabase::addDatabase ("QSQLITE");
db.setDatabaseName ("C:/Programming/Qtsamples/Image_from_db_to_Table/db.db");if(!db.open ()) { qDebug() << "The database is NOT open!"; } else { qDebug() << "The database is open!"; } QSqlQuery query("SELECT ID,Pic FROM Items"); if(query.isActive()==true) { qDebug() << "The query is active."; } else { qDebug() << "The query is NOT active."; } query.first (); int ID ; QStandardItemModel *smodel = new QStandardItemModel; QStandardItem *Item = new QStandardItem(); QStandardItem *Item2 = new QStandardItem(); Item->setData (ID = query.value (0).toInt (),Qt::DisplayRole); qDebug() << "ID = " << Item->data ().toInt (); smodel->setItem (0,0,Item); QByteArray ByteArray; ByteArray = query.value (1).toByteArray (); QPixmap Pixmap; Pixmap.loadFromData (ByteArray); Pixmap = Pixmap.scaled (100,100,Qt::KeepAspectRatio); Item2->setData (QVariant(Pixmap),Qt::DecorationRole); smodel->setItem (0,1,Item2); // ui->tableView->verticalHeader ()->setDefaultSectionSize (100); ui->tableView->setEditTriggers (QAbstractItemView::NoEditTriggers); smodel->setHeaderData (0,Qt::Horizontal, QObject::tr ("ID")); smodel->setHeaderData (1,Qt::Horizontal, QObject::tr ("Picture")); ui->tableView->setModel (smodel); ui->tableView->resizeColumnsToContents (); ui->tableView->resizeRowsToContents ();
db.close ();
}
MainWindow::~MainWindow()
{
delete ui;
}Thank you for your help.
-
Hi,
May be you can set font using QFontui->tableView->horizontalHeader()->setFont( font);
Size using
setStyleSheet
ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView { font-size: 10pt; }");
color using
ui->tableView->horizontalHeader()->setStyleSheet("color: blue;");
-
Hi,
Thank you for your help.
I added the following to the code afterui->tableView->resizeRowsToContents ();
The added code:
ui->tableView->horizontalHeader ()->setFont (Arial); ui->tableView->horizontalHeader ()->setStyleSheet ("QheaderView {font-size: 14pt;}"); ui->tableView->horizontalHeader ()->setStyleSheet ("color: blue;"); The first line generates an error message: C:\Programming\Qtsamples\Image_from_db_to_Table\mainwindow.cpp:65: error: 'Arial' was not declared in this scope ui->tableView->horizontalHeader ()->setFont (Arial); The rest changes the color but not the font size. What did I do incorrectly? Thanks. ^
-
Hi,
Arial is not a variable that exists. You should have something like
setFont(QFont("Arial"));
-
I think my topic fit quit good to this post so I reopen a part? :)
I determined also that this way
ui->tableView->horizontalHeader()->setFont( correctParams);
does not have any effects on the header only this works
ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView {font: 14pt}");
BUT
I would like not to format the whole header instead I prefer to format some selected "section" (or let say cells of the header pool)I perform it in this way and it returns 1 / true
qDebug() << ui->TV_DBIn->model()->setHeaderData(2,Qt::Horizontal,QTextFormat(QTextFormat::FontWeight),Qt::FontRole);
But there is no effect.
I hope it does not to do with "QStyleFactory"