How can I run the gui application in the ssh command or php exec?
-
wrote on 27 Apr 2016, 10:39 last edited by lucida
The Client: win7
The server:
Centos 7.0
Qt 5.6.0I use vnc login in the server,and in the server,I do:
Qt Creator ->New Project -> Qt Widgets Application
Kit Selection:Desktop Qt 5.6.0 GCC 64bit
Not select the "Create Gui"
Then I delete the mainwindow.h and mainwindow.cpp
Then the main.cpp:#include <QApplication>
#include <QPainter>
#include <QtGui>
#include <QTextStream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString str_img1="/home/testqt/test1.jpg";
QImage img1 = QImage(str_img1);
QPixmap pixmap1=QPixmap::fromImage( img1 );
QPainter painter1(&pixmap1);
QPen pen;
QColor color;
color.setRgb(112,112,112,255);
pen.setColor(color);
painter1.setPen(pen);
painter1.drawPoint(10,20);
QImage img2=pixmap1.toImage();
QByteArray ba;
QBuffer buf(&ba);
img2.save(&buf,"JPG");
QTextStream cout(stdout, QIODevice::WriteOnly);
cout<<ba.toBase64()<<endl;
return 0;
}Then in the server, when I use Qt Creator run the application,the application runs well,and can output the strings of the pic.
The question is that:
1:when I use vnc login in the server(X window),and open the terminal,and go to the dir of the debug,like this:
cd /home/build-untitled2-Desktop_Qt_5_6_0_GCC_64bit-Debug
./untitled2
It runs well.and can output the strings of the pic.2:when I use ssh login in the server,and go to the ,and go to the dir of the debug,like this:
cd /home/build-untitled2-Desktop_Qt_5_6_0_GCC_64bit-Debug
./untitled2
It is wrong.Prompt:
QXcbConnection: Could not connect to display
aborted3:when I use php to exec the application,it is output nothing. when I use php to exec the ls or other commands,it is right.
The 2 and The 3,are what I want to solve.Thanks very much.
-
QtCreator on a server?!
"Qt Widgets Application" - is for GUI applications, you should select "Qt Console Application" if you do not want GUI. -
Hi,
To add to @jsulm, with ssh unless you ask for X forwarding, you won't have access to any X display.
Note that since you don't show anything, there's no need to convert back and forth with QPixmap. QImage is enough for your need.
-
wrote on 28 Apr 2016, 02:55 last edited by lucida
The QT had installed in the sever. @jsulm
@SGaist
Qt Creator ->New Project -> Qt Console Application
Kit Selection:Desktop Qt 5.6.0 GCC 64bit
Then .pro file: modify QT -= gui to QT += gui
The main.php
#include <QCoreApplication>
#include <QPainter>
#include <QtGui>
#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream cout(stdout, QIODevice::WriteOnly);
QString str_img1="/home/testqt/test1.jpg";
QImage img1 = QImage(str_img1);
cout<<2<<endl;
QPixmap pixmap1=QPixmap::fromImage( img1 );
QPainter painter1(&pixmap1);
QPen pen;
QColor color;
color.setRgb(112,112,112,255);
pen.setColor(color);
painter1.setPen(pen);
painter1.drawPoint(10,20);
QImage img2=pixmap1.toImage();
QByteArray ba;
QBuffer buf(&ba);
img2.save(&buf,"JPG");
cout<<ba.toBase64()<<endl;
return 0;
}Then run the application use Qt Creator,can only output : 2
When I use ssh ,output : 2 segment fault
The question are that:
1:When I donot use QPixmap,what is the code about using QPainter,and Qpen,and Qcolor?
2:When use QPixmap,How to solve the question?Thanks!
-
wrote on 28 Apr 2016, 03:06 last edited by
When I donot use Gui,How can I use QImage and others like QPainter ,etc.
Thanks! -
wrote on 28 Apr 2016, 03:23 last edited by Paul Colby
Hi @lucida,
2:when I use ssh login in the server,and go to the ,and go to the dir of the debug,like this:
...
QXcbConnection: Could not connect to display abortedAs @SGaist said, you'd need to use X11 Forwarding for the SSH option to work. Which SSH client are you using?
Since your client is Windows, you're probably using PuTTY, in which case see Using PuTTY - 3.4 Using X11 forwarding in SSH.
When diagnosing X11 forwarding, I highly recommend you install an ultra-simple GUI app (I like to use 'xclock'), and get that working first. That way you know if the problem is X11-over-ssh or your app.
sudo rpm -ivh xclock xclock
Usually, you'll want to install the
xauth
package on your CentOS server too, for X11 Forwarding to work.Cheers.
-
wrote on 28 Apr 2016, 03:32 last edited by lucida
@Paul-colby
Thanks for your replay.
Use X11 Forwarding can only solve putty-ssh.
Then how to solve the php exec the Qt application NOT having X11 or X-window,etc.
Thanks! -
wrote on 28 Apr 2016, 03:43 last edited by
The important question is that:
How to use QImage,QPainter,QPixmap,etc in the Core environment.
If not,what should I do to deal with the needs of the IMG.
Thanks! -
wrote on 28 Apr 2016, 04:28 last edited by
@lucida said:
Then how to solve the php exec the Qt application NOT having X11 or X-window,etc.
How are you using PHP?
Are you logging into the server and using a PHP script to execute your Qt application? If so, can you show as the PHP code?
Or are you trying to use PHP under a web server such as Apache? In which case you'd need Qt to be able to render HTML, which you can do manually, but I don't think QtGUI supports.
Please explain a little more what you mean by using "php to exec the application".
Thanks.
-
wrote on 28 Apr 2016, 05:02 last edited by
@Paul-colby
Thanks !testqt.php
<?php
exec("sudo whoami",$output);
print_r($output); //can output,rightexec("ls",$output);
print_r($output);//can output,right$appdir="/home/qttest/build-untitled1-Desktop_Qt_5_6_0_GCC_64bit-Release/untitled1";
exec($appdir,$info);
print_r($info);//output nothing,wrong!
?>
using Apache/nginx server by http://www.x.com/testqt.php
Thanks! -
wrote on 28 Apr 2016, 05:23 last edited by Paul Colby
@lucida said:
$appdir="/home/qttest/build-untitled1-Desktop_Qt_5_6_0_GCC_64bit-Release/untitled1";
exec($appdir,$info);
print_r($info);//output nothing,wrong!Ok, so executing any application (Qt-based on not) that way will only get you the stdout (and stderr with some effort) from the app. In this case, as @jsulm said, you should create a Qt Console Application, rather than a GUI application.
However, you also said:
When I donot use Gui,How can I use QImage and others like QPainter ,etc
What are you wanting to use QImage etc for? Taking a step back, what do you want the web page to be returning?
If, for example, you're wanting the web page to show a fully rendered Qt-based GUI in HTML, I don't think that's going to be possible. You'd probably be better off investigating something like GWT, though that's Java, and I'm not aware of a C++ equivalent.
If, on the other hand, you just what the script to render a single asset - eg a single PNG or JPEG image, then you can use Qt to create a console application, and output the generated image's byte data to stdout. Though in that case, I'd seriously consider using something like FastCGI to run your Qt app without PHP being involved.
Again, it depends on what you're trying to achieve by running Qt via PHP in a web server.
Cheers.
-
wrote on 28 Apr 2016, 05:29 last edited by
Thank you very much!
1/12