QPainter::begin():返回错误

QPainter::begin(): Returned false

所以我正在使用 QT 5.15.2,我想在这里做的是将 QTableView 的内容打印到 PDF 文件中,这是我的代码

 void ManageTeachers::on_print_clicked() {

    QPdfWriter pdf("C:");
    QPainter painter(&pdf);

        int i = 4000;
            painter.setPen(Qt::blue);
            painter.setFont(QFont("Arial", 30));
            painter.drawText(1100,1200,"List of teachers");
            painter.setPen(Qt::black);
            painter.setFont(QFont("Arial", 15));
           
            painter.drawRect(100,100,7300,2600);
            painter.drawRect(0,3000,9600,500);
            painter.setFont(QFont("Arial", 9));
            painter.drawText(200,3300,"CIN");
            painter.drawText(1300,3300,"First name");
            painter.drawText(2100,3300,"last name");
            painter.drawText(3200,3300,"phone");
            painter.drawText(5300,3300,"email");

            painter.drawText(5300,3300,"salary");
            QSqlQuery query;
            query.prepare("select * from teacher");
            query.exec();
            while (query.next())
            {
                painter.drawText(200,i,query.value(0).toString());
                painter.drawText(1300,i,query.value(1).toString());
                painter.drawText(2200,i,query.value(2).toString());
                painter.drawText(3200,i,query.value(3).toString());
                painter.drawText(4500,i,query.value(4).toString());
               i = i + 500;
            }
            int reponse = QMessageBox::question(this, "Génerer PDF", "<PDF Enregistré>...Vous Voulez Affichez Le PDF ?", QMessageBox::Yes |  QMessageBox::No);
                if (reponse == QMessageBox::Yes)
                {

                    painter.end();
                }
                if (reponse == QMessageBox::No)
                {
                     painter.end();
                } }

问题是我在控制台中收到这些错误

QPainter::begin(): Returned false
QPainter::setPen: Painter not active
QPainter::setFont: Painter not active
QPainter::setPen: Painter not active
QPainter::setFont: Painter not active
QPainter::drawRects: Painter not active
QPainter::drawRects: Painter not active
QPainter::setFont: Painter not active
QPainter::end: Painter not active, aborted

有什么帮助吗?我添加了 QT+= printsupport multimedia

QPdfWriter pdf("C:");
QPainter painter(&pdf);

这很可疑。如果您查阅 documentation for QPdfWriter,它会指出第一个参数应该是文件名。 “C:”不是文件名。您需要传递要写入的文件的名称,例如:

QString filename("C:/my_file.pdf");
QPdfWriter pdf(filename);
QPainter painter(&pdf);