4

i'm trying to make a qt widget that shows a table of qlabels displaying hex numbers.

i pass the numbers to the labels as qstrings ready to be printed and the labels work properly but the font type is the system default (a sans serif) that has different letter sizes, so the numbers containing "A-F" digits are no more aligned with the other numbers...

i initially create the font with the function:

static const QFont getMonospaceFont(){
  QFont monospaceFont("monospace");  // tried both with and without capitalized initial M
  monospaceFont.setStyleHint(QFont::TypeWriter);
  return monospaceFont;
}

and create a custom QLabel class that has this constructor:

monoLabel(QWidget *parent = 0, Qt::WindowFlags f = 0) : QLabel(parent, f) {
  setTextFormat(Qt::RichText);
  setFont(getMonospaceFont());
}

but it doesn't work, so i add to the main file

QApplication app(argn, argv);
app.setFont(monoLabel::getMonospaceFont(), "monoLabel");

and again the font remains unchanged..

i searched the net for font-setting problems with QLabels but i seem to be the only one who doesn't get them to work properly..

what am i doing wrong??

Matteo
  • 1,107
  • 2
  • 27
  • 47
mellotanica
  • 174
  • 5
  • 12

1 Answers1

5

You probably want a Monospace style hint, not Typewriter. The following works for me on OS X under Qt 4 and 5.

Setting QLabel to rich text is unnecessary for your application.

Note that QFontInfo::fixedPitch() is not the same as QFont::fixedPitch(). The latter lets you know whether you requested a fixed pitch font. The former indicated whether you actually got a fixed pitch font.

screenshot

// https://github.com/KubaO/stackoverflown/tree/master/questions/label-font-18896933
// This project is compatible with Qt 4 and Qt 5
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtWidgets>
#endif

bool isFixedPitch(const QFont &font) {
   const QFontInfo fi(font);
   qDebug() << fi.family() << fi.fixedPitch();
   return fi.fixedPitch();
}

QFont getMonospaceFont() {
   QFont font("monospace");
   if (isFixedPitch(font)) return font;
   font.setStyleHint(QFont::Monospace);
   if (isFixedPitch(font)) return font;
   font.setStyleHint(QFont::TypeWriter);
   if (isFixedPitch(font)) return font;
   font.setFamily("courier");
   if (isFixedPitch(font)) return font;
   return font;
}

int main(int argc, char *argv[]) {
   QApplication a(argc, argv);
   QString text("0123456789ABCDEF");
   QWidget w;
   QVBoxLayout layout(&w);
   QLabel label1(text), label2(text);
   label1.setFont(getMonospaceFont());
   layout.addWidget(&label1);
   layout.addWidget(&label2);
   w.show();
   return a.exec();
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • i'm using qt 4.8.2 with qmake 2.01a (through qt creator 2.5.0) on a debian wheezy machine (32 bit, updated around monday) – mellotanica Sep 20 '13 at 15:06
  • OK, I've updated it a bit so that it tries various options and outputs debug information. If it still fails, please post the debug output in the comments. – Kuba hasn't forgotten Monica Sep 20 '13 at 16:43
  • ok, this time it works properly (it prints out `"DejaVu Sans Mono" true`) and moving things around in my project in a pseudo-random way made the change happen... it seems that adding `app.setFont(monoLabel::getMonospaceFont(), "parentWidget");` right after the creation of the QApplication, where `parentWidget` is the class that contains the labels, was the only way to have monospace text in the labels.. it worsk and i'm fine with it but i did not understand why.. – mellotanica Sep 22 '13 at 16:42
  • You really don't need to change the application-wide font. It should work on a per-widget basis. – Kuba hasn't forgotten Monica Sep 22 '13 at 23:35
  • i also thought so, but if i set the font for the labels only it doesn't have any effect, the only way (in my program) seems to be to set the font for the labels' parent widget class before it is istantiated – mellotanica Sep 24 '13 at 00:13
  • Have you run my example? Does it work? If so, try adding your code to it and see where it'll break. – Kuba hasn't forgotten Monica Sep 24 '13 at 02:04