Qt我的程序在使用printdialog后退出

时间:2015-12-16 11:42:47

标签: qt pointers qt5

使用我的QPrintDialog后,我的完整运行程序退出而不仅仅是QPrintDialog。我无法解释原因。 这是源代码:

在这个片段中,我初步化了我的PrintDialog功能。

void VerteilerData::on_pushButton_calc_clicked()
{
    emit (on_pushButton_Load_clicked());
    //Beim Initialisieren von Printing druck eine 0 übergeben um in Printing vert.getdata zu skippen
    QString QHelp="0";
    Printing druck(this,QHelp);
    druck.print_calculation(vert_anz,pro_idthis);
}

这是我启动QPrintDialog的代码; 如果您需要一些细节,请询问我是否会添加信息

.
.
.
.(Some SQL Queries for the information i need)

{
printAuss(angebot);
}


//Here Starts the Dialog

void Printing::printAuss(const QStringList &entries)
{


    QPrintDialog printDialog(&printer);
       if (printDialog.exec())
     {
         QPainter painter (&printer);
         QList<QStringList> pages;

         paginate(&painter,&pages,entries);
         printPages(&painter, pages);
         painter.end();
         return;
     }
}

void Printing::paginate (QPainter *painter, QList<QStringList> *pages, const QStringList &entries)
{


    QStringList currentPage;
    QImage test(":/pics/BriefbogenGroß.jpg");
    qDebug()<<"Höhe_neu"<<test.height();
    painter->setWindow(test.rect());
    int pageHeight = painter->window().height() -1 * TopGap -1 * BottomGap;
    int y=0;
    foreach (QString entry, entries)
    {    qDebug()<<entries.size();
        int height;
        height= entryHeight(painter, entry);

        if (y+height > pageHeight && !currentPage.empty())
        {

            pages->append(currentPage);
            currentPage.clear();
            y = 0;
        }
        currentPage.append(entry);
        y+= height+ MediumGap;
    }
    if(!currentPage.empty())
        pages->append(currentPage);
 }

int Printing::entryHeight(QPainter *painter, const QString &entry)
{


    QStringList fields = entry.split(" § ");
    QString title = fields[0];
    QString body =fields[1];
    QString price =fields[2];

    QImage test(":/pics/BriefbogenGroß.jpg");
    painter->setWindow(test.rect());
    int textWidth = painter->window().width() - 50* SmallGap;  //Breite
    int maxHeight = painter->window().height();
    qDebug()<<"höhe "<<maxHeight;
    painter->setFont(titleFont);
    QRect titleRect = painter->boundingRect(0,0, textWidth,maxHeight,
                                            Qt::TextWordWrap, title);
    painter->setFont(bodyFont);
    QRect bodyRect = painter->boundingRect(0,0, textWidth, maxHeight,
                                            Qt::TextWordWrap, body);
    painter->setFont(titleFont);
    QRect priceRect = painter->boundingRect(0,0,textWidth,maxHeight,
                                            Qt::TextWordWrap, price);

    return titleRect.height() + bodyRect.height() + priceRect.height() + 4 * SmallGap;
}

void Printing::printPages(QPainter *painter, const QList<QStringList> &pages)
{
    int firstPage = printer.fromPage() -1;
    if(firstPage >=pages.size())
        return;
    if(firstPage== -1)
        firstPage=0;
    int lastPage = printer.toPage() -1;
    if(lastPage == -1 || lastPage>= pages.size())
        lastPage = pages.size() -1;
    int numPages = lastPage - firstPage +1;
    for(int i = 0;i<printer.numCopies();++i)
    {
        for(int j = 0; j<numPages;++j)
        {
            if (i !=0 || j!=0)
                printer.newPage();

            int index;
            if(printer.pageOrder() == QPrinter::FirstPageFirst)
            {
                index = firstPage + j;
            }
            else
            {
                index =lastPage - j;
            }
            printPage(painter,pages[index],index + 1,numPages );
        }
    }

}

void Printing::printPage(QPainter *painter, const QStringList &entries, int pageNumber,int maxPages)
{
    QString Seitenzahl;
    Seitenzahl=QString::number(pageNumber);
    Seitenzahl.append(" / ");
    Seitenzahl.append(QString::number(maxPages));
    painter->save();
    QImage test(":/pics/BriefbogenGroß.jpg");
    painter->setWindow(test.rect());
    qDebug()<<"Image Höchte: "<<test.height();
    painter->drawImage(0,0,test);
    painter->translate(0, TopGap);
    foreach (QString entry, entries) {
        QStringList fields = entry.split(" § ");
        QString title = fields[0];
        QString body = fields[1];
        QString price = fields[2];
        printBox(painter,title,titleFont,Qt::lightGray,false);
        printBox(painter,body,bodyFont, Qt::white,false);
        printBox(painter,price,titleFont,Qt::white,true);
        painter->translate(0,MediumGap);
    }
    //Was Ans ende des Dokumentes schreiben
    if(pageNumber==maxPages)
    {
        painter->translate(0,30);
        QString help;
        help.append("Gesamtnettopreis.....: ");
        help.append(QString::number(gesamtpreis));
        help.append(" €");
        painter->setFont(titleFont);

        int boxWidth = painter->window().width();

        int textWidth = boxWidth -50 * SmallGap; // 2
        int maxHeight = painter->window().height();
        QRect textRect = painter->boundingRect(SmallGap,SmallGap,textWidth,maxHeight,Qt::TextWordWrap | Qt::AlignRight,help);
        painter->drawText(textRect,Qt::TextWordWrap,help);

    }
    painter->restore();
    painter->setFont(bodyFont);
    painter->drawText(painter->window(),
                      Qt::AlignHCenter | Qt:: AlignBottom,
                      Seitenzahl);


}
void Printing::printBox(QPainter *painter, const QString &str, const QFont &font, const QBrush &brush,bool price)
{
    painter->setFont(font);

    int boxWidth = painter->window().width()-50*SmallGap; // -- 45 SmallGap

    // Text Breite
    int textWidth = boxWidth -2 * SmallGap;  // 2
    int maxHeight = painter->window().height();
     QRect textRect;
    if(price==true)
    {
             textRect = painter->boundingRect(SmallGap*23.5,SmallGap,textWidth,maxHeight,Qt::TextWordWrap | Qt::AlignRight,str); //
    }
    else
    {
             textRect = painter->boundingRect(SmallGap*23.5,SmallGap,textWidth,maxHeight,Qt::TextWordWrap | Qt::AlignLeft,str); //
    }

    int boxHeight = textRect.height() + 2*SmallGap;
    painter->setPen(QPen(Qt::black,2.0,Qt::SolidLine));
    painter->setBrush(brush);
    painter->drawRect(22.5*SmallGap,0,boxWidth,boxHeight);
    painter->drawText(textRect,Qt::TextWordWrap ,str);
    painter->translate(0,boxHeight);
}

0 个答案:

没有答案