Important: Please read the Qt Code of Conduct -

QToolButton english encode error

  • I have a problem of english encode error.
    My pice of code as below:
    class Button:public QToolButton {
    Button(const QSize &size, const QString &text, QWidget *parent=NULL):QToolButton(parent) {

        l = new QHBoxLayout(this);
        label = new QLabel(QString("<br><br><br><center>%0</center>").arg(text), this);
        qDebug()<<label->text ();
        label->setFixedSize(this->width(), this->height ());
        l->addWidget (label, 0, Qt::AlignBottom | Qt::AlignCenter);
        this->setLayout (l);
    ~Button() {
        delete label;
        delete l;

    QLabel *label;
    QHBoxLayout *l;
    When i create an instance as:
    new Button(QSize(134, 134), "用iPhone<br>发送铃声至电脑", this);

    The "iPhone" char will be displayed as "jQipof", other chars (chinese) are all right.
    I have a test on QWidget, this string displays ok.
    Is there a limitation of setting a label on QToolButton/QPushButton? or it's just a bug of them?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    QAbstractButton already handles text so one of the question is why are you currently inheriting a button like that ?

    Why not use setText with your text content ?

  • I want to specify the point of the text on the button. By default the text is located at the center of the QToolButton. Then is there a way to specify the alignment of the text of the button?

  • Lifetime Qt Champion

    AFAIK, you can only decide whether the text is under or beside the icon. But not it's alignment

  • It's just what i found. And finally solved this bug by change Chinese and English combination.
    Because QToolButton have some states(hover, pressed ...), I used qss to change the backgroud, and some text is need to put on the button which is not beside or under the icon, just located at the bottom of the button. so i inherited the QToolButton.

  • Moderators

    Glad you were able to get it solved! If you don't mind, please edit your first post to add [Solved] to the beginning of the title. Thanks!

Log in to reply