<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to go new line on a QLabel which is on QPushButton?]]></title><description><![CDATA[<p dir="auto">Hi everyone,<br />
My goal is to go new line on a QPushButton. Unfortunately, QPushButton does not let us go new line without '\n'. I do not want to use \n. So, I decided add QLabel to a QPushButton.<br />
Because QLabel let us to go newline without '\n'.<br />
For now, my problem is I can not get the color of font and cant go new line correctly.<br />
Here is the code:</p>
<p dir="auto"><strong>QMultiLinePushButton.h:</strong></p>
<pre><code>#ifndef QMULTILINEPUSHBUTTON_H
#define QMULTILINEPUSHBUTTON_H

#include &lt;QPushButton&gt;
class QLabel;

class QMultiLinePushButton : public QPushButton
{
    Q_OBJECT
public:
    QMultiLinePushButton(QWidget *parent = nullptr);

    void setText(const QString &amp;text);
    QString text() const;

    void setTextAlignment(Qt::Alignment align = Qt::AlignCenter);

protected:
    virtual void resizeEvent(QResizeEvent *event); // qpushbutton protected methods

private:
    QLabel* m_label;

};

#endif // QMULTILINEPUSHBUTTON_H

</code></pre>
<p dir="auto"><strong>QMultiLinePushButton.cpp</strong></p>
<pre><code>#include "QMultiLinePushButton.h"
#include &lt;QLabel&gt;
#include &lt;QHBoxLayout&gt;
#include &lt;QResizeEvent&gt;
#include &lt;QDebug&gt;

QMultiLinePushButton::QMultiLinePushButton(QWidget *parent)
{
    this-&gt;setParent(parent);
    m_label = new QLabel(this);
    m_label-&gt;setWordWrap(true);
    m_label-&gt;setAlignment(Qt::AlignCenter);

//    QHBoxLayout* layout = new QHBoxLayout(this);
//    layout-&gt;addWidget(m_label, 0, Qt::AlignCenter);

//    layout-&gt;setMargin(1);
    qDebug() &lt;&lt; geometry();
}

void QMultiLinePushButton::setText(const QString &amp;text)
{
    m_label-&gt;setText(text);
}

QString QMultiLinePushButton::text() const
{
    return m_label-&gt;text();
}

void QMultiLinePushButton::setTextAlignment(Qt::Alignment align)
{
    m_label-&gt;setAlignment(align);
}

void QMultiLinePushButton::resizeEvent(QResizeEvent *event)
{
    qDebug() &lt;&lt; geometry();
    m_label-&gt;setFixedSize(size());
    m_label-&gt;setStyleSheet("text-align: left;");
    m_label-&gt;setFont(this-&gt;font()); // why is the font black?
    event-&gt;accept();
}

</code></pre>
<p dir="auto">Here is the form:<br />
<img src="https://ddgobkiprc33d.cloudfront.net/0a46579f-7283-4bdb-b696-ac1080f465a2.png" alt="63e1769e-2ff0-45d2-9a3b-3eae0c38a70c-image.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">Each text has the text which is "Deneme bir iki üç dört beş altı yedi sekiz dokuz on". Meaning: "Trying one two ... nine ten" in English</p>
<p dir="auto">Best regards. Have a nice day.</p>
]]></description><link>https://forum.qt.io/topic/140298/how-to-go-new-line-on-a-qlabel-which-is-on-qpushbutton</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 01:05:42 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/140298.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 25 Oct 2022 08:10:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to go new line on a QLabel which is on QPushButton? on Tue, 25 Oct 2022 11:19:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/eefesafak">@<bdi>eefesafak</bdi></a><br />
I am not sure why a '\n' is undesirable but you could try adding two QLabels in a single QPushButton so that you could do something like:</p>
<pre><code>QMultiLinePushButton button;
button.setTextUpper("I love cheese");
button.setTextLower("(the smelly kind)");
</code></pre>
<p dir="auto">You would then be able to set different fonts, sizes, and colours to the texts.</p>
<p dir="auto">Just a thought.</p>
]]></description><link>https://forum.qt.io/post/733989</link><guid isPermaLink="true">https://forum.qt.io/post/733989</guid><dc:creator><![CDATA[dogbear]]></dc:creator><pubDate>Tue, 25 Oct 2022 11:19:01 GMT</pubDate></item><item><title><![CDATA[Reply to How to go new line on a QLabel which is on QPushButton? on Tue, 25 Oct 2022 08:34:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/eefesafak">@<bdi>eefesafak</bdi></a><br />
Before you spend all this time trying to get <code>QPushButton</code> + <code>QLabel</code> right, did you <em>consider</em> that apparently a <code>QToolButton</code> does accept <code>\n</code>?  Just a thought.</p>
]]></description><link>https://forum.qt.io/post/733962</link><guid isPermaLink="true">https://forum.qt.io/post/733962</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 25 Oct 2022 08:34:48 GMT</pubDate></item><item><title><![CDATA[Reply to How to go new line on a QLabel which is on QPushButton? on Tue, 25 Oct 2022 08:16:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/eefesafak">@<bdi>eefesafak</bdi></a><br />
I forgot that adding the program output.<br />
<img src="https://ddgobkiprc33d.cloudfront.net/0c9b49d5-86de-485a-a6ee-f53383d5d1a8.png" alt="7d309c1a-d7fa-44c9-a90d-e4587816bff4-image.png" class=" img-fluid img-markdown" /></p>
]]></description><link>https://forum.qt.io/post/733958</link><guid isPermaLink="true">https://forum.qt.io/post/733958</guid><dc:creator><![CDATA[eefesafak]]></dc:creator><pubDate>Tue, 25 Oct 2022 08:16:16 GMT</pubDate></item></channel></rss>