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. QTextTable::insertColumns() don't work.

QTextTable::insertColumns() don't work.

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.5k Views 2 Watching
  • 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
    bogdann
    wrote on last edited by ambershark
    #1

    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

    A 1 Reply Last reply
    0
    • B bogdann

      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

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @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?

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      B 1 Reply Last reply
      0
      • A 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?

        B Offline
        B Offline
        bogdann
        wrote on last edited by
        #3

        @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

        jsulmJ 1 Reply Last reply
        0
        • B bogdann

          @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

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

          @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!

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

          B 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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!

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

            @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?

            jsulmJ 1 Reply Last reply
            0
            • B bogdann

              @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?

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

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

              SlotChangeTable()

              Did you verify that SlotChangeTable() was called?

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

              B 1 Reply Last reply
              0
              • jsulmJ jsulm

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

                SlotChangeTable()

                Did you verify that SlotChangeTable() was called?

                B Offline
                B Offline
                bogdann
                wrote on last edited by
                #7

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

                table->insertColumns(1,1);
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  B 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Hi,

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

                    B Offline
                    B Offline
                    bogdann
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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