QTextTable::insertColumns() don't work.



  • Hi, why insertColumns() dont'work in this example?

    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        QWidget* w = new QWidget(this);
    
        QVBoxLayout* lay = new QVBoxLayout(w);
        QTextEdit* edit;
        edit = new QTextEdit(this);
        lay->addWidget(edit);
        changeTable = new QPushButton ( trUtf8("Change table") );
        connect( changeTable, SIGNAL( clicked(bool) ), SLOT( SlotChangeTable() ) );
        lay->addWidget(changeTable);
    
        setCentralWidget(w);
    
    
        // addTable
        QTextTableFormat tableFormat;
        tableFormat.setAlignment(Qt::AlignCenter);
        tableFormat.setBorderStyle( QTextTableFormat::BorderStyle_Solid );
        tableFormat.setCellPadding( 4 );
        tableFormat.setCellSpacing( 0 );
        tableFormat.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
    
        edit->textCursor().insertTable(3, 7, tableFormat);
    
        QTextCursor cursor(edit->textCursor());
        cursor.setPosition(1);
        edit->setTextCursor(cursor);
    
        QTextTable* table = edit->textCursor().currentTable();
        table->mergeCells( 0, 0, 2 , 1);
    
        table->mergeCells( 0, 1, 2 , 1);
        table->mergeCells( 0, 2, 2 , 1);
        table->mergeCells( 0, 3, 2 , 1);
        table->mergeCells( 0, 5, 2 , 1);
        table->mergeCells( 0, 6, 2 , 1);
        table->mergeCells( 2, 0, 1, 6);
    
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    
    void MainWindow::SlotChangeTable()
    {
        QTextTable* table = edit->textCursor().currentTable();
        table->insertColumns(1,1);
    
    }
    

    Edited to add code tags - @ambershark



  • @bogdann said in QTextTable::insertColumns() don't work.:

    void MainWindow::SlotChangeTable()
    {
    QTextTable* table = edit->textCursor().currentTable();
    table->insertColumns(1,1);
    }

    So what is the value of table->columns() before the insert call and table->columns() after?

    Also are you sure edit->textCursor().currentTable() is the table you are expecting it to be?



  • @ambershark
    Value of

    table->columns()
    

    before = 7.
    Yes i sure

    edit->textCursor().currentTable()
    

    is my table.
    I made a mistake in the example, the

    QTextEdit * edit;
    

    should be global


  • Moderators

    @bogdann Oh, this is an often seen mistake and it is really easy to overlook - I didn't notice it as I checked the code :-)
    "should be global" - you mean class member? Avoid global variables!



  • @jsulm I hope full code will answer on everything your questions :)
    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QTextEdit>
    #include <QTextTable>
    #include <QTextCursor>
    #include <QTextTableCell>
    #include <QPushButton>
    #include <QVBoxLayout>
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        QTextEdit* edit;
        QPushButton *changeTable;
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    public slots:
        void SlotChangeTable();
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp:

    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        QWidget* w = new QWidget(this);
    
        QVBoxLayout* lay = new QVBoxLayout(w);
        edit = new QTextEdit(this);
        lay->addWidget(edit);
        changeTable = new QPushButton ( trUtf8("Change table") );
        connect( changeTable, SIGNAL( clicked(bool) ), SLOT( SlotChangeTable() ) );
        lay->addWidget(changeTable);
    
        setCentralWidget(w);
    
    
        // addTable
        QTextTableFormat tableFormat;
        tableFormat.setAlignment(Qt::AlignCenter);
        tableFormat.setBorderStyle( QTextTableFormat::BorderStyle_Solid );
        tableFormat.setCellPadding( 4 );
        tableFormat.setCellSpacing( 0 );
        tableFormat.setWidth( QTextLength( QTextLength::PercentageLength, 100 ) );
    
        edit->textCursor().insertTable(3, 7, tableFormat);
    
        QTextCursor cursor(edit->textCursor());
        cursor.setPosition(1);
        edit->setTextCursor(cursor);
    
        QTextTable* table = edit->textCursor().currentTable();
        table->mergeCells( 0, 0, 2 , 1);
    
        table->mergeCells( 0, 1, 2 , 1);
        table->mergeCells( 0, 2, 2 , 1);
        table->mergeCells( 0, 3, 2 , 1);
        table->mergeCells( 0, 5, 2 , 1);
        table->mergeCells( 0, 6, 2 , 1);
        table->mergeCells( 2, 0, 1, 6);
    
    }
    
    MainWindow::~MainWindow()
    {
    
    }
    
    void MainWindow::SlotChangeTable()
    {
        QTextTable* table = edit->textCursor().currentTable();
        table->insertColumns(1,1);
    
    }
    
    

    I don't understand why can not I insert a new columns?


  • Moderators

    @bogdann said in QTextTable::insertColumns() don't work.:

    SlotChangeTable()

    Did you verify that SlotChangeTable() was called?



  • @jsulm Yes. The program stops working when it reaches the method

    table->insertColumns(1,1);
    

  • Lifetime Qt Champion

    Hi,

    You should check the return value of currentTable. You might be getting null because the cursor is somewhere else.



  • @SGaist Hi, thanks for answer, but return value != null.


  • Lifetime Qt Champion

    From a quick test:

    ASSERT: "c + colspan <= nCols" in file qt5/qtbase/src/gui/text/qtexttable.cpp, line 478

    addColumn doesn't like your long merged bottom cell.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.