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

Print multiple tabs to single pdf



  • Dear qt quick community!

    I am still learning qml and I am currently developing a tool for myself. Following you find a screenshot of what the tool roughly looks like at the moment:

    !http://oi60.tinypic.com/o05gko.jpg(screenshot tool)!

    So basically, I have an application window with some tool buttons and multiple tabs. What I what to do now, is to implement the 'saveAsPdf'-button, which gathers all the information on the different tabs and creates a pdf.
    My problem is that I am not able to access the data (e.g. comboboxes, textfields, listview etc.) from the ToolButton in the ApplicationWindow. Following you find my code so far:

    main.qml:
    @
    import QtQuick 2.2
    import QtQuick.Controls 1.1
    import QtQuick.Controls.Styles 1.1
    import QtQuick.Layouts 1.1

    import "MyConst.js" as Const

    ApplicationWindow {

    id: id_applicationWindow
    
    width: 800
    height: 800
    
    property int margin: 11
    
    Component{
        id: id_comp
        MyTab{
            id: id_myTab
        }
    }
    
    menuBar: MenuBar {
        Menu {
            title: "File"
            MenuItem { text: "Open..." }
            MenuItem { text: "Close" }
        }
        Menu {
            title: "Edit"
            MenuItem { text: "Cut" }
            MenuItem { text: "Copy" }
            MenuItem { text: "Paste" }
        }
    }
    
    toolBar: ToolBar {
    
        width: id_applicationWindow.width
        height: 50
    
        ToolButton {
            id: id_addTab
            text: "add tab"
            anchors.verticalCenter: parent.verticalCenter
            onClicked: {
                id_tabView.myAddTab("tab", id_comp)
            }
        }
    
        ToolButton {
            id: id_removeTab
            text: "remove tab"
            anchors.left: id_addTab.right
            anchors.leftMargin: Const.mySpacing
            anchors.verticalCenter: parent.verticalCenter
            onClicked: {
                id_tabView.myRemoveCurrentTab();
            }
        }
    
        ToolButton {
            id: id_save
            text: "saveAsPdf"
            anchors.left: id_removeTab.right
            anchors.leftMargin: Const.mySpacing
            anchors.verticalCenter: parent.verticalCenter
            onClicked: {
                console.log("number of open tabs: " + id_tabView.count)
                console.log("current tab: " + id_tabView.currentIndex)
                var nbTabs = id_tabView.count
                for (var i=0; i<nbTabs; i++) {
                    console.log("airport tab " + i + ": " + id_tabView.getTab(i).children)
                    console.log("nb children: " + id_tabView.getTab(i).children.length)
                    var myChildrenList = id_tabView.getTab(i).children
                }
            }
        }
    }
    
    TabView{
        id: id_tabView
        anchors.fill: parent
    
        Tab{
            id: id_tab0
            title: "myTab"
            MyTab{
                id: id_myTab
            }
        }
    
        function myAddTab(title, componentID){
            if (id_tabView.count < 10)
                id_tabView.addTab(title, componentID)
        }
    
        function myRemoveCurrentTab(){
            if (id_tabView.count > 1)
                id_tabView.removeTab(id_tabView.currentIndex)
        }
    
    }
    

    }
    @

    a snippet of MyTab.qml:
    @

    import QtQuick 2.2
    import QtQuick.Controls 1.1
    import QtQuick.Layouts 1.1

    import QtQuick.XmlListModel 2.0
    import QtQuick.Controls.Styles 1.1

    import "MyFunctions.js" as FormatFunctions

    import DownloadManager 1.0
    import XMLCrawler 1.0

    Item{
    id: id_itemTab
    anchors.fill: parent
    anchors.margins: 8

    ColumnLayout {
    

    ...
    }
    @

    What is the best way to access all the data on the different tabs? Do you have any suggestions? I am very thankful for any hint or suggestion...also concerning my code in general! Thank you very much!

    PS: when I hit the button 'remove tab', I get the following typeError:
    "MyTab.qml:15: TypeError: Cannot read property of null"
    where line 15 is: "anchors.fill: parent" in MyTab.qml
    I wasn't able to figure out why I get this error...any hints?



  • Alright, so I figured out how to access the data of a custom component: 'property alias' did the magic :)
    Now for the second part (creating a pdf), I am almost done, but I have a weird issue: My goal is to have several tables with only a single pixel border...unfortunately 'border-collapse' is not supported by QT's html subset. Kind of a workaround is to set the cell spacing to -1. However, then there is a weird issue with the colors/borders, as can be seen in the following pictures:
    !http://oi61.tinypic.com/96dun7.jpg(table border)!
    !http://oi57.tinypic.com/t7h34x.jpg!
    The code to produced the above pdf is:
    @
    QTextDocument doc2;
    QTextCursor myCursor(&doc2);
    myCursor.insertText("This is the first page");

    QTextTableFormat myTableFormat;
    myTableFormat.setBorder( QTextFrameFormat::BorderStyle_Solid );
    myTableFormat.setBorder(1);
    myTableFormat.setBorderBrush(QBrush(QColor(200, 200, 200)));
    myTableFormat.setCellPadding(0);
    myTableFormat.setCellSpacing(-1);
    myTableFormat.setWidth( QTextLength(QTextLength::PercentageLength, 100) );
    myTableFormat.setAlignment( Qt::AlignLeft );
    myCursor.insertTable(4, 2, myTableFormat);
    
    printer.setOutputFileName(currPath + "/test02.pdf");
    printer.setOutputFormat( QPrinter::PdfFormat );
    printer.setPaperSize(QPrinter::A4);
    printer.setPageMargins( 20, 20, 20, 20, QPrinter::Millimeter);
    doc2.print(&printer);
    printer.newPage();
    

    @
    I tested different colors and my impression is that the table always consists of two colors...Is there a way to assign these colors?

    I know I could use the webkit as well for printing (https://qt-project.org/forums/viewthread/22137). However, the dll is about 30Mb and I would like to avoid it if possible.


Log in to reply