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 Can I set text orientation in QTextTable vertical?
QtWS25 Last Chance

How Can I set text orientation in QTextTable vertical?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 6.0k 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.
  • B Offline
    B Offline
    best_iyke
    wrote on last edited by
    #1

    I insert some text in QTextTable in QTextEdit using QTextCursor.
    But I want the text in the table header to be set vertically oriented and I don't know how to achieve this. I have make some search, and I could not get any guide on it. Please someone should help me with some code or guide.
    Thanks in advance for the assistance.

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • B best_iyke

      I insert some text in QTextTable in QTextEdit using QTextCursor.
      But I want the text in the table header to be set vertically oriented and I don't know how to achieve this. I have make some search, and I could not get any guide on it. Please someone should help me with some code or guide.
      Thanks in advance for the assistance.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @best_iyke I guess Qt::AlignVCenter is your friend:
      http://doc.qt.io/qt-5/qtexttableformat.html#setAlignment
      http://doc.qt.io/qt-5/qtexttable.html#setFormat

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • B best_iyke

        I insert some text in QTextTable in QTextEdit using QTextCursor.
        But I want the text in the table header to be set vertically oriented and I don't know how to achieve this. I have make some search, and I could not get any guide on it. Please someone should help me with some code or guide.
        Thanks in advance for the assistance.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @best_iyke from the title of this thread I assume, you want to rotate your text by 90° to display vertically ?

        than the item property rotation should be what you're looking for.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        B 1 Reply Last reply
        0
        • jsulmJ jsulm

          @best_iyke I guess Qt::AlignVCenter is your friend:
          http://doc.qt.io/qt-5/qtexttableformat.html#setAlignment
          http://doc.qt.io/qt-5/qtexttable.html#setFormat

          B Offline
          B Offline
          best_iyke
          wrote on last edited by
          #4

          @jsulm void GwWidget::createTable()
          {
          QTextEdit *textEdit = new QTextEdit;
          setCentralWidget(textEdit);

          QTextCursor textCursor = textEdit->textCursor();
          
          QTextTableFormat tf;
          tf.setAlignment(Qt::AlignVCenter);
          
          QTextTable *table = textCursor.insertTable(5,5);
          table->setFormat(tf);
          
          QTextTableCell cell1 = table->cellAt(0,0);
          textCursor = cell1.firstCursorPosition();
          textCursor.insertText("Column1");
          
          QTextTableCell cell2 = table->cellAt(0,1);
          textCursor = cell2.firstCursorPosition();
          textCursor.insertText("Column1");
          
          QTextTableCell cell3 = table->cellAt(0,2);
          textCursor = cell3.firstCursorPosition();
          textCursor.insertText("Column3");
          
          QTextTableCell cell4 = table->cellAt(0,3);
          textCursor = cell4.firstCursorPosition();
          textCursor.insertText("Column4");
          
          QTextTableCell cell5 = table->cellAt(0,4);
          textCursor = cell5.firstCursorPosition();
          textCursor.insertText("Column5");
          

          }

          Qt::AlignVCenter does not change the orientation of the text (i.e. rotating the text by 90 degree) in the table cells.

          1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @best_iyke from the title of this thread I assume, you want to rotate your text by 90° to display vertically ?

            than the item property rotation should be what you're looking for.

            B Offline
            B Offline
            best_iyke
            wrote on last edited by
            #5

            @J.Hilk This is exactly what i want to achieve, but I don't know how to implement it. The link is QML doc. I am using c++. Please I need help. Thanks.

            From my search findings is like I have to create custom QTextObject and rotate the text by 90 degree. But I don't know how to go about it.
            The svgTextObject example in Qt examples did not give me a good guide on how to do it.

            J.HilkJ 1 Reply Last reply
            0
            • B best_iyke

              @J.Hilk This is exactly what i want to achieve, but I don't know how to implement it. The link is QML doc. I am using c++. Please I need help. Thanks.

              From my search findings is like I have to create custom QTextObject and rotate the text by 90 degree. But I don't know how to go about it.
              The svgTextObject example in Qt examples did not give me a good guide on how to do it.

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @best_iyke
              uh, sry I mentioned QML; because its super easy to do there, I don't know of an easy way to implent this in QWidgets.

              The only way I know of would be to create a custom widget reimplementing the paintevent and than:

              void CustomWidget::paintEvent(QPaintEvent*)
              {
                  QPainter painter(this);
                  painter.translate(0,sizeHint().height());
                  painter.rotate(90);
              
                  painter.drawText(QRect (QPoint(0,0),sizeHint),Qt::AlignCenter,text());
              }
              

              example taken from here, interesting stackoverflow thread, you should read through it.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • B Offline
                B Offline
                best_iyke
                wrote on last edited by
                #7

                I have been able to create a new text object but it is the rotated text is truncated. Here is the code:

                #ifndef WIDGET_H
                #define WIDGET_H

                #include <QMainWindow>
                #include <QTextCursor>

                class QTextEdit;

                class GwWidget : public QMainWindow
                {
                Q_OBJECT

                public:
                GwWidget(QWidget *parent = 0);
                ~GwWidget();
                enum { VTextCharFormat = QTextFormat::UserFormat + 1 };
                enum TxetCharProperties{ VTextCharPropertyId = 1 };

                void createTable();
                

                private slots:
                void insertTextObject(const QString &string, QTextTable *table, int cellRow, int cellCol);

                private:
                void setupTextObject();
                QTextEdit *textEdit;
                QTextCursor textCursor;
                };

                #endif // WIDGET_H

                #include "GwWidget.h"
                #include <QTextEdit>
                #include <QTextTable>
                #include "GwTextObject.h"

                GwWidget::GwWidget(QWidget *parent)
                : QMainWindow(parent)
                {
                textEdit = new QTextEdit;
                setCentralWidget(textEdit);
                textCursor = textEdit->textCursor();

                setupTextObject();
                createTable();
                

                }

                GwWidget::~GwWidget()
                {

                }

                void GwWidget::createTable()
                {
                QTextTableFormat tf;
                tf.setAlignment(Qt::AlignVCenter);
                tf.setCellSpacing(0);

                QTextTable *table = textCursor.insertTable(5,5);
                table->setFormat(tf);
                
                
                
                insertTextObject("Column0", table, 0, 0);
                
                QTextTableCell cell1 = table->cellAt(0,1);
                textCursor = cell1.firstCursorPosition();
                textCursor.insertText("Column1");
                
                QTextTableCell cell2 = table->cellAt(0,2);
                textCursor = cell2.firstCursorPosition();
                textCursor.insertText("Column2");
                
                QTextTableCell cell3 = table->cellAt(0,3);
                textCursor = cell3.firstCursorPosition();
                textCursor.insertText("Column3");
                
                QTextTableCell cell4 = table->cellAt(0,4);
                textCursor = cell4.firstCursorPosition();
                textCursor.insertText("Column4");
                

                }

                void GwWidget::insertTextObject(const QString &string, QTextTable *table, int cellRow, int cellCol)
                {
                QTextCharFormat charFormat;

                charFormat.setObjectType(VTextCharFormat);
                charFormat.setProperty(VTextCharPropertyId, QVariant(string));
                
                QTextTableCell cell5 = table->cellAt(cellRow, cellCol);
                textCursor = cell5.firstCursorPosition();
                textCursor.insertText(QString(QChar::ObjectReplacementCharacter), charFormat);
                

                }

                void GwWidget::setupTextObject()
                {
                QObject *textObject = new GwTextObject;
                textObject->setParent(this);
                textEdit->document()->documentLayout()->registerHandler(VTextCharFormat, textObject);
                }

                B 1 Reply Last reply
                0
                • B best_iyke

                  I have been able to create a new text object but it is the rotated text is truncated. Here is the code:

                  #ifndef WIDGET_H
                  #define WIDGET_H

                  #include <QMainWindow>
                  #include <QTextCursor>

                  class QTextEdit;

                  class GwWidget : public QMainWindow
                  {
                  Q_OBJECT

                  public:
                  GwWidget(QWidget *parent = 0);
                  ~GwWidget();
                  enum { VTextCharFormat = QTextFormat::UserFormat + 1 };
                  enum TxetCharProperties{ VTextCharPropertyId = 1 };

                  void createTable();
                  

                  private slots:
                  void insertTextObject(const QString &string, QTextTable *table, int cellRow, int cellCol);

                  private:
                  void setupTextObject();
                  QTextEdit *textEdit;
                  QTextCursor textCursor;
                  };

                  #endif // WIDGET_H

                  #include "GwWidget.h"
                  #include <QTextEdit>
                  #include <QTextTable>
                  #include "GwTextObject.h"

                  GwWidget::GwWidget(QWidget *parent)
                  : QMainWindow(parent)
                  {
                  textEdit = new QTextEdit;
                  setCentralWidget(textEdit);
                  textCursor = textEdit->textCursor();

                  setupTextObject();
                  createTable();
                  

                  }

                  GwWidget::~GwWidget()
                  {

                  }

                  void GwWidget::createTable()
                  {
                  QTextTableFormat tf;
                  tf.setAlignment(Qt::AlignVCenter);
                  tf.setCellSpacing(0);

                  QTextTable *table = textCursor.insertTable(5,5);
                  table->setFormat(tf);
                  
                  
                  
                  insertTextObject("Column0", table, 0, 0);
                  
                  QTextTableCell cell1 = table->cellAt(0,1);
                  textCursor = cell1.firstCursorPosition();
                  textCursor.insertText("Column1");
                  
                  QTextTableCell cell2 = table->cellAt(0,2);
                  textCursor = cell2.firstCursorPosition();
                  textCursor.insertText("Column2");
                  
                  QTextTableCell cell3 = table->cellAt(0,3);
                  textCursor = cell3.firstCursorPosition();
                  textCursor.insertText("Column3");
                  
                  QTextTableCell cell4 = table->cellAt(0,4);
                  textCursor = cell4.firstCursorPosition();
                  textCursor.insertText("Column4");
                  

                  }

                  void GwWidget::insertTextObject(const QString &string, QTextTable *table, int cellRow, int cellCol)
                  {
                  QTextCharFormat charFormat;

                  charFormat.setObjectType(VTextCharFormat);
                  charFormat.setProperty(VTextCharPropertyId, QVariant(string));
                  
                  QTextTableCell cell5 = table->cellAt(cellRow, cellCol);
                  textCursor = cell5.firstCursorPosition();
                  textCursor.insertText(QString(QChar::ObjectReplacementCharacter), charFormat);
                  

                  }

                  void GwWidget::setupTextObject()
                  {
                  QObject *textObject = new GwTextObject;
                  textObject->setParent(this);
                  textEdit->document()->documentLayout()->registerHandler(VTextCharFormat, textObject);
                  }

                  B Offline
                  B Offline
                  best_iyke
                  wrote on last edited by
                  #8

                  @best_iyke
                  class GwTextObject : public QObject, public QTextObjectInterface
                  {
                  Q_OBJECT
                  Q_INTERFACES(QTextObjectInterface)
                  public:
                  QSizeF intrinsicSize(QTextDocument *doc, int posInDocument,
                  const QTextFormat &format) override;

                  void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc,
                                  int posInDocument, const QTextFormat &format) override;
                  

                  };

                  QSizeF GwTextObject::intrinsicSize(QTextDocument */doc/, int /posInDocument/, const QTextFormat &format)
                  {
                  QString str = format.property(GwWidget::VTextCharPropertyId).toString();
                  int strSize = str.size();
                  return QSizeF(strSize + 10, strSize + 80);
                  }

                  void GwTextObject::drawObject(QPainter *painter, const QRectF &rect,
                  QTextDocument */doc/, int /posInDocument/, const QTextFormat &format)
                  {
                  QString text = format.property(GwWidget::VTextCharPropertyId).toString();

                  QFont font = painter->font();
                  font.setPointSize(12);
                  font.setFamily("Algerian");
                  font.setWeight(QFont::DemiBold);
                  painter->setFont(font);
                  painter->translate(rect.width() -15, rect.height());
                  painter->rotate(-90);
                  painter->drawText(rect, text);
                  

                  }

                  1 Reply Last reply
                  2

                  • Login

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