Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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



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



  • 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)
    


  • @LeLev Thank you it works perfectly!!


Log in to reply