PDA

View Full Version : Call extern app Adobe Acrobat to open *.pdf



Lodhart
26th April 2009, 12:33
Hi, i need help
i do not know how i can call external view (Adobe) to open *.pdf ... from Qt.

nightghost
26th April 2009, 12:50
OSX has the command "open". Just create a QProcess with the string "open <PATH_TO_YOUR_PDF>". OSX will use the default application to open the file. Windows and Linux have similiar commands.

nightghost
26th April 2009, 14:18
Funny, i was searching in our sourcecode today and found the code, where we implemented the external open. Maybe it will help you a bit more than me last short answer. (System and Environment are our own classes, but I think you will get it) the env variable DESKTOP_LAUNCH points to the program that handles the file open (under linux) if that variable is not set, than the default launcher is used.



const QString System::EnvironmentVariableIdentifyingDesktopLaunc her("DESKTOP_LAUNCH");


QString System::getDefaultApplicationLauncher() {
if (Environment::isSet(EnvironmentVariableIdentifying DesktopLauncher)) {
QString launcher = Environment::get(EnvironmentVariableIdentifyingDes ktopLauncher);
return launcher + " %1";
} else if (System::isWindows()) {
return "cmd /c \"start %1\"";
} else if (System::isMacOSX()) {
return "open \"%1\"";
} else if (System::runningGNOME()) {
return "gnome-open \"%1\"";
} else if (System::runningKDE()) {
return "kfmclient exec \"%1\"";
} else {
// last try. Only used by XFCE, but no better Idea for default
return "exo-open \"%1\"";
}
}


bool System::openFileWithTheDefaultApplication(const QUrl& url) {
QString launcher = getDefaultApplicationLauncher();
QString url_to_execute = url.toEncoded();
return QProcess::execute(launcher.arg(url_to_execute));
}


Maybe it can be done better, but it works for us :) The default case (the else) is not really perfekt, but we never get this far ;)

sophister
26th April 2009, 14:54
I onlyy know how to start an excutive application:


QObject *parent = NULL;
QString program = "J:\\2\\exeshan\\Debug\\11.ppt";
QStringList arguments;
arguments << "-style" << "motif";
QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments);

the "program" is the path of the application you want to run.

andy.fillebrown
26th April 2009, 22:55
I'm using QProcess::startDetached, to do the same, fwiw.

nightghost
27th April 2009, 08:35
Jep. Thats better to prevent the GUI from freezing if the started program takes a long time to start.

spirit
27th April 2009, 08:37
why don't you just use QDesktopServices:: openUrl?

nightghost
27th April 2009, 10:07
Cool. I didn't know about this class. Works great.

sophister
27th April 2009, 15:20
why don't you just use QDesktopServices:: openUrl?

I have tried this, but it cannot open files such as ".pdf", ".txt", and it only can run ".exe" applications. The following is my codes:

if(!QDesktopServices::openUrl(QUrl(path)))
{
qDebug() << "Fail to run the file";
}

the path is the path of the file to be run or open.
thanks

spirit
27th April 2009, 15:22
can you provide a compilable example?

sophister
27th April 2009, 15:45
Maybe the source codes are a little complicated:

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

bool MainWindow::on_addExeButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("choose the file or application"), qApp->applicationDirPath(), tr("Excutable(*.*)"));
if(filename.isNull())
return false;
//User chooses a file that has been chosen before
if(buttons.keys().contains(filename))
{
QMessageBox::critical(this, tr("Existed File"), tr("Sorry but the file you choose has been chose before.\n"
"check the buttons on the interface!!"));
return false;
}
//User chooses a new file which has not been selected before
QFileInfo fileInfo(filename);
QFileIconProvider seekIcon;
QIcon icon = seekIcon.icon(fileInfo);
QToolButton *btn = new QToolButton;
btn->resize( 500, 500);
btn->setIcon(icon);
connect(btn, SIGNAL(clicked()), this, SLOT(callExe()));
//click the button to run the corresponding exe or open the file
buttons.insert(filename, btn);
ui->verticalLayout1->addWidget(btn);
ui->verticalLayout1->addStretch();
qDebug() << buttons.size();
return true;
}

void MainWindow::callExe()
{
qDebug() << "Ready to execute program";
QToolButton *sender = qobject_cast<QToolButton*>(this->sender());
//This SLOT is called by these inner code, not the user, then return
if(!sender)
{
return;
}

qDebug() << "Sender found successfully!";
QString path;
QMapIterator<QString, QToolButton *> i(buttons);
while(i.hasNext())
{
// qDebug() << "Enter the while loop successfully!";
if(i.peekNext().value() == sender)
{
path = i.next().key();
qDebug() << "Find the path!!\n";
break;
}
else
{
i.next();
}
}

if(path.isEmpty())
{
qDebug() << "******File path is null!!";
return;
}
//打开相应程序 In English------Using QProcess to run the applications
//Using class QDesktopServices to run the files or applications
if(!QDesktopServices::openUrl(QUrl(path)))
{
qDebug() << "Fail to run the file";
}

qDebug() << "SLOT successfully!" << endl;
}

spirit
27th April 2009, 15:59
maybe something wrong with path.
try this example, works fine for me.


#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

const QString file = QFileDialog::getOpenFileName();

if (!QDesktopServices::openUrl(file))
qDebug() << "fail";

return 0;
}

nightghost
27th April 2009, 16:08
We've sometimes problems (only under Windows) if the string points to a local file and the implicit cast is used. Using QUrl::fromLocalFile() works always. In the above code change line 10:


if (!QDesktopServices::openUrl(file))


to

if (!QDesktopServices::openUrl(QUrl::fromLocalFile(fi le)))

spirit
27th April 2009, 16:11
yes, that's true.

sophister
27th April 2009, 16:39
Yea, only when I add the QUrl::from Local File() does the app works!!
Thanks a lot!

But I find that it cannot open files with extention ".png".
So strange it is.

sophister
27th April 2009, 16:42
thanks, it do works.
but, uh, it cannot open the files with extention ".png" or "jpg", a little strange.

sophister
28th April 2009, 05:36
in another application, it does works!!!