Unsolved How Can I set text orientation in QTextTable vertical?
-
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. -
@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 -
@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.
-
@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.
-
@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. -
@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.
-
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_OBJECTpublic:
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);
} -
@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);
}