Hi, i need help
i do not know how i can call external view (Adobe) to open *.pdf ... from Qt.
Printable View
Hi, i need help
i do not know how i can call external view (Adobe) to open *.pdf ... from Qt.
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.
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.
Code:
if (Environment::isSet(EnvironmentVariableIdentifyingDesktopLauncher)) { 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) { }
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 ;)
I onlyy know how to start an excutive application:
Code:
QStringList arguments; arguments << "-style" << "motif"; myProcess->start(program, arguments);
the "program" is the path of the application you want to run.
I'm using QProcess::startDetached, to do the same, fwiw.
Jep. Thats better to prevent the GUI from freezing if the started program takes a long time to start.
why don't you just use QDesktopServices:: openUrl?
Cool. I didn't know about this class. Works great.
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:
the path is the path of the file to be run or open.
thanks
can you provide a compilable example?
Maybe the source codes are a little complicated:
Code:
{ 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 QFileIconProvider seekIcon; 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"; //This SLOT is called by these inner code, not the user, then return if(!sender) { return; } qDebug() << "Sender found successfully!"; QString path; 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 { qDebug() << "Fail to run the file"; } qDebug() << "SLOT successfully!" << endl; }
maybe something wrong with path.
try this example, works fine for me.
Code:
#include <QtGui> #include <QApplication> int main(int argc, char *argv[]) { qDebug() << "fail"; return 0; }
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:
to
yes, that's true.
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.
thanks, it do works.
but, uh, it cannot open the files with extention ".png" or "jpg", a little strange.
in another application, it does works!!!