Reputation: 638
I need to create a group box that supports HTML text when we set
MyGroupBox *gb = new MyGroupBox();
gb->setTitle("<u> This is underlined text</u>");
I have tried some searches but no results. In my head right now I think only about to set style for my groupbox. Something like this:
MyGroupBox.cpp
MyGroupBox::MyGroupBox( QWidget *p_parent ) : QGroupBox( p_parent )
{
setStyle( &m_style );
}
TitleStyle.hpp
class TitleStyle : public QProxyStyle
{
public:
TitleStyle() = default;
virtual void drawComplexControl( ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = nullptr ) const override;
};
TitleStyle.cpp
void TitleStyle::drawComplexControl( ComplexControl p_control, const QStyleOptionComplex *p_option, QPainter *p_painter, const QWidget *p_widget ) const
{
if ( p_control == CC_GroupBox )
{
if ( const QStyleOptionGroupBox *title = qstyleoption_cast<const QStyleOptionGroupBox *>( p_option ) )
{
QTextDocument td;
td.setHtml( title->text );
td.drawContents( p_painter );
}
}
else
{
QProxyStyle::drawComplexControl( p_control, p_option, p_painter, p_widget );
}
}
This still does not work. I know my drawComplexControl
is weird, but that is what in my mind now. Can anyone tell me if I am going in the right direction? If yes, how could I change the class TitleStyle
. If not, how could I do?
Solution: It took me a while to find my mistake. With the code above, the title should be rich text supported already.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QProxyStyle>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class TitleStyle : public QProxyStyle
{
public:
TitleStyle() = default;
virtual void drawComplexControl( ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = nullptr ) const override;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
TitleStyle m_style;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextDocument>
void TitleStyle::drawComplexControl( ComplexControl p_control, const QStyleOptionComplex *p_option, QPainter *p_painter, const QWidget *p_widget ) const
{
if ( p_control == CC_GroupBox )
{
if ( const QStyleOptionGroupBox *title = qstyleoption_cast<const QStyleOptionGroupBox *>( p_option ) )
{
QTextDocument td;
td.setHtml( title->text );
td.drawContents( p_painter );
}
}
else
{
QProxyStyle::drawComplexControl( p_control, p_option, p_painter, p_widget );
}
}
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->groupBox->setStyle(&m_style);
ui->groupBox->setTitle("<b><u>This is an underlined title</u></b>");
}
MainWindow::~MainWindow()
{
delete ui;
}
Result:
Upvotes: 0
Views: 252
Reputation: 638
Actually I have found my mistake in other positions. With this code, the title should be rich text supported already. I have tried with Qt Creator and it worked. I update my solution above.
Upvotes: 1