I'm having a bit of trouble with my function for displaying pdf's with the poppler library. The code below is the function in which the problem occurs.
const QString &file is the path to the file
int page is the page on which it has to open
When i set file to a real path (e.g. "/Users/User/Documents/xxx.pdf"), it is no problem to open it. But when i give the path to a qrc file (":/files/xxx.pdf"), it won't work. I want to use it for displaying a user manual for instance, within the application.
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.
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*/
}
}
I hope you can help me,
greetings,
Matt
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();
Perfectly reads all data from the resource, have checked it. So i suggest you did something wrong when tried that. Maybe path errors, maybe something else...
Related
I have a problem with the QFileDialog class, namely with the setDirectory() and directory() methods. I need to make it so that after opening a file, my program remembers the directory in which the selected file is stored, and the next time QFileDialog is called, it automatically opens the directory that was used last. Here is a snippet of my code:
static QString _st_doc_last_directory;
void MainWindow::open()
{
if (!fileDialog)
{ fileDialog = new QFileDialog(this);
}
if (!_st_doc_last_directory.isEmpty()) fileDialog->setDirectory(_st_doc_last_directory);
QString fileName = fileDialog->getOpenFileName(this, tr("Open Document"), ".", tr("Compressed CAD Models (*.data)"));
if (!fileName.isEmpty())
{ _st_doc_last_directory = fileDialog->directory().dirName();
}
}
The crux of my problem is that when the setDirectory() or directory() method is called, my program crashes with a
"Segmentation fault"
message. How can I fix it, please advise. Thanks in advance.
Whenever you start this method, you have this as the start window: ".". (admittedly I don't know what's going on internally, but I think this leads to this problem).
You can query beforehand whether your defined string is empty. if so you set a path, otherwise you store one in your string. If you don't want to do this from the beginning every time you start the program, you can also use QSettings. This saves you the path in the registry (ie if you use windows).
With QFileInfo you can easily get the path
void MainWindow::open()
{
if(_st_doc_last_directory.isEmpty())
_st_doc_last_directory = QDir::homePath();
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Document"), _st_doc_last_directory, tr("Compressed CAD Models (*.data)"));
QFileInfo info(fileName);
if(!fileName.isEmpty())
_st_doc_last_directory = info.absolutePath();
}
I am working to develop typing tutor project. i am unable to read a specific line from file and allow user to enter the word. so how can i fix this??
void MainWindow::on_pushButton_clicked()
{
QFile file("D:/programs/QT/file_handle/file.txt");
if(!file.open(QFile::ReadOnly | QFile::Text))
QMessageBox::warning(this,"title","file not open");
QTextStream in(&file);
while(!in.atEnd()){
line=in.readLine();
ui->textBrowser->setPlainText(line);
}
file.close();
}
void MainWindow::on_pushButton_2_clicked()
{
QString str=ui->textEdit->toPlainText();
if(line==str)
ui->label->setText("they are same");
else
ui->label->setText("they are not same");
}
i expect to pause and pass control to textEdit where user enters a word after reading the first line. but the actual output appears reading the final line of the file
You'll need to do this asynchronously.
Whether you read the whole file into a buffer / list or you keep the file open doesn't matter.
You would need to make file (or buffer / list) a member of MainWindow.
In on_pushButton_clicked() you would only read the first / next line and display it.
I already took at others questions but I didn't find an answer.
I have a problem to print HTML code I download with a QNetworkAccessManager.
I need to log into a website to retrieve this code.
I have a slot like this:
void Aims::slotRequestFinished(QNetworkReply* requestReply)
{
QString data = QString(requestReply->readAll());
qDebug() << data;
}
For the first two steps (connection), I can see the HTML code in the console.
The last step doesn't get any data. There is no redirection nor error.
Now, the stranger part is that when I change my code to show the page into a webview, qDebug doesn't show anything, but the code loaded is shown correctly in the webview.
void Aims::slotRequestFinished(QNetworkReply* requestReply)
{
QString data = QString(requestReply->readAll());
qDebug() << data;
ui->webView->setHtml(data);
}
Well, I can save the content into a file. But I would really like to understand why I can't see anything in qDebug
I'm new to QT. Currently in my project I implemented QFileDialog.
In my usecase : whenever user choose a text file, it executes functionA. However, I found that if I click cancel in the fileDialog, functionA will still be executed.
This is my code snipplet:
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Text File (*.txt"));
// I want something like following :
if(QFileDialog.isOkButtonClicked)
{
// execute functionsA
}
I looked into QFileDialog documentation and nothing similiar.
Is it possible to achieve this or is there any other solution ? thanks.
thanks to AlexanderVX
the solution is simple :
if(!fileName.isEmpty()&& !fileName.isNull()){
// functionA
}
I need to play a sound when a button is clicked, I have this:
Phonon::MediaObject *clickObject = new Phonon::MediaObject(this);
clickObject->setCurrentSource(Phonon::MediaSource("Click/sound.wav");
Phonon::AudioOutput *clickOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(clickObject, clickOutput);
and
void MainWindow::on_pushButton_clicked()
{
clickObject->play();
}
but no sound is played?
Where am I wrong?
Thanks.
EDIT: It works now, it was the wrong path.
Probably the file path "Click/sound.wav" doesn't point where you think it points.
Try this before calling the setCurrentSource()-function:
bool exists = QFile::exists("Click/sound.wav");
If the Click directory is supposed to be in the same directory as your exe, create the path like this:
QString filePath = QCoreApplication::applicationDirPath() + "/Click/sound.wav";
clickObject->setCurrentSource(Phonon::MediaSource(filePath));
And I would suggest using Qt resource system. Then you would point to the sound file like this:
clickObject->setCurrentSource(Phonon::MediaSource(":/Click/sound.wav"));
You should at least connect the signal stateChanged(Phonon::State, Phonon::State) from your MediaObject object to a custom slot to detect errors: if the state changes to Phonon::ErrorState the reason of the error might be accessible through QMediaObject::errorString().