2011-06-01 61 views
1

我對使用poppler庫顯示pdf的功能有點麻煩。下面的代碼是問題發生的功能。Qt:使用poppler庫打開qrc pdf

常量QString的&文件是文件路徑 INT頁面是由它來打開

當我設置文件,以一個真實的路徑(例如「/用戶/用戶/文件/ XXX的頁面。 pdf「),打開它是沒有問題的。但是當我給qrc文件(「:/files/xxx.pdf」)的路徑,它將無法正常工作。我想在應用程序中使用它來顯示用戶手冊。

我也試着先將QFile打開,打開並執行readAll,然後加載通過執行Poppler::Document::loadFromData(the qbytearray)接收到的QByteArray,但在ReadOnly模式下打開QFile時它已經出錯。

void class::setPdf(const QString &file, int page) 
{ 
    Poppler::Document *doc = Poppler::Document::load(file); 
    if (!doc) { 
     QMessageBox msgbox(QMessageBox::Critical, tr("Open Error"), tr("Please check preferences: cannot open:\n") + file, 
         QMessageBox::Ok, this); 
     msgbox.exec(); 
    } 
    else{ /*Code for displaying the pdf, which works fine*/ 
    } 
} 

我希望你能幫助我,

問候,

馬特

+0

你是怎麼用QFile打開它的?通常情況下,QFile在readOnly模式下從qrc讀取資源,沒有任何問題... – Raiv 2011-06-01 08:45:36

回答

1

I've also tried first making a QFile out of it, opening it and doing readAll, then loading the QByteArray received by doingPoppler::Document::loadFromData(the qbytearray), but it errors already when opening the QFile in ReadOnly mode.

QFile f; 
f.setFileName(":/skin/AppIcon16.png"); 
f.open(QIODevice::ReadOnly); 
QByteArray r=f.readAll(); 

完美讀取來自資源的所有數據,都檢查了。所以我建議你在嘗試時做了錯誤的事情。也許路徑錯誤,也許別的東西...

+0

這樣的錯誤來自QFile還是來自Poppler :: Document :: loadFromData(qbytearray)?當我檢查它與if(!f.open(QIODevice :: ReadOnly){ QMessageBox :: information(this,tr(「Unable to open file」),f.errorString()); } – Raiv 2011-06-01 09:37:32

+0

qfile, } 它給出它可能會損壞(繼續)的輸出,但是當我加載相同的pdf,但然後在我的高清真正的路徑,它工作正常。 它可能是在PDF和PNG在qrc文件? – 2011-06-01 09:40:25

+0

我不這麼認爲。rcc只是打包所有內容。因此,也許你應該regen你的rcc文件。你能夠從該文件獲得其他資源? – Raiv 2011-06-01 09:44:10