How to change font size of title in QTableView - OSX specific
I am trying to change the font size used in row and column headers for a QTableView filled with QAbstractTableModel in Qt5.4.0 on Mac OSX 10.9.5.
UPDATE . This looks like an OSX issue, as I can run all three experiments below on CentOS 6.2 Linux with Qt5.4.0 and they all work fine, generating the expected change font size.
For the sake of brevity, I'll include all three experiments I've done [some of them were inspired by StackOverflow and other blogs ...] tagged and tagged in my example below:
#include <QApplication>
#include <QMainWindow>
#include <QtGui>
#include <QAbstractTableModel>
#include <QTableView>
#include <QHeaderView>
class TestModel : public QAbstractTableModel
{
public:
TestModel() {};
~TestModel() {};
int rowCount( const QModelIndex& parent = QModelIndex() ) const;
int columnCount( const QModelIndex& parent = QModelIndex() ) const;
QVariant data( const QModelIndex& index, int role ) const;
QVariant headerData( int section, Qt::Orientation orientation, int role ) const;
};
int
TestModel::rowCount( const QModelIndex& index ) const {
return 5;
}
int
TestModel::columnCount( const QModelIndex& index ) const {
return 5;
}
QVariant
TestModel::data( const QModelIndex& index, int role = Qt::DisplayRole ) const {
switch( role ) {
case( Qt::DisplayRole ):
return( QString( "%1,%2" ).arg( index.row() ).arg( index.column() ) );
default:
return QVariant();
}
}
QVariant
TestModel::headerData( int section, Qt::Orientation orientation, int role ) const {
QFont font;
switch( role ) {
case( Qt::DisplayRole ):
if( orientation == Qt::Horizontal ) {
return( QString( "Column %1" ).arg( section ) );
} else {
return( QString( "Row %1" ).arg( section ) );
}
// EXPERIMENT #3:
// case( Qt::FontRole ):
// font.setPointSize(48);
// return font;
default:
return QVariant();
}
}
int
main( int argc, char *argv[] ) {
QApplication app( argc, argv );
TestModel* model = new TestModel();
QMainWindow* mw = new QMainWindow();
QTableView* tableView = new QTableView();
tableView->setModel( model );
// EXPERIMENT #1:
// QFont font;
// font = tableView->horizontalHeader()->font();
// font.setPointSize(48);
// tableView->horizontalHeader()->setFont( font );
// EXPERIMENT #2:
// tableView->horizontalHeader()->setStyleSheet("QHeaderView { font-size: 48pt; }");
mw->setCentralWidget( tableView );
mw->show();
return app.exec();
}
When I run this without any attempt at changing the font, either with EXPERIMENT # 1 or EXPERIMENT # 2, I get this:
Note that in the results of EXPERIMENT # 1 and # 2, the font size for the column and row headings did not change, contrary to my expectations. When I run the code with EXPERIMENT # 3 enabled, I still don't change the font size for the column and row headings, although I get the weird effect that the column heading fields appear sized for a larger font:
My preference would be to get EXPERIMENT # 1 to work, which better harmonizes with the real code.
How can I successfully change the font size of the QTableView row and column headers?
source to share
No one has answered this question yet
Check out similar questions: