2

I am trying to build a Qt app provided as an example by Qt Creator.

Code:

arrowpad.h:

#ifndef ARROWPAD_H
#define ARROWPAD_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QPushButton;
QT_END_NAMESPACE

//! [0]
class ArrowPad : public QWidget
//! [0] //! [1]
{
//! [1] //! [2]
    Q_OBJECT
//! [2]

public:
    ArrowPad(QWidget *parent = 0);

private:
    QPushButton *upButton;
    QPushButton *downButton;
    QPushButton *leftButton;
    QPushButton *rightButton;
};

#endif

arrowpad.cpp:

#include <QtGui>

#include "arrowpad.h"

ArrowPad::ArrowPad(QWidget *parent)
    : QWidget(parent)
{
//! [0]
    upButton = new QPushButton(tr("&Up"));
//! [0] //! [1]
    downButton = new QPushButton(tr("&Down"));
//! [1] //! [2]
    leftButton = new QPushButton(tr("&Left"));
//! [2] //! [3]
    rightButton = new QPushButton(tr("&Right"));
//! [3]

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(upButton, 0, 1);
    mainLayout->addWidget(leftButton, 1, 0);
    mainLayout->addWidget(rightButton, 1, 2);
    mainLayout->addWidget(downButton, 2, 1);
    setLayout(mainLayout);
}

How can I get it to run a command when I press a button?

I have found this : How to run a system command in Qt?.

But I don't know how to use it with QPushButton, it just thinks it is a string of text.

Any commands must work in Linux Fedora 19, as that is what I am running it on.

Here's the current result, where I do not think the button names are specified as (tr("&Up")); in the above .cpp script by the way.

qt app

Wilf
  • 713
  • 1
  • 11
  • 26

5 Answers5

3

Apartidge is right, but to add on to that and to have multiple buttons connected to the same function you simply have to connect the correct signals and slots.

For your example:

connect(upButton, SIGNAL(clicked()), this, SLOT(onButtonPressed()));
connect(downButton, SIGNAL(clicked()), this, SLOT(onButtonPressed()));
connect(leftButton, SIGNAL(clicked()), this, SLOT(onSomeOtherButtonPressed()));
connect(rightButton, SIGNAL(clicked()), this, SLOT(onYetAnotherButtonPressed()));

Putting this in your constructor will connect upButton and downButton to the same slot, onButtonPressed(). leftButton will trigger onSomeOtherButtonPressed() and rightButton will trigger onYetAnotherButtonPressed().

awpitt13
  • 155
  • 9
2

The button emits a pressed signal when it is clicked. What you must do is connect this signal to a slot, and then do your actions in that slot.

For instance,

ArrowPad::ArrowPad(QWidget *parent)
    : QWidget(parent)
{

    upButton = new QPushButton(tr("&Up"));
    connect(upButton, SIGNAL(clicked()), this, SLOT(onButtonPressed()));
    // And so on for the other buttons
}

void ArrowPad::onButtonPressed()
{
    // Button is pressed, do actions
}
apartridge
  • 1,790
  • 11
  • 18
1

Have you tried understanding SIGNAL and SLOT mechanism?

http://doc.qt.io/qt-4.8/signalsandslots.html

jesterjunk
  • 2,342
  • 22
  • 18
1

In qt most of things work through signal/slots mechanism. So you define some slots and simply connect the signals to them.

arrowpad.h

#ifndef ARROWPAD_H
#define ARROWPAD_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QPushButton;
QT_END_NAMESPACE

//! [0]
class ArrowPad : public QWidget
//! [0] //! [1]
{
//! [1] //! [2]
    Q_OBJECT
//! [2]

public:
    ArrowPad(QWidget *parent = 0);

private:
    QPushButton *upButton;
    QPushButton *downButton;
    QPushButton *leftButton;
    QPushButton *rightButton;



// 1- Here you declare the slots
private slots:
 void onUpButtonClick();
 void onDownButtonClick();
 void onLeftButtonClick();
 void onRightButtonClick();
};

#endif

Now in arrowpad.cpp

#include <QtGui>

#include "arrowpad.h"

ArrowPad::ArrowPad(QWidget *parent)
    : QWidget(parent)
{
//! [0]
    upButton = new QPushButton(tr("&Up"));
//! [0] //! [1]
    downButton = new QPushButton(tr("&Down"));
//! [1] //! [2]
    leftButton = new QPushButton(tr("&Left"));
//! [2] //! [3]
    rightButton = new QPushButton(tr("&Right"));
//! [3]

// 2- Connect signals to slots
  QObject::connect(upButton, signal(clicked()), this, slot(onUpButtonClick()));

  QObject::connect(downButton, signal(clicked()), this, slot(onDownButtonClick()));

  QObject::connect(rightButton, signal(clicked()), this, slot(onRightButtonClick()));

  QObject::connect(leftButton, signal(clicked()), this, slot(onLeftButtonClick()));


    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(upButton, 0, 1);
    mainLayout->addWidget(leftButton, 1, 0);
    mainLayout->addWidget(rightButton, 1, 2);
    mainLayout->addWidget(downButton, 2, 1);
    setLayout(mainLayout);
}

// 3- Define the slots
 void ArrowPad::onUpButtonClick()
{
   qDebug() << "onUpButtonClick clicked";
}
 void ArrowPad::onDownButtonClick()
{
   qDebug() << "onDownButtonClick clicked";
}
 void ArrowPad::onLeftButtonClick()
{
   qDebug() << "onLeftButtonClick clicked";
}
 void ArrowPad::onRightButtonClick()
{
   qDebug() << "onRightButtonClick clicked";
}
J.Javan
  • 813
  • 8
  • 9
0

Here's an example on how to combine QProcess and QPushButton signals using a lambda:

QPushButton *button = new QPushButton();
QProcess *process = new QProcess();

//connect QPushButton pressed signals to a lambda
button->connect(button,&QPushButton::pressed,[=]()
{
    //Here you can specify what you need to happen when the button is pressed
    process->startCommand("Some command");
});

button->show();

Read more about Signals & Slots and QProcess to be able to tweak this example, and read this for more examples: Qt Slots and C++11 lambda