Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Need advices to add a table in a textarea QML/C++

Need advices to add a table in a textarea QML/C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 462 Views
  • 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.
  • J Offline
    J Offline
    JulienB
    wrote on last edited by
    #1

    Hello,
    I want to insert a table in my text editor (markdown).
    I can do it with my code but I think it is ugly and surely there is a better way to do it.
    I call the method insertTable() in setTableColumns() but it is strange.
    I am new in QML can you give me advices.
    Is it possible to get the number of rows and columns from QML with just one Q_PROPERTY?

    In the C++ part :

        Q_PROPERTY(int tableRows READ tableRows WRITE setTableRows)
        Q_PROPERTY(int tableColumns READ tableColumns WRITE setTableColumns)
    
    int Backend::tableRows()
    {
        return m_tableRow;
    }
    
    void Backend::setTableRows(int nbrOfRows)
    {
        m_tableRow = nbrOfRows;
    }
    
    int Backend::tableColumns()
    {
        return m_tableColumn;
    }
    
    void Backend::setTableColumns(int nbrOfColumns)
    {
        m_tableColumn = nbrOfColumns;
        insertTable();
    }
    
    void Backend::insertTable()
    {
        QTextCursor cursor(textCursor());
        cursor.movePosition(QTextCursor::Start);
    
        QTextTable *table = cursor.insertTable(tableRows(), tableColumns());
    }
    

    In the QML part :

                SpinBox {
                    id: tableRows
                }
    
                SpinBox {
                    id: tableColumns
                }
    
                Button {
                    id: closeButton
                    text: "Close"
                    onClicked: {
                        popup.close()
    
                        backend.tableRows = tableRows.value
                        backend.tableColumns = tableColumns.value
                    }
                }
    
        ScrollView {
            anchors.fill: parent
            TextArea {
                id: textEdit
                textFormat: TextEdit.MarkdownText
                focus: true
                selectByMouse: true
                persistentSelection: true
            }
    

    Thank you

    S ODБOïO 2 Replies Last reply
    0
    • J JulienB

      Hello,
      I want to insert a table in my text editor (markdown).
      I can do it with my code but I think it is ugly and surely there is a better way to do it.
      I call the method insertTable() in setTableColumns() but it is strange.
      I am new in QML can you give me advices.
      Is it possible to get the number of rows and columns from QML with just one Q_PROPERTY?

      In the C++ part :

          Q_PROPERTY(int tableRows READ tableRows WRITE setTableRows)
          Q_PROPERTY(int tableColumns READ tableColumns WRITE setTableColumns)
      
      int Backend::tableRows()
      {
          return m_tableRow;
      }
      
      void Backend::setTableRows(int nbrOfRows)
      {
          m_tableRow = nbrOfRows;
      }
      
      int Backend::tableColumns()
      {
          return m_tableColumn;
      }
      
      void Backend::setTableColumns(int nbrOfColumns)
      {
          m_tableColumn = nbrOfColumns;
          insertTable();
      }
      
      void Backend::insertTable()
      {
          QTextCursor cursor(textCursor());
          cursor.movePosition(QTextCursor::Start);
      
          QTextTable *table = cursor.insertTable(tableRows(), tableColumns());
      }
      

      In the QML part :

                  SpinBox {
                      id: tableRows
                  }
      
                  SpinBox {
                      id: tableColumns
                  }
      
                  Button {
                      id: closeButton
                      text: "Close"
                      onClicked: {
                          popup.close()
      
                          backend.tableRows = tableRows.value
                          backend.tableColumns = tableColumns.value
                      }
                  }
      
          ScrollView {
              anchors.fill: parent
              TextArea {
                  id: textEdit
                  textFormat: TextEdit.MarkdownText
                  focus: true
                  selectByMouse: true
                  persistentSelection: true
              }
      

      Thank you

      S Offline
      S Offline
      sonicss
      wrote on last edited by
      #2

      @JulienB U can return QVariantMap in your c++ function.

      1 Reply Last reply
      1
      • J JulienB

        Hello,
        I want to insert a table in my text editor (markdown).
        I can do it with my code but I think it is ugly and surely there is a better way to do it.
        I call the method insertTable() in setTableColumns() but it is strange.
        I am new in QML can you give me advices.
        Is it possible to get the number of rows and columns from QML with just one Q_PROPERTY?

        In the C++ part :

            Q_PROPERTY(int tableRows READ tableRows WRITE setTableRows)
            Q_PROPERTY(int tableColumns READ tableColumns WRITE setTableColumns)
        
        int Backend::tableRows()
        {
            return m_tableRow;
        }
        
        void Backend::setTableRows(int nbrOfRows)
        {
            m_tableRow = nbrOfRows;
        }
        
        int Backend::tableColumns()
        {
            return m_tableColumn;
        }
        
        void Backend::setTableColumns(int nbrOfColumns)
        {
            m_tableColumn = nbrOfColumns;
            insertTable();
        }
        
        void Backend::insertTable()
        {
            QTextCursor cursor(textCursor());
            cursor.movePosition(QTextCursor::Start);
        
            QTextTable *table = cursor.insertTable(tableRows(), tableColumns());
        }
        

        In the QML part :

                    SpinBox {
                        id: tableRows
                    }
        
                    SpinBox {
                        id: tableColumns
                    }
        
                    Button {
                        id: closeButton
                        text: "Close"
                        onClicked: {
                            popup.close()
        
                            backend.tableRows = tableRows.value
                            backend.tableColumns = tableColumns.value
                        }
                    }
        
            ScrollView {
                anchors.fill: parent
                TextArea {
                    id: textEdit
                    textFormat: TextEdit.MarkdownText
                    focus: true
                    selectByMouse: true
                    persistentSelection: true
                }
        

        Thank you

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by ODБOï
        #3

        hi
        @JulienB said in Need advices to add a table in a textarea QML/C++:

        Is it possible to get the number of rows and columns from QML with just one Q_PROPERTY?

        if you want to pass value from qml to c++ you can simply create a Q_INVOKABLE method that takes 2 arguments

        Q_INVOKABLE void setSize(int r, int c){
        ...
          QTextTable *table = cursor.insertTable(r ,c);
        }
        

        // qml

        backend.setSize(tableRows.value , tableColumns.value)
        
        J 1 Reply Last reply
        1
        • ODБOïO ODБOï

          hi
          @JulienB said in Need advices to add a table in a textarea QML/C++:

          Is it possible to get the number of rows and columns from QML with just one Q_PROPERTY?

          if you want to pass value from qml to c++ you can simply create a Q_INVOKABLE method that takes 2 arguments

          Q_INVOKABLE void setSize(int r, int c){
          ...
            QTextTable *table = cursor.insertTable(r ,c);
          }
          

          // qml

          backend.setSize(tableRows.value , tableColumns.value)
          
          J Offline
          J Offline
          JulienB
          wrote on last edited by
          #4

          @LeLev Thank you it works perfectly!!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved