PDA

View Full Version : How to call another program?



shihao
16th March 2010, 04:05
Dear all,

I would like to create an button that once you click, it pop up another clock program that display the times.

My program is something like this

QToolButton * mybutton2 = new QToolButton(this);
mybutton2->setIcon(QIcon("/shihao/qicon/clock.JPG"));
mybutton2->setText("Sample text");
mybutton2->setToolButtonStyle(Qt::ToolButtonIconOnly);
mybutton2->setIconSize(QSize(50,80));
mybutton2->setGeometry(490, 70, 95, 100);
mybutton2->setAutoRaise(1);

connect(mybutton2, SIGNAL(clicked()), qApp, SLOT(quit()));
What should I do in order to connect to another program? what should I replace with the quit() function? Where should I put the clock program? should be inside the same cpp file or should be the same directory?

Thanks in advance.

ChrisW67
16th March 2010, 04:11
Have a look at QProcess

shihao
16th March 2010, 07:04
Sorry, I just start to use QT few days ago, I still feel confuse about the documentation,

In my case, I still need to create a new processes?

QProcess *myProcess = new QProcess(parent);
myProcess->start(analogclock.cpp,-qws);

It return me error message:
/shihao/shihao3/main.cpp:50: error: ‘analogclock’ was not declared in this scope
/shihao/shihao3/main.cpp:49: error: ‘qws’ was not declared in this scope.

And how I relate the starting program process with the button event?

Thanks and Regards
Shi Hao

aamer4yu
16th March 2010, 07:08
I dont think you can run .cpp files, can you ?
Also check the arguments type... analog.cpp should have been in quotes... "analog.cpp"
Give time to assistant and learning... it will help :)

shihao
16th March 2010, 07:16
Yes, I cannot run the cpp file..
What I want to do is when I click a button, a submenu which is the analogclock.cpp will appear and replace the previous screen
What should I do in order to do that?

How to launch the assistant ?? Thank you.

aamer4yu
16th March 2010, 07:33
What I want to do is when I click a button, a submenu which is the analogclock.cpp will appear and replace the previous screen
Didnt get that ,, can you elaborate ?

For Qt Assistant, go the QTDIR/bin folder. You will find the assistant.exe . I wonder if you havent used it till now :rolleyes:

shihao
16th March 2010, 07:49
ok, there is a button on the screen.
When I press the button, I want the analogclock to be appear on the screen.
What should I do in order to do that ? OnButtonEvent or something?

shihao
16th March 2010, 07:52
analogclock is another cpp file which display the analog time.

shihao
16th March 2010, 09:45
Guys, I thought this is a simple task for expert or intermediate or expert user?
Anyone who know please help ??
What I want to do is when I click on a button, it will call analogclock.cpp. Analogclock is a cpp file that use to display the time. Any advice is appreciated.

Thanks in advance

aamer4yu
16th March 2010, 10:07
YOu cannot execute a .cpp file... only .exe file..
First you will need to compile the analog.cpp file and make a exe out of it.

Then from your program, you can call QProcess::startDetached("analog.exe");

shihao
16th March 2010, 12:26
ok, I change my mind.
I want to combine 2 program together.
So this is my code:

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QIcon>
#include <qpixmap.h>
#include <QAbstractButton>
#include <QToolButton>
#include <QStyle>
#include <QtGui>
#include "analogclock.h"

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
void MyWidget::call();
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(900, 400);
QToolButton * mybutton2 = new QToolButton(this);
mybutton2->setIcon(QIcon("/shihao/qicon/clock.JPG"));
mybutton2->setText("Sample text");
mybutton2->setToolButtonStyle(Qt::ToolButtonIconOnly);
mybutton2->setIconSize(QSize(50,80));
mybutton2->setGeometry(490, 70, 95, 100);
mybutton2->setAutoRaise(1);

connect(mybutton2, SIGNAL(clicked()), qApp, SLOT(call()));
}

void MyWidget::call(){
AnalogClock a;
a.show();
}


AnalogClock::AnalogClock(QWidget *parent)
: QWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
setWindowTitle(tr("Analog Clock"));
resize(200, 200);
}

void AnalogClock::paintEvent(QPaintEvent *)
{
static const QPoint hourHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -40)
};
static const QPoint minuteHand[3] = {
QPoint(7, 8),
QPoint(-7, 8),
QPoint(0, -70)
};

QColor hourColor(127, 0, 127);
QColor minuteColor(0, 127, 127, 191);

int side = qMin(width(), height());
QTime time = QTime::currentTime();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);
painter.setPen(Qt::NoPen);
painter.setBrush(hourColor);
painter.save();
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 3);
painter.restore();
painter.setPen(hourColor);

for (int i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);
}
painter.setPen(Qt::NoPen);
painter.setBrush(minuteColor);
painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(minuteHand, 3);
painter.restore();
painter.setPen(minuteColor);

for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
}


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
return app.exec();
}

error message: /main.cpp:18: error: extra qualification ‘MyWidget::’ on member ‘call’, what's wrong with my code??

aamer4yu
16th March 2010, 12:30
class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);
void MyWidget::call(); // should be --> void call();
};

Also in your call() function you are creating
AnalogClock a;
a.show();

which will disappear since a will created on stack. make it a member variable instead.

Also, please put the code in [CODE] tags

shihao
16th March 2010, 13:14
appear new error messaage which is:
main.cpp::-1: error: undefined reference to `vtable for AnalogClock'

Means what ?

aamer4yu
16th March 2010, 14:03
Try to see if you are using any reference or pointer to analogclock object...

ChrisW67
16th March 2010, 23:47
The `vtable for AnalogClock' message usually means that moc has not been run over the source, the resulting moc output has not been included, or that a necessary Q_OBJECT macro is missing. I think it is the last option that you are lacking in MyWidget and/or AnalogClock, and adding it will allow qmake to work things out for you.

If you leave the source all in the one file then you should
#include "main.moc"somewhere.


Your MyWidget::call method is not declared as a slot.
You will need to post your code and pro file complete, wrapped in [code] tags or as an attachment, if you need more specific help.

shihao
17th March 2010, 06:42
Thanks ChrisW67 and aamer4yu for the reply.

Also in your call() function you are creating
AnalogClock a;
a.show();
which will disappear since a will created on stack. make it a member variable instead.
Also, please put the code in [CODE] tags

How to do that?

Anyway, I have amend the code, and don't have any error already. When I click the button, I want the clock to be appear, but seems that it doesn't work. Here is my code

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QIcon>
#include <qpixmap.h>
#include <QAbstractButton>
#include <QToolButton>
#include <QStyle>
#include <QtGui>
#include <QApplication>
#include "analogclock.h"

class MyWidget : public QWidget
{
public:
MyWidget(QWidget *parent = 0);

public slots:
void call();
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(900, 400);
QToolButton * mybutton = new QToolButton(this);
mybutton->setIcon(QIcon("/shihao/icon/icon/Technorati.JPG"));
mybutton->setText("Sample text");
mybutton->setToolButtonStyle(Qt::ToolButtonIconOnly);
mybutton->setIconSize(QSize(80,90));
mybutton->setGeometry(270, 70, 95, 100);
mybutton->setAutoRaise(1);

QToolButton * mybutton1 = new QToolButton(this);
mybutton1->setIcon(QIcon("/shihao/qicon/qicon7.JPG"));
mybutton1->setText("Sample text");
mybutton1->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
mybutton1->setIconSize(QSize(50,70));
mybutton1->setGeometry(380, 70, 95, 100);
mybutton1->setAutoRaise(1);

QToolButton * mybutton2 = new QToolButton(this);
mybutton2->setIcon(QIcon("/shihao/qicon/clock.JPG"));
mybutton2->setText("Sample text");
mybutton2->setToolButtonStyle(Qt::ToolButtonIconOnly);
mybutton2->setIconSize(QSize(50,80));
mybutton2->setGeometry(490, 70, 95, 100);
mybutton2->setAutoRaise(1);

connect(mybutton2, SIGNAL(clicked()), qApp, SLOT(call()));
}

void MyWidget::call(){
AnalogClock clock;
clock.show();
}


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget widget;
widget.show();
//AnalogClock clock;
//clock.show();
return app.exec();
}

The output that I want is when I click the button, the clock only will displayed. Now when I click on the button, nothing happened. What should I do?
Please be noted that when I uncomment the analogclock and instead of widget in the main function, the clock able to display. The clock able to run correctly on main but unable to run in function. What should I do? Thanks for the helps for far.