Qt World Summit: Submit your Presentation

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) :
    ui(new Ui::MainWindow)
    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!";
            qDebug() << "The database is open!";
        QSqlQuery query("SELECT ID,Pic FROM Items");
              qDebug() << "The query is active.";
             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 ();


    delete ui;

    Thank you for your help.

  • Hi,
    May be you can set font using QFont

              ui->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 after

    ui->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.

  • Lifetime Qt Champion


    Arial is not a variable that exists. You should have something like setFont(QFont("Arial"));

  • Thank you. Unfortunately it doesn't do anything. Is the location in the code correct? Is it possible that some settings somewhere else overrides the code?
    Thank you.

  • Hi,

    ui->tableView->horizontalHeader()->setStyleSheet ("QHeaderView {font: 14pt Arial; color: blue;}");

  • Thank you. It worked.

  • @gabor53
    You could also try

         QFont font("Arial", 20, QFont::Bold);
         ui->tableView->horizontalHeader()->setFont( font );

  • 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}");

    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"



    Actually everything is stated in the Qt Help, - just need to know how to correctly use it :)