Search code examples
c++qtqtextcursor

Creating QTextTable And Inserting Data


I am trying to create a QTextTable and insert data into it. Currently I am unable to create the table because of several errors

use of undeclared identifier 'editor'

I am also unsure how I can insert data into the TextTable. My code is below

QTextCursor cursor(editor->textCursor());
cursor.movePosition(QTextCursor::Start);
QTextTable *table = cursor.insertTable(5, 3);

I tried the code below and I have no error I am just wondering how i can insert data into the texttable so i can print it?

QTextEdit *editor = new QTextEdit();
QTextCursor cursor(editor->textCursor());
cursor.movePosition(QTextCursor::Start);
QTextTable *table = cursor.insertTable(5, 3);
table->insertRows(0, 5);

Solution

  • Add a text browser and try this one. (The form contains a QTextBrowser with object name textBrowser)

    QTextCursor cursor(ui->textBrowser->textCursor());
    cursor.movePosition(QTextCursor::Start);
    
    QTextTable *table = cursor.insertTable(2, 3);
    
    for(int i=0; i<2; i++)
    {
        for(int j=0; j<3; j++)
        {
    
            table->cellAt(i, j).firstCursorPosition().insertText("Hello");
        }
    }