Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to go new line on a QLabel which is on QPushButton?

How to go new line on a QLabel which is on QPushButton?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 836 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • eefesafakE Offline
    eefesafakE Offline
    eefesafak
    wrote on last edited by
    #1

    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_H
    
    

    QMultiLinePushButton.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:
    63e1769e-2ff0-45d2-9a3b-3eae0c38a70c-image.png

    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.

    eefesafakE JonBJ 3 Replies Last reply
    0
    • eefesafakE eefesafak

      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_H
      
      

      QMultiLinePushButton.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:
      63e1769e-2ff0-45d2-9a3b-3eae0c38a70c-image.png

      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.

      eefesafakE Offline
      eefesafakE Offline
      eefesafak
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • eefesafakE eefesafak

        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_H
        
        

        QMultiLinePushButton.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:
        63e1769e-2ff0-45d2-9a3b-3eae0c38a70c-image.png

        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.

        eefesafakE Offline
        eefesafakE Offline
        eefesafak
        wrote on last edited by
        #3

        @eefesafak
        I forgot that adding the program output.
        7d309c1a-d7fa-44c9-a90d-e4587816bff4-image.png

        1 Reply Last reply
        0
        • eefesafakE eefesafak

          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_H
          
          

          QMultiLinePushButton.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:
          63e1769e-2ff0-45d2-9a3b-3eae0c38a70c-image.png

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @eefesafak
          Before you spend all this time trying to get QPushButton + QLabel right, did you consider that apparently a QToolButton does accept \n? Just a thought.

          1 Reply Last reply
          1
          • D Offline
            D Offline
            dogbear
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved