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(! ())
            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 );

Log in to reply

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