Results 1 to 16 of 16

Thread: How to call another program?

  1. #1
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to call another program?

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to call another program?

    Have a look at QProcess

  3. #3
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to call another program?

    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

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call another program?

    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

  5. #5
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to call another program?

    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.

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call another program?

    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

  7. #7
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to call another program?

    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?

  8. #8
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to call another program?

    analogclock is another cpp file which display the analog time.

  9. #9
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to call another program?

    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

  10. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call another program?

    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");

  11. The following user says thank you to aamer4yu for this useful post:

    shihao (16th March 2010)

  12. #11
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to call another program?

    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:aintEvent(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??

  13. #12
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call another program?

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5. void MyWidget::call(); // should be --> void call();
    6. };
    To copy to clipboard, switch view to plain text mode 

    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

  14. #13
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to call another program?

    appear new error messaage which is:
    main.cpp::-1: error: undefined reference to `vtable for AnalogClock'

    Means what ?

  15. #14
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to call another program?

    Try to see if you are using any reference or pointer to analogclock object...

  16. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to call another program?

    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
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    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.

  17. #16
    Join Date
    Mar 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How to call another program?

    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.

Similar Threads

  1. How to call WinWord.exe through the Qt program?
    By bangqianchen in forum Qt Programming
    Replies: 11
    Last Post: 7th September 2010, 14:55
  2. a program to start another program.
    By zakis in forum Qt Programming
    Replies: 4
    Last Post: 17th December 2009, 19:25
  3. how to call mfc dll use qt?
    By yunpeng880 in forum Qt Programming
    Replies: 2
    Last Post: 12th March 2009, 04:32
  4. Replies: 7
    Last Post: 19th January 2008, 15:29
  5. How do I make a program out of my program?
    By Randulf in forum Newbie
    Replies: 1
    Last Post: 29th May 2007, 11:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.