How to go new line on a QLabel which is on QPushButton?
-
Hi everyone,
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.
Because QLabel let us to go newline without '\n'.
For now, my problem is I can not get the color of font and cant go new line correctly.
Here is the code:QMultiLinePushButton.h:
#ifndef QMULTILINEPUSHBUTTON_H #define QMULTILINEPUSHBUTTON_H #include <QPushButton> class QLabel; class QMultiLinePushButton : public QPushButton { Q_OBJECT public: QMultiLinePushButton(QWidget *parent = nullptr); void setText(const QString &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_HQMultiLinePushButton.cpp
#include "QMultiLinePushButton.h" #include <QLabel> #include <QHBoxLayout> #include <QResizeEvent> #include <QDebug> QMultiLinePushButton::QMultiLinePushButton(QWidget *parent) { this->setParent(parent); m_label = new QLabel(this); m_label->setWordWrap(true); m_label->setAlignment(Qt::AlignCenter); // QHBoxLayout* layout = new QHBoxLayout(this); // layout->addWidget(m_label, 0, Qt::AlignCenter); // layout->setMargin(1); qDebug() << geometry(); } void QMultiLinePushButton::setText(const QString &text) { m_label->setText(text); } QString QMultiLinePushButton::text() const { return m_label->text(); } void QMultiLinePushButton::setTextAlignment(Qt::Alignment align) { m_label->setAlignment(align); } void QMultiLinePushButton::resizeEvent(QResizeEvent *event) { qDebug() << geometry(); m_label->setFixedSize(size()); m_label->setStyleSheet("text-align: left;"); m_label->setFont(this->font()); // why is the font black? event->accept(); }Here is the form:

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
Best regards. Have a nice day.
-
Hi everyone,
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.
Because QLabel let us to go newline without '\n'.
For now, my problem is I can not get the color of font and cant go new line correctly.
Here is the code:QMultiLinePushButton.h:
#ifndef QMULTILINEPUSHBUTTON_H #define QMULTILINEPUSHBUTTON_H #include <QPushButton> class QLabel; class QMultiLinePushButton : public QPushButton { Q_OBJECT public: QMultiLinePushButton(QWidget *parent = nullptr); void setText(const QString &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_HQMultiLinePushButton.cpp
#include "QMultiLinePushButton.h" #include <QLabel> #include <QHBoxLayout> #include <QResizeEvent> #include <QDebug> QMultiLinePushButton::QMultiLinePushButton(QWidget *parent) { this->setParent(parent); m_label = new QLabel(this); m_label->setWordWrap(true); m_label->setAlignment(Qt::AlignCenter); // QHBoxLayout* layout = new QHBoxLayout(this); // layout->addWidget(m_label, 0, Qt::AlignCenter); // layout->setMargin(1); qDebug() << geometry(); } void QMultiLinePushButton::setText(const QString &text) { m_label->setText(text); } QString QMultiLinePushButton::text() const { return m_label->text(); } void QMultiLinePushButton::setTextAlignment(Qt::Alignment align) { m_label->setAlignment(align); } void QMultiLinePushButton::resizeEvent(QResizeEvent *event) { qDebug() << geometry(); m_label->setFixedSize(size()); m_label->setStyleSheet("text-align: left;"); m_label->setFont(this->font()); // why is the font black? event->accept(); }Here is the form:

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
Best regards. Have a nice day.
-
Hi everyone,
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.
Because QLabel let us to go newline without '\n'.
For now, my problem is I can not get the color of font and cant go new line correctly.
Here is the code:QMultiLinePushButton.h:
#ifndef QMULTILINEPUSHBUTTON_H #define QMULTILINEPUSHBUTTON_H #include <QPushButton> class QLabel; class QMultiLinePushButton : public QPushButton { Q_OBJECT public: QMultiLinePushButton(QWidget *parent = nullptr); void setText(const QString &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_HQMultiLinePushButton.cpp
#include "QMultiLinePushButton.h" #include <QLabel> #include <QHBoxLayout> #include <QResizeEvent> #include <QDebug> QMultiLinePushButton::QMultiLinePushButton(QWidget *parent) { this->setParent(parent); m_label = new QLabel(this); m_label->setWordWrap(true); m_label->setAlignment(Qt::AlignCenter); // QHBoxLayout* layout = new QHBoxLayout(this); // layout->addWidget(m_label, 0, Qt::AlignCenter); // layout->setMargin(1); qDebug() << geometry(); } void QMultiLinePushButton::setText(const QString &text) { m_label->setText(text); } QString QMultiLinePushButton::text() const { return m_label->text(); } void QMultiLinePushButton::setTextAlignment(Qt::Alignment align) { m_label->setAlignment(align); } void QMultiLinePushButton::resizeEvent(QResizeEvent *event) { qDebug() << geometry(); m_label->setFixedSize(size()); m_label->setStyleSheet("text-align: left;"); m_label->setFont(this->font()); // why is the font black? event->accept(); }Here is the form:

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
Best regards. Have a nice day.
@eefesafak
I forgot that adding the program output.

-
Hi everyone,
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.
Because QLabel let us to go newline without '\n'.
For now, my problem is I can not get the color of font and cant go new line correctly.
Here is the code:QMultiLinePushButton.h:
#ifndef QMULTILINEPUSHBUTTON_H #define QMULTILINEPUSHBUTTON_H #include <QPushButton> class QLabel; class QMultiLinePushButton : public QPushButton { Q_OBJECT public: QMultiLinePushButton(QWidget *parent = nullptr); void setText(const QString &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_HQMultiLinePushButton.cpp
#include "QMultiLinePushButton.h" #include <QLabel> #include <QHBoxLayout> #include <QResizeEvent> #include <QDebug> QMultiLinePushButton::QMultiLinePushButton(QWidget *parent) { this->setParent(parent); m_label = new QLabel(this); m_label->setWordWrap(true); m_label->setAlignment(Qt::AlignCenter); // QHBoxLayout* layout = new QHBoxLayout(this); // layout->addWidget(m_label, 0, Qt::AlignCenter); // layout->setMargin(1); qDebug() << geometry(); } void QMultiLinePushButton::setText(const QString &text) { m_label->setText(text); } QString QMultiLinePushButton::text() const { return m_label->text(); } void QMultiLinePushButton::setTextAlignment(Qt::Alignment align) { m_label->setAlignment(align); } void QMultiLinePushButton::resizeEvent(QResizeEvent *event) { qDebug() << geometry(); m_label->setFixedSize(size()); m_label->setStyleSheet("text-align: left;"); m_label->setFont(this->font()); // why is the font black? event->accept(); }Here is the form:

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
Best regards. Have a nice day.
@eefesafak
Before you spend all this time trying to getQPushButton+QLabelright, did you consider that apparently aQToolButtondoes accept\n? Just a thought. -
@eefesafak
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:QMultiLinePushButton button; button.setTextUpper("I love cheese"); button.setTextLower("(the smelly kind)");You would then be able to set different fonts, sizes, and colours to the texts.
Just a thought.