1

In my program I am displaying a dialog window to provide information to the user. I am using the QT Label element to do this and I would like to make the text look more "organic" or carved in, like the title text of a window than simple black on gray. Does anyone have any suggestions? I am using QT Creator and coding in c++ for Linux.

here is an image of what I am trying to acomplish

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Chronigan
  • 83
  • 5
  • 2
    You might want to include a screenshot showing an example of the effect you are looking for, since different window managers render window titles different ways. – Jeremy Friesner Jul 05 '17 at 02:33
  • Did you try the [rich-text option](http://doc.qt.io/qt-5/qlabel.html#textFormat-prop) of the QLabel? If enabled the QLabel processes [mark-up](http://doc.qt.io/qt-5/richtext-html-subset.html) (subset of HTML). As QLabel is derived from [`QFrame`](http://doc.qt.io/qt-5/qframe.html), it provides also properties to adjust a frame in various styles. Further opportunities might be available, if you override the [style](http://doc.qt.io/qt-5/qwidget.html#setStyle) (inherited from [`QWidget`](http://doc.qt.io/qt-5/qwidget.html)) but this is beyond my personal experience. – Scheff's Cat Jul 05 '17 at 05:40
  • Concerning the comment of @JeremyFriesner, I would recommend to have a look at [`QStylePainter`](http://doc.qt.io/qt-5/qstylepainter.html) (e.g. [`QStylePainter::drawComplexControl(Qt::CC_TitleBar, ...)`](http://doc.qt.io/qt-5/qstyle.html#drawComplexControl) which allows to paint own stuff using the active style engine. (This is what we use for our own Qt widget (extensions) to keep the look in sync. with the rest.) – Scheff's Cat Jul 05 '17 at 05:49
  • Concerning `QLabel` and style, I found this: [SO: QLabel: set color of text and background](https://stackoverflow.com/questions/2749798/qlabel-set-color-of-text-and-background). – Scheff's Cat Jul 05 '17 at 05:59

1 Answers1

1

I prepared an MCVE to demonstrate the options I suggested in my comments.

Changing style of QLabel is rather simple and straight forward.

Using the QStylePainter doesn't quite match what I actually expected. I left in the sample code what I got so far. (May be, somebody could leave a helpful hint.) I will investigate in this topic later and edit this answer as soon as I got some satisfying progress on this topic.

The sample code:

#include <QtWidgets>

class TitleBar: public QWidget {

  private:
    QStyleOptionTitleBar _option;
    //QString _text;

  public:
    explicit TitleBar(
      const QString &text = QString(), QWidget *pQParent = nullptr);

  protected:
    virtual QSize sizeHint() const;
    virtual void resizeEvent(QResizeEvent *pQEvent);
    virtual void paintEvent(QPaintEvent *pQEvent);
};

int main(int argc, char **argv)
{
  qDebug() << "Qt Version: " << QT_VERSION_STR;
  // main application
#undef qApp // undef macro qApp out of the way
  QApplication qApp(argc, argv);
  // setup of GUI
  QWidget qWin;
  QVBoxLayout qBox;
  // a boring label
  QLabel qLbl1(QString::fromUtf8("Rather boring"));
  qLbl1.setAlignment(Qt::AlignCenter);
  qBox.addWidget(&qLbl1);
  // a label with frame
  QLabel qLbl2(QString::fromUtf8("Label with Frame"));
  qLbl2.setFrameStyle(QLabel::Panel | QLabel::Raised);
  qLbl2.setLineWidth(2);
  qLbl2.setAlignment(Qt::AlignCenter);
  qBox.addWidget(&qLbl2);
  // a label with rich-text
  QLabel qLbl3(
    QString::fromUtf8(
      "<body bgcolor='#28f'>" // doesn't have the effect
        "<font size='+2' color='#f8e' face='Old English Text MT'>"
          "Label <i>with</i> <b>Rich-Text</b>"
        "</font>"
      "</body>")); // background is set by style-sheet instead
  qLbl3.setTextFormat(Qt::RichText); // might be auto-detected...
  qLbl3.setStyleSheet("QLabel { background-color: #28f; }");
  qLbl3.setAlignment(Qt::AlignCenter);
  qLbl3.show();
  qBox.addWidget(&qLbl3);
  // a home-brew title bar
  TitleBar qTitle("A Home-Brew Title Bar");
  qBox.addWidget(&qTitle);
  // finish setup of GUI
  qWin.setLayout(&qBox);
  qWin.show();
  // run application
  return qApp.exec();
}

TitleBar::TitleBar(const QString &text, QWidget *pQParent):
  QWidget(pQParent)
{
  _option.initFrom(this);
  _option.titleBarFlags = Qt::Window;
  _option.text = text;
}

QSize TitleBar::sizeHint() const
{
#if 0 // does not provide the expected result
  return _option.rect.size();
#elif 0 // does not provide the expected result
  return style()->proxy()->subControlRect(QStyle::CC_TitleBar,
    &_option, QStyle::SC_TitleBarLabel).size();
#else 
  return QSize(0,
    style()->proxy()->pixelMetric(QStyle::PM_TitleBarHeight,
      &_option, this));
#endif // 0
}

void TitleBar::resizeEvent(QResizeEvent *pQEvent)
{
  _option.rect = QRect(_option.rect.topLeft(), pQEvent->size());
}

void TitleBar::paintEvent(QPaintEvent *pQEvent)
{
  QPainter qPainter(this);
  style()->proxy()->drawComplexControl(QStyle::CC_TitleBar,
    &_option, &qPainter, this);
}

I compiled and tested it in VS2013 on Windows 10 (64 bit). Below a snapshot:

Snapshot of testQLabelStyled

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56