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

Couldn't print Arabic right



  • Here's how it appears on UI and PDF:

    cap1.PNG

    looks ok on the UI BUT when I print it's terrible! Row Surah 1, Ayah 1 completely vanished in the PDF and those diacritical marks collude with previous/following rows. Here's the project in GitHub, I also have added the sample database, quran.db, for testing.


  • Moderators

    @Emon-Haque said in Couldn't print Arabic right:

    Here's the project in GitHub

    Please commit source code to GitHub as individual text files, not as a zipped file. That defeats the purpose of a version control system like Git.

    Anyway, please tell us how you tried to print as PDF.



  • @JKSH, here's my print function:

    void QueryResultView::printTable(){
        QPrinter printer;
        QPrintDialog dialog(&printer);
        if(dialog.exec() != QPrintDialog::Accepted) return;
        printer.setPageMargins(QMarginsF(72,72,72,72), QPageLayout::Point);
        printer.setResolution(fontMetrics().fontDpi());
        QTextDocument document;
        auto pageSize = printer.pageRect(QPrinter::DevicePixel).size();
        document.setPageSize(pageSize);
        QTextCursor cursor(&document);
        QTextTableFormat tableFormat;
        tableFormat.setBorder(0);
        tableFormat.setCellSpacing(0);
        tableFormat.setCellPadding(0);
        tableFormat.setHeaderRowCount(1);
        tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
        int row = resultModel->rowCount() + 1;
        int column = resultModel->columnCount();
        cursor.insertTable(row, column, tableFormat);
        addHeader(cursor);
        for (int r = 1; r < row; r++) {
            for (int c = 0; c < column; c++) {
                auto index = table->model()->index(r, c);
                cursor.insertText( table->model()->data(index).toString() );
                cursor.movePosition(QTextCursor::NextCell);
            }
        }
        auto date = QDateTime::currentDateTime().toString("dd MMMM, yyyy");
        auto time = QDateTime::currentDateTime().toString("hh:mm:ss AP");
        int pageCount = document.pageCount();
        QPainter painter(&printer);
        for (int pageNo = 0; pageNo < pageCount; pageNo++) {
            if (pageNo > 0) printer.newPage();
            const QRectF currentPage(0, pageNo * pageSize.height(), pageSize.width(), pageSize.height());
            painter.save();
            painter.translate(0, -currentPage.top());
            document.drawContents(&painter, QRectF(0, currentPage.top(), pageSize.width(), pageSize.height()));
            painter.restore();
            QRectF footerRect(0, pageSize.height(), pageSize.width(), fontMetrics().height());
            painter.drawLine(footerRect.left(), footerRect.top(), footerRect.right(), footerRect.top());
            painter.drawText(footerRect, Qt::AlignVCenter | Qt::AlignLeft, "Generated on " + date + " | " + time);
            painter.drawText(footerRect, Qt::AlignVCenter | Qt::AlignRight, QString("Page: %1/%2").arg(pageNo + 1).arg(pageCount));
        }
    }
    

    When I upload a folder in github, it doesn't work!

    EDIT
    When the print dialog appeared, I selected Microsoft Print to PDF and clicked Print after selecting file location and providing file name.



  • @JKSH, to bring in the first row, I should've had r - 1 in for:

    for (int r = 1; r < row; r++) {
        for (int c = 0; c < column; c++) {
            auto index = table->model()->index(r - 1, c);
            cursor.insertText( table->model()->data(index).toString() );
            cursor.movePosition(QTextCursor::NextCell);
        }
    }
    

    copy/paste error! BUT those diacritical marks still overlaps with previous/following rows and becomes unreadable.


  • Moderators

    @deleted385 said in Couldn't print Arabic right:

    those diacritical marks still overlaps with previous/following rows and becomes unreadable.

    I'm not familiar with the QTextCursor API, so I'll let someone else comment on your code.

    For now, I recommend you just try to print a simple PDF with 2 lines. Keep modifying the code until you get 2 lines that don't overlap.

    When I upload a folder in github, it doesn't work!

    GitHub is not for uploading files. You should commit your code on your PC and then push it to GitHub: https://docs.github.com/en/get-started/quickstart/hello-world


Log in to reply