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. Draw vertical text
Forum Updated to NodeBB v4.3 + New Features

Draw vertical text

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.3k Views
  • 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.
  • V Offline
    V Offline
    Vlad02
    wrote on last edited by
    #1

    Hi everybody! I need draw vertical text with QPainter::drawText. For example, text "Hello" I need draw as:
    H
    e
    l
    l
    o
    But I don't know, how do this. Maybe somebody can give some good advices. Thanks in advance!

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      This text is not drawn vertical but horizontal with a linebreak after each character. So add a linebreak after each character in your string you pass to QPainter::drawText()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      V 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        This text is not drawn vertical but horizontal with a linebreak after each character. So add a linebreak after each character in your string you pass to QPainter::drawText()

        V Offline
        V Offline
        Vlad02
        wrote on last edited by
        #3

        @Christian-Ehrlicher So if I want draw number as text, I need convert int to string and edit it with insert linebreak?

        JonBJ 1 Reply Last reply
        0
        • V Vlad02

          @Christian-Ehrlicher So if I want draw number as text, I need convert int to string and edit it with insert linebreak?

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

          @Vlad02
          Absolutely yes! You always have to convert numbers to strings to print them!

          V 1 Reply Last reply
          0
          • JonBJ JonB

            @Vlad02
            Absolutely yes! You always have to convert numbers to strings to print them!

            V Offline
            V Offline
            Vlad02
            wrote on last edited by
            #5

            @JonB Yes, I understand this. I am interesting edit this text. I need insert linebreaks in right places? And how I can do this easy?

            JonBJ 1 Reply Last reply
            0
            • V Vlad02

              @JonB Yes, I understand this. I am interesting edit this text. I need insert linebreaks in right places? And how I can do this easy?

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

              @Vlad02

              @Christian-Ehrlicher said in Draw vertical text:

              So add a linebreak after each character in your string you pass to QPainter::drawText()

              V 1 Reply Last reply
              0
              • JonBJ JonB

                @Vlad02

                @Christian-Ehrlicher said in Draw vertical text:

                So add a linebreak after each character in your string you pass to QPainter::drawText()

                V Offline
                V Offline
                Vlad02
                wrote on last edited by
                #7

                @JonB Okey, thanks for help! And @Christian-Ehrlicher thanks too

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  Vlad02
                  wrote on last edited by
                  #8

                  I'm sorry, ran into a problem. How I can insert linebreak after each character?

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • V Vlad02

                    I'm sorry, ran into a problem. How I can insert linebreak after each character?

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Vlad02 said in Draw vertical text:

                    How I can insert linebreak after each character?

                    Iterate over the string and create a new one where you add the character and a linebreak in each iteration. Basic programming stuff.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    V 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Vlad02 said in Draw vertical text:

                      How I can insert linebreak after each character?

                      Iterate over the string and create a new one where you add the character and a linebreak in each iteration. Basic programming stuff.

                      V Offline
                      V Offline
                      Vlad02
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher what if '\n' not work? I get that: "H\ne\nl\nl\no\n".

                      QString str;
                          for (QChar ch : text){
                              str.append(ch);
                              str.append('\n');
                          }
                      
                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        What drawText() function do you use? Please read the documentation about one of them:

                        This function does not handle the newline character (\n), as it cannot break text into multiple lines, and it cannot display the newline character. Use the QPainter::drawText() overload that takes a rectangle instead if you want to draw multiple lines of text with the newline character, or if you want the text to be wrapped.

                        #include <QtWidgets>
                        
                        class Widget : public QWidget
                        {
                          void paintEvent(QPaintEvent *event) override
                          {
                            QPainter p(this);
                            p.drawText(rect(), "T\ne\ns\nt");
                          }
                        };
                        
                        int main(int argc, char **argv)
                        {
                          QApplication app(argc, argv);
                          Widget w;
                          w.resize(100,100);
                          w.show();
                          return app.exec();
                        }
                        

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        V 1 Reply Last reply
                        1
                        • Christian EhrlicherC Christian Ehrlicher

                          What drawText() function do you use? Please read the documentation about one of them:

                          This function does not handle the newline character (\n), as it cannot break text into multiple lines, and it cannot display the newline character. Use the QPainter::drawText() overload that takes a rectangle instead if you want to draw multiple lines of text with the newline character, or if you want the text to be wrapped.

                          #include <QtWidgets>
                          
                          class Widget : public QWidget
                          {
                            void paintEvent(QPaintEvent *event) override
                            {
                              QPainter p(this);
                              p.drawText(rect(), "T\ne\ns\nt");
                            }
                          };
                          
                          int main(int argc, char **argv)
                          {
                            QApplication app(argc, argv);
                            Widget w;
                            w.resize(100,100);
                            w.show();
                            return app.exec();
                          }
                          
                          V Offline
                          V Offline
                          Vlad02
                          wrote on last edited by
                          #12

                          @Christian-Ehrlicher Oh, thank you very very much!!! I'm sorry for wasting time!

                          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