Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Formatting tableView header text
Forum Updated to NodeBB v4.3 + New Features

Formatting tableView header text

Scheduled Pinned Locked Moved Solved General and Desktop
tableviewheaderformatting
10 Posts 5 Posters 15.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gabor53
    wrote on 31 Dec 2015, 04:47 last edited by
    #1

    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.
    1 Reply Last reply
    0
    • R Offline
      R Offline
      Ratzz
      wrote on 31 Dec 2015, 07:33 last edited by Ratzz
      #2

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

      --Alles ist gut.

      1 Reply Last reply
      2
      • G Offline
        G Offline
        gabor53
        wrote on 31 Dec 2015, 15:10 last edited by
        #3

        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.
                                                            ^
        
        
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 31 Dec 2015, 22:12 last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gabor53
            wrote on 1 Jan 2016, 05:07 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Devopia53
              wrote on 1 Jan 2016, 11:13 last edited by Devopia53 1 Jan 2016, 11:14
              #6

              Hi,

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

              1 Reply Last reply
              1
              • G Offline
                G Offline
                gabor53
                wrote on 2 Jan 2016, 04:25 last edited by
                #7

                Thank you. It worked.

                R 1 Reply Last reply 2 Jan 2016, 04:47
                0
                • G gabor53
                  2 Jan 2016, 04:25

                  Thank you. It worked.

                  R Offline
                  R Offline
                  Ratzz
                  wrote on 2 Jan 2016, 04:47 last edited by
                  #8

                  @gabor53
                  You could also try

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

                  --Alles ist gut.

                  1 Reply Last reply
                  1
                  • G4banditG Offline
                    G4banditG Offline
                    G4bandit
                    wrote on 8 Feb 2018, 14:02 last edited by
                    #9

                    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"

                    1 Reply Last reply
                    0
                    • G4banditG Offline
                      G4banditG Offline
                      G4bandit
                      wrote on 8 Feb 2018, 14:19 last edited by
                      #10

                      SOLUTION:

                      ui->tableView->model()->setHeaderData(2,Qt::Horizontal,QFont("Arial",10,QFont::Bold),Qt::FontRole);
                      

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

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved