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 andtable->columns()
after?Also are you sure
edit->textCursor().currentTable()
is the table you are expecting it to be? -
@ambershark
Value oftable->columns()
before = 7.
Yes i sureedit->textCursor().currentTable()
is my table.
I made a mistake in the example, theQTextEdit * edit;
should be global
-
@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?
-
@bogdann said in QTextTable::insertColumns() don't work.:
SlotChangeTable()
Did you verify that SlotChangeTable() was called?
-
Hi,
You should check the return value of
currentTable
. You might be getting null because the cursor is somewhere else. -
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.